@box2d/debug-draw
Version:
Debug drawing helper for @box2d
192 lines (191 loc) • 6.37 kB
JavaScript
"use strict";
// MIT License
Object.defineProperty(exports, "__esModule", { value: true });
exports.b2Joint = exports.b2AngularStiffness = exports.b2LinearStiffness = exports.b2JointDef = exports.b2JointEdge = exports.b2JointType = void 0;
const b2_draw_1 = require("../common/b2_draw");
const b2_math_1 = require("../common/b2_math");
const temp = {
pA: new b2_math_1.b2Vec2(),
pB: new b2_math_1.b2Vec2(),
};
var b2JointType;
(function (b2JointType) {
b2JointType[b2JointType["e_unknownJoint"] = 0] = "e_unknownJoint";
b2JointType[b2JointType["e_revoluteJoint"] = 1] = "e_revoluteJoint";
b2JointType[b2JointType["e_prismaticJoint"] = 2] = "e_prismaticJoint";
b2JointType[b2JointType["e_distanceJoint"] = 3] = "e_distanceJoint";
b2JointType[b2JointType["e_pulleyJoint"] = 4] = "e_pulleyJoint";
b2JointType[b2JointType["e_mouseJoint"] = 5] = "e_mouseJoint";
b2JointType[b2JointType["e_gearJoint"] = 6] = "e_gearJoint";
b2JointType[b2JointType["e_wheelJoint"] = 7] = "e_wheelJoint";
b2JointType[b2JointType["e_weldJoint"] = 8] = "e_weldJoint";
b2JointType[b2JointType["e_frictionJoint"] = 9] = "e_frictionJoint";
b2JointType[b2JointType["e_motorJoint"] = 10] = "e_motorJoint";
b2JointType[b2JointType["e_areaJoint"] = 11] = "e_areaJoint";
})(b2JointType || (exports.b2JointType = b2JointType = {}));
/**
* A joint edge is used to connect bodies and joints together
* in a joint graph where each body is a node and each joint
* is an edge. A joint edge belongs to a doubly linked list
* maintained in each attached body. Each joint has two joint
* nodes, one for each attached body.
*/
class b2JointEdge {
constructor(joint, other) {
/** The previous joint edge in the body's joint list */
this.prev = null;
/** The next joint edge in the body's joint list */
this.next = null;
this.joint = joint;
this.other = other;
}
}
exports.b2JointEdge = b2JointEdge;
/**
* Joint definitions are used to construct joints.
*/
class b2JointDef {
constructor(type) {
/** Use this to attach application specific data to your joints. */
this.userData = {};
/** Set this flag to true if the attached bodies should collide. */
this.collideConnected = false;
this.type = type;
}
}
exports.b2JointDef = b2JointDef;
/**
* Utility to compute linear stiffness values from frequency and damping ratio
*/
function b2LinearStiffness(def, frequencyHertz, dampingRatio, bodyA, bodyB) {
const massA = bodyA.GetMass();
const massB = bodyB.GetMass();
let mass;
if (massA > 0 && massB > 0) {
mass = (massA * massB) / (massA + massB);
}
else if (massA > 0) {
mass = massA;
}
else {
mass = massB;
}
const omega = 2 * Math.PI * frequencyHertz;
def.stiffness = mass * omega * omega;
def.damping = 2 * mass * dampingRatio * omega;
}
exports.b2LinearStiffness = b2LinearStiffness;
/**
* Utility to compute rotational stiffness values frequency and damping ratio
*/
function b2AngularStiffness(def, frequencyHertz, dampingRatio, bodyA, bodyB) {
const IA = bodyA.GetInertia();
const IB = bodyB.GetInertia();
let I;
if (IA > 0 && IB > 0) {
I = (IA * IB) / (IA + IB);
}
else if (IA > 0) {
I = IA;
}
else {
I = IB;
}
const omega = 2 * Math.PI * frequencyHertz;
def.stiffness = I * omega * omega;
def.damping = 2 * I * dampingRatio * omega;
}
exports.b2AngularStiffness = b2AngularStiffness;
/**
* The base joint class. Joints are used to constraint two bodies together in
* various fashions. Some joints also feature limits and motors.
*/
class b2Joint {
constructor(def) {
// DEBUG: b2Assert(def.bodyA !== def.bodyB);
var _a;
this.m_type = b2JointType.e_unknownJoint;
/** @internal protected */
this.m_prev = null;
/** @internal protected */
this.m_next = null;
/** @internal protected */
this.m_islandFlag = false;
/** @internal protected */
this.m_collideConnected = false;
this.m_userData = {};
this.m_type = def.type;
this.m_edgeA = new b2JointEdge(this, def.bodyB);
this.m_edgeB = new b2JointEdge(this, def.bodyA);
this.m_bodyA = def.bodyA;
this.m_bodyB = def.bodyB;
this.m_collideConnected = (_a = def.collideConnected) !== null && _a !== void 0 ? _a : false;
this.SetUserData(def.userData);
}
/**
* Get the type of the concrete joint.
*/
GetType() {
return this.m_type;
}
/**
* Get the first body attached to this joint.
*/
GetBodyA() {
return this.m_bodyA;
}
/**
* Get the second body attached to this joint.
*/
GetBodyB() {
return this.m_bodyB;
}
/**
* Get the next joint the world joint list.
*/
GetNext() {
return this.m_next;
}
/**
* Get the user data reference.
*/
GetUserData() {
return this.m_userData;
}
/**
* Set the user data. Use this to store your application specific data.
* This is a merge operation. Only specified keys will be overridden.
*/
SetUserData(data) {
Object.assign(this.m_userData, data);
}
/**
* Short-cut function to determine if either body is enabled.
*/
IsEnabled() {
return this.m_bodyA.IsEnabled() && this.m_bodyB.IsEnabled();
}
/**
* Get collide connected.
* Note: modifying the collide connect flag won't work correctly because
* the flag is only checked when fixture AABBs begin to overlap.
*/
GetCollideConnected() {
return this.m_collideConnected;
}
/**
* Shift the origin for any points stored in world coordinates.
*/
ShiftOrigin(_newOrigin) { }
/** Debug draw this joint */
Draw(draw) {
const x1 = this.m_bodyA.GetTransform().p;
const x2 = this.m_bodyB.GetTransform().p;
const p1 = this.GetAnchorA(temp.pA);
const p2 = this.GetAnchorB(temp.pB);
draw.DrawSegment(x1, p1, b2_draw_1.debugColors.joint6);
draw.DrawSegment(p1, p2, b2_draw_1.debugColors.joint6);
draw.DrawSegment(x2, p2, b2_draw_1.debugColors.joint6);
}
}
exports.b2Joint = b2Joint;