File: src/constraints/PointToPointConstraint.js
module.exports = PointToPointConstraint;
var Constraint = require('./Constraint');
var ContactEquation = require('../equations/ContactEquation');
/**
* Connects two bodies at given offset points.
* @class PointToPointConstraint
* @extends Constraint
* @constructor
* @param {Body} bodyA
* @param {Vec3} pivotA The point relative to the center of mass of bodyA which bodyA is constrained to.
* @param {Body} bodyB Body that will be constrained in a similar way to the same point as bodyA. We will therefore get sort of a link between bodyA and bodyB. If not specified, bodyA will be constrained to a static point.
* @param {Vec3} pivotB See pivotA.
* @param {Number} maxForce The maximum force that should be applied to constrain the bodies.
*
* @example
* var bodyA = new Body({ mass: 1 });
* var bodyB = new Body({ mass: 1 });
* bodyA.position.set(-1, 0, 0);
* bodyB.position.set(1, 0, 0);
* bodyA.addShape(shapeA);
* bodyB.addShape(shapeB);
* world.addBody(bodyA);
* world.addBody(bodyB);
* var localPivotA = new Vec3(1, 0, 0);
* var localPivotB = new Vec3(-1, 0, 0);
* var constraint = new PointToPointConstraint(bodyA, localPivotA, bodyB, localPivotB);
* world.addConstraint(constraint);
*/
function PointToPointConstraint(bodyA,pivotA,bodyB,pivotB,maxForce){
Constraint.call(this,bodyA,bodyB);
// Equations to be fed to the solver
var eqs = this.equations = [
new ContactEquation(bodyA,bodyB), // Normal
new ContactEquation(bodyA,bodyB), // Tangent2
new ContactEquation(bodyA,bodyB), // Tangent2
];
var normal = eqs[0];
var t1 = eqs[1];
var t2 = eqs[2];
t1.minForce = t2.minForce = normal.minForce = -maxForce;
t1.maxForce = t2.maxForce = normal.maxForce = maxForce;
// Update
this.update = function(){
bodyB.position.vsub(bodyA.position,normal.ni);
normal.ni.normalize();
bodyA.quaternion.vmult(pivotA,normal.ri);
bodyB.quaternion.vmult(pivotB,normal.rj);
normal.ni.tangents(t1.ni,t2.ni);
t1.ri.copy(normal.ri);
t1.rj.copy(normal.rj);
t2.ri.copy(normal.ri);
t2.rj.copy(normal.rj);
};
}
PointToPointConstraint.prototype = new Constraint();