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.sub2(bodyA.position, ri);
worldAnchorB.sub2(bodyB.position, rj);
// Compute distance vector between world anchor points
worldAnchorB.sub2(worldAnchorA, r);
final rlen = r.length;
rUnit.setFrom(r);
rUnit.normalize();
// Compute relative velocity of the anchor points, u
bodyB.velocity.sub2(bodyA.velocity, u);
// Add rotational velocity
bodyB.angularVelocity.cross2(rj, tmp);
u.add2(tmp, u);
bodyA.angularVelocity.cross2(ri, tmp);
u.sub2(tmp, u);
// F = - k * ( x - L ) - D * ( u )
rUnit.scale2(-k * (rlen - l) - d * u.dot(rUnit), f);
// Add forces to bodies
bodyA.force.sub2(f, bodyA.force);
bodyB.force.add2(f, bodyB.force);
// Angular force
ri.cross2(f, rixf);
rj.cross2(f, rjxf);
bodyA.torque.sub2(rixf, bodyA.torque);
bodyB.torque.add2(rjxf, bodyB.torque);
}