Skip to content

Commit

Permalink
HingeConstraint now extends the PointToPointConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Jan 22, 2015
1 parent 2cb284b commit 23ec22d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
50 changes: 9 additions & 41 deletions src/constraints/HingeConstraint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = HingeConstraint;

var Constraint = require('./Constraint');
var PointToPointConstraint = require('./PointToPointConstraint');
var RotationalEquation = require('../equations/RotationalEquation');
var RotationalMotorEquation = require('../equations/RotationalMotorEquation');
var ContactEquation = require('../equations/ContactEquation');
Expand All @@ -19,24 +20,14 @@ var Vec3 = require('../math/Vec3');
* @param {Vec3} [options.pivotB]
* @param {Vec3} [options.axisB]
* @param {Number} [options.maxForce=1e6]
* @extends Constraint
* @extends PointToPointConstraint
*/
function HingeConstraint(bodyA, bodyB, options){
Constraint.call(this, bodyA, bodyB, options);

var maxForce = typeof(options.maxForce) !== 'undefined' ? options.maxForce : 1e6;
var pivotA = options.pivotA ? options.pivotA.clone() : new Vec3();
var pivotB = options.pivotB ? options.pivotB.clone() : new Vec3();

/**
* Pivot defined locally in bodyA.
* @property {Vec3} pivotA
*/
var pivotA = this.pivotA = options.pivotA ? options.pivotA.clone() : new Vec3();

/**
* Pivot, defined locally in bodyB.
* @property {Vec3} pivotB
*/
var pivotB = this.pivotB = options.pivotB ? options.pivotB.clone() : new Vec3();
PointToPointConstraint.call(this, bodyA, pivotA, bodyB, pivotB, options);

/**
* Rotation axis, defined locally in bodyA.
Expand All @@ -60,10 +51,6 @@ function HingeConstraint(bodyA, bodyB, options){
*/
var r2 = this.rotationalEquation2 = new RotationalEquation(bodyA,bodyB);

var normal = this.equationX = new ContactEquation(bodyA,bodyB);
var t1 = this.equationY = new ContactEquation(bodyA,bodyB);
var t2 = this.equationZ = new ContactEquation(bodyA,bodyB);

/**
* @property {RotationalMotorEquation} motorEquation
*/
Expand All @@ -74,15 +61,9 @@ function HingeConstraint(bodyA, bodyB, options){
this.equations.push(
r1, // rotational1
r2, // rotational2
t1,
t2,
normal,
motor
);

t1.minForce = t2.minForce = normal.minForce = -maxForce;
t1.maxForce = t2.maxForce = normal.maxForce = maxForce;

var unitPivotA = pivotA.unit();
var unitPivotB = pivotB.unit();

Expand All @@ -103,7 +84,7 @@ function HingeConstraint(bodyA, bodyB, options){
axisB_x_pivotB.normalize();

}
HingeConstraint.prototype = new Constraint();
HingeConstraint.prototype = new PointToPointConstraint();
HingeConstraint.constructor = HingeConstraint;

/**
Expand Down Expand Up @@ -143,10 +124,9 @@ HingeConstraint.prototype.update = function(){
eqs = this.equations,
motor = this.motorEquation,
r1 = this.rotationalEquation1,
r2 = this.rotationalEquation2,
normal = this.equationX,
t1 = this.equationY,
t2 = this.equationZ;
r2 = this.rotationalEquation2;

PointToPointConstraint.prototype.update.call(this);

var axisA_x_pivotA = this.axisA_x_pivotA;
var axisA = this.axisA;
Expand All @@ -156,18 +136,6 @@ HingeConstraint.prototype.update = function(){
var axisA_x_axisA_x_pivotA = this.axisA_x_axisA_x_pivotA;
var axisB_x_pivotB = this.axisB_x_pivotB;

// Update world positions of pivots
normal.ni.set(1,0,0);
t1.ni.set(0,1,0);
t2.ni.set(0,0,1);
bodyA.quaternion.vmult(this.pivotA,normal.ri);
bodyB.quaternion.vmult(this.pivotB,normal.rj);

t1.ri.copy(normal.ri);
t1.rj.copy(normal.rj);
t2.ri.copy(normal.ri);
t2.rj.copy(normal.rj);

axisA.cross(pivotA, axisA_x_pivotA);
if(axisA_x_pivotA.norm2() < 0.001){ // pivotA is along the same line as axisA
pivotA.tangents(axisA_x_pivotA, axisA_x_pivotA);
Expand Down
7 changes: 5 additions & 2 deletions src/constraints/PointToPointConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = PointToPointConstraint;

var Constraint = require('./Constraint');
var ContactEquation = require('../equations/ContactEquation');
var Vec3 = require('../math/Vec3');

/**
* Connects two bodies at given offset points.
Expand Down Expand Up @@ -31,17 +32,19 @@ var ContactEquation = require('../equations/ContactEquation');
function PointToPointConstraint(bodyA,pivotA,bodyB,pivotB,maxForce){
Constraint.call(this,bodyA,bodyB);

maxForce = typeof(maxForce) !== 'undefined' ? maxForce : 1e6;

/**
* Pivot, defined locally in bodyA.
* @property {Vec3} pivotA
*/
this.pivotA = pivotA.clone();
this.pivotA = pivotA ? pivotA.clone() : new Vec3();

/**
* Pivot, defined locally in bodyB.
* @property {Vec3} pivotB
*/
this.pivotB = pivotB.clone();
this.pivotB = pivotB ? pivotB.clone() : new Vec3();

/**
* @property {ContactEquation} equationX
Expand Down

0 comments on commit 23ec22d

Please sign in to comment.