the-world-engine
Version:
three.js based, unity like game engine for browser
208 lines (191 loc) • 5.33 kB
JavaScript
import { b2Maybe, b2_pi } from "../common/b2_settings.js";
import { b2Vec2 } from "../common/b2_math.js";
import { b2Color } from "../common/b2_draw.js";
export var b2JointType;
(function(t) {
t[t["e_unknownJoint"] = 0] = "e_unknownJoint";
t[t["e_revoluteJoint"] = 1] = "e_revoluteJoint";
t[t["e_prismaticJoint"] = 2] = "e_prismaticJoint";
t[t["e_distanceJoint"] = 3] = "e_distanceJoint";
t[t["e_pulleyJoint"] = 4] = "e_pulleyJoint";
t[t["e_mouseJoint"] = 5] = "e_mouseJoint";
t[t["e_gearJoint"] = 6] = "e_gearJoint";
t[t["e_wheelJoint"] = 7] = "e_wheelJoint";
t[t["e_weldJoint"] = 8] = "e_weldJoint";
t[t["e_frictionJoint"] = 9] = "e_frictionJoint";
t[t["e_ropeJoint"] = 10] = "e_ropeJoint";
t[t["e_motorJoint"] = 11] = "e_motorJoint";
t[t["e_areaJoint"] = 12] = "e_areaJoint";
})(b2JointType || (b2JointType = {}));
export class b2Jacobian {
constructor() {
this.linear = new b2Vec2;
this.angularA = 0;
this.angularB = 0;
}
SetZero() {
this.linear.SetZero();
this.angularA = 0;
this.angularB = 0;
return this;
}
Set(t, i, e) {
this.linear.Copy(t);
this.angularA = i;
this.angularB = e;
return this;
}
}
export class b2JointEdge {
constructor(t) {
this._other = null;
this.prev = null;
this.next = null;
this.joint = t;
}
get other() {
if (this._other === null) {
throw new Error;
}
return this._other;
}
set other(t) {
if (this._other !== null) {
throw new Error;
}
this._other = t;
}
Reset() {
this._other = null;
this.prev = null;
this.next = null;
}
}
export class b2JointDef {
constructor(t) {
this.type = b2JointType.e_unknownJoint;
this.userData = null;
this.collideConnected = false;
this.type = t;
}
}
export function b2LinearStiffness(t, i, e, s, n) {
const o = s.GetMass();
const r = n.GetMass();
let h;
if (o > 0 && r > 0) {
h = o * r / (o + r);
} else if (o > 0) {
h = o;
} else {
h = r;
}
const l = 2 * b2_pi * i;
t.stiffness = h * l * l;
t.damping = 2 * h * e * l;
}
export function b2AngularStiffness(t, i, e, s, n) {
const o = s.GetInertia();
const r = n.GetInertia();
let h;
if (o > 0 && r > 0) {
h = o * r / (o + r);
} else if (o > 0) {
h = o;
} else {
h = r;
}
const l = 2 * b2_pi * i;
t.stiffness = h * l * l;
t.damping = 2 * h * e * l;
}
export class b2Joint {
constructor(t) {
this.m_type = b2JointType.e_unknownJoint;
this.m_prev = null;
this.m_next = null;
this.m_edgeA = new b2JointEdge(this);
this.m_edgeB = new b2JointEdge(this);
this.m_index = 0;
this.m_islandFlag = false;
this.m_collideConnected = false;
this.m_userData = null;
this.m_type = t.type;
this.m_edgeA.other = t.bodyB;
this.m_edgeB.other = t.bodyA;
this.m_bodyA = t.bodyA;
this.m_bodyB = t.bodyB;
this.m_collideConnected = b2Maybe(t.collideConnected, false);
this.m_userData = b2Maybe(t.userData, null);
}
GetType() {
return this.m_type;
}
GetBodyA() {
return this.m_bodyA;
}
GetBodyB() {
return this.m_bodyB;
}
GetNext() {
return this.m_next;
}
GetUserData() {
return this.m_userData;
}
SetUserData(t) {
this.m_userData = t;
}
IsEnabled() {
return this.m_bodyA.IsEnabled() && this.m_bodyB.IsEnabled();
}
GetCollideConnected() {
return this.m_collideConnected;
}
Dump(t) {
t("// Dump is not supported for this joint type.\n");
}
ShiftOrigin(t) {}
Draw(t) {
const i = this.m_bodyA.GetTransform();
const e = this.m_bodyB.GetTransform();
const s = i.p;
const n = e.p;
const o = this.GetAnchorA(b2Joint.Draw_s_p1);
const r = this.GetAnchorB(b2Joint.Draw_s_p2);
const h = b2Joint.Draw_s_color.SetRGB(.5, .8, .8);
switch (this.m_type) {
case b2JointType.e_distanceJoint:
t.DrawSegment(o, r, h);
break;
case b2JointType.e_pulleyJoint:
{
const i = this;
const e = i.GetGroundAnchorA();
const s = i.GetGroundAnchorB();
t.DrawSegment(e, o, h);
t.DrawSegment(s, r, h);
t.DrawSegment(e, s, h);
}
break;
case b2JointType.e_mouseJoint:
{
const i = b2Joint.Draw_s_c;
i.Set(0, 1, 0);
t.DrawPoint(o, 4, i);
t.DrawPoint(r, 4, i);
i.Set(.8, .8, .8);
t.DrawSegment(o, r, i);
}
break;
default:
t.DrawSegment(s, o, h);
t.DrawSegment(o, r, h);
t.DrawSegment(n, r, h);
}
}
}
b2Joint.Draw_s_p1 = new b2Vec2;
b2Joint.Draw_s_p2 = new b2Vec2;
b2Joint.Draw_s_color = new b2Color(.5, .8, .8);
b2Joint.Draw_s_c = new b2Color;