applyForce method

void applyForce()

Apply the spring force to the connected bodies.

Implementation

void applyForce() {
  final k = stiffness;
  final d = damping;
  final l = restLength;
  final bodyA = this.bodyA;
  final bodyB = this.bodyB;
  final r = _applyForceR;
  final rUnit = _applyForceRUnit;
  final u = _applyForceU;
  final f = _applyForceF;
  final tmp = _applyForceTmp;
  final worldAnchorA = _applyForceWorldAnchorA;
  final worldAnchorB = _applyForceWorldAnchorB;
  final ri = _applyForceRi;
  final rj = _applyForceRj;
  final rixf = _applyForceRixf;
  final rjxf = _applyForceRjxf;

  // Get world anchors
  getWorldAnchorA(worldAnchorA);
  getWorldAnchorB(worldAnchorB);

  // Get offset points
  worldAnchorA.vsub(bodyA.position, ri);
  worldAnchorB.vsub(bodyB.position, rj);

  // Compute distance vector between world anchor points
  worldAnchorB.vsub(worldAnchorA, r);
  final rlen = r.length();
  rUnit.copy(r);
  rUnit.normalize();

  // Compute relative velocity of the anchor points, u
  bodyB.velocity.vsub(bodyA.velocity, u);
  // Add rotational velocity

  bodyB.angularVelocity.cross(rj, tmp);
  u.vadd(tmp, u);
  bodyA.angularVelocity.cross(ri, tmp);
  u.vsub(tmp, u);

  // F = - k * ( x - L ) - D * ( u )
  rUnit.scale(-k * (rlen - l) - d * u.dot(rUnit), f);

  // Add forces to bodies
  bodyA.force.vsub(f, bodyA.force);
  bodyB.force.vadd(f, bodyB.force);

  // Angular force
  ri.cross(f, rixf);
  rj.cross(f, rjxf);
  bodyA.torque.vsub(rixf, bodyA.torque);
  bodyB.torque.vadd(rjxf, bodyB.torque);
}