lib/joint/DistanceJoint.js
function DistanceJoint(def, bodyA, bodyB, anchorA, anchorB) {
  if (!(this instanceof DistanceJoint)) {
    return new DistanceJoint(def, bodyA, bodyB, anchorA, anchorB);
  }

  // order of constructor arguments is changed in v0.2
  if (bodyB && anchorA && ('m_type' in anchorA) && ('x' in bodyB) && ('y' in bodyB)) {
    var temp = bodyB;
    bodyB = anchorA;
    anchorA = temp;
  }

  def = options(def, DEFAULTS);
  Joint.call(this, def, bodyA, bodyB);
  bodyA = this.m_bodyA;
  bodyB = this.m_bodyB;

  this.m_type = DistanceJoint.TYPE;

  // Solver shared
  this.m_localAnchorA = anchorA ? bodyA.getLocalPoint(anchorA) : def.localAnchorA || Vec2.zero();
  this.m_localAnchorB = anchorB ? bodyB.getLocalPoint(anchorB) : def.localAnchorB || Vec2.zero();
  this.m_length = Math.isFinite(def.length) ? def.length :
    Vec2.distance(bodyA.getWorldPoint(this.m_localAnchorA), bodyB.getWorldPoint(this.m_localAnchorB));
  this.m_frequencyHz = def.frequencyHz;
  this.m_dampingRatio = def.dampingRatio;
  this.m_impulse = 0.0;
  this.m_gamma = 0.0;
  this.m_bias = 0.0;

  // Solver temp
  this.m_u; // Vec2
  this.m_rA; // Vec2
  this.m_rB; // Vec2
  this.m_localCenterA; // Vec2
  this.m_localCenterB; // Vec2
  this.m_invMassA;
  this.m_invMassB;
  this.m_invIA;
  this.m_invIB;
  this.m_mass;

  // 1-D constrained system
  // m (v2 - v1) = lambda
  // v2 + (beta/h) * x1 + gamma * lambda = 0, gamma has units of inverse mass.
  // x2 = x1 + h * v2

  // 1-D mass-damper-spring system
  // m (v2 - v1) + h * d * v2 + h * k *

  // C = norm(p2 - p1) - L
  // u = (p2 - p1) / norm(p2 - p1)
  // Cdot = dot(u, v2 + cross(w2, r2) - v1 - cross(w1, r1))
  // J = [-u -cross(r1, u) u cross(r2, u)]
  // K = J * invM * JT
  // = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2
}
function DistanceJoint(def, bodyA, bodyB, anchorA, anchorB) {
  if (!(this instanceof DistanceJoint)) {
    return new DistanceJoint(def, bodyA, bodyB, anchorA, anchorB);
  }

  // order of constructor arguments is changed in v0.2
  if (bodyB && anchorA && ('m_type' in anchorA) && ('x' in bodyB) && ('y' in bodyB)) {
    var temp = bodyB;
    bodyB = anchorA;
    anchorA = temp;
  }

  def = options(def, DEFAULTS);
  Joint.call(this, def, bodyA, bodyB);
  bodyA = this.m_bodyA;
  bodyB = this.m_bodyB;

  this.m_type = DistanceJoint.TYPE;

  // Solver shared
  this.m_localAnchorA = anchorA ? bodyA.getLocalPoint(anchorA) : def.localAnchorA || Vec2.zero();
  this.m_localAnchorB = anchorB ? bodyB.getLocalPoint(anchorB) : def.localAnchorB || Vec2.zero();
  this.m_length = Math.isFinite(def.length) ? def.length :
    Vec2.distance(bodyA.getWorldPoint(this.m_localAnchorA), bodyB.getWorldPoint(this.m_localAnchorB));
  this.m_frequencyHz = def.frequencyHz;
  this.m_dampingRatio = def.dampingRatio;
  this.m_impulse = 0.0;
  this.m_gamma = 0.0;
  this.m_bias = 0.0;

  // Solver temp
  this.m_u; // Vec2
  this.m_rA; // Vec2
  this.m_rB; // Vec2
  this.m_localCenterA; // Vec2
  this.m_localCenterB; // Vec2
  this.m_invMassA;
  this.m_invMassB;
  this.m_invIA;
  this.m_invIB;
  this.m_mass;

  // 1-D constrained system
  // m (v2 - v1) = lambda
  // v2 + (beta/h) * x1 + gamma * lambda = 0, gamma has units of inverse mass.
  // x2 = x1 + h * v2

  // 1-D mass-damper-spring system
  // m (v2 - v1) + h * d * v2 + h * k *

  // C = norm(p2 - p1) - L
  // u = (p2 - p1) / norm(p2 - p1)
  // Cdot = dot(u, v2 + cross(w2, r2) - v1 - cross(w1, r1))
  // J = [-u -cross(r1, u) u cross(r2, u)]
  // K = J * invM * JT
  // = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2
}
function DistanceJoint ( def , bodyA , bodyB , anchorA , anchorB ) { if ( ! ( this instanceof DistanceJoint ) ) { return new DistanceJoint ( def , bodyA , bodyB , anchorA , anchorB ) ; } // order of constructor arguments is changed in v0.2 if ( bodyB && anchorA && ( 'm_type' in anchorA ) && ( 'x' in bodyB ) && ( 'y' in bodyB ) ) { var temp = bodyB ; bodyB = anchorA ; anchorA = temp ; } def = options ( def , DEFAULTS ) ; Joint . call ( this , def , bodyA , bodyB ) ; bodyA = this . m_bodyA ; bodyB = this . m_bodyB ; this . m_type = DistanceJoint . TYPE ; // Solver shared this . m_localAnchorA = anchorA ? bodyA . getLocalPoint ( anchorA ) : def . localAnchorA || Vec2 . zero ( ) ; this . m_localAnchorB = anchorB ? bodyB . getLocalPoint ( anchorB ) : def . localAnchorB || Vec2 . zero ( ) ; this . m_length = Math . isFinite ( def . length ) ? def . length : Vec2 . distance ( bodyA . getWorldPoint ( this . m_localAnchorA ) , bodyB . getWorldPoint ( this . m_localAnchorB ) ) ; this . m_frequencyHz = def . frequencyHz ; this . m_dampingRatio = def . dampingRatio ; this . m_impulse = 0.0 ; this . m_gamma = 0.0 ; this . m_bias = 0.0 ; // Solver temp this . m_u ; // Vec2 this . m_rA ; // Vec2 this . m_rB ; // Vec2 this . m_localCenterA ; // Vec2 this . m_localCenterB ; // Vec2 this . m_invMassA ; this . m_invMassB ; this . m_invIA ; this . m_invIB ; this . m_mass ; // 1-D constrained system // m (v2 - v1) = lambda // v2 + (beta/h) * x1 + gamma * lambda = 0, gamma has units of inverse mass. // x2 = x1 + h * v2 // 1-D mass-damper-spring system // m (v2 - v1) + h * d * v2 + h * k * // C = norm(p2 - p1) - L // u = (p2 - p1) / norm(p2 - p1) // Cdot = dot(u, v2 + cross(w2, r2) - v1 - cross(w1, r1)) // J = [-u -cross(r1, u) u cross(r2, u)] // K = J * invM * JT // = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2 }
A distance joint constrains two points on two bodies to remain at a fixed
distance from each other. You can view this as a massless, rigid rod.

@param {DistanceJointDef} def
@param {Body} bodyA
@param {Body} bodyB
@param {Vec2} anchorA Anchor A in global coordination.
@param {Vec2} anchorB Anchor B in global coordination.
A distance joint constrains two points on two bodies to remain at a fixed distance from each other . You can view this as a massless rigid rod .