Skip to content

Commit

Permalink
implemented constraint warming
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Apr 25, 2017
1 parent 26a60e4 commit daf26af
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/constraint/Constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ var Common = require('../core/Common');
return constraint;
};

/**
* Prepares for solving by constraint warming.
* @private
* @method preSolveAll
* @param {body[]} bodies
*/
Constraint.preSolveAll = function(bodies) {
for (var i = 0; i < bodies.length; i += 1) {
var body = bodies[i],
impulse = body.constraintImpulse;

if (body.isStatic || (impulse.x === 0 && impulse.y === 0 && impulse.angle === 0)) {
continue;
}

body.position.x += impulse.x;
body.position.y += impulse.y;
body.angle += impulse.angle;
}
};

/**
* Solves all constraints in a list of collisions.
* @private
Expand Down Expand Up @@ -247,9 +268,10 @@ var Common = require('../core/Common');
Bounds.update(part.bounds, part.vertices, body.velocity);
}

impulse.angle = 0;
impulse.x = 0;
impulse.y = 0;
// dampen the cached impulse for warming next step
impulse.angle *= Constraint._warming;
impulse.x *= Constraint._warming;
impulse.y *= Constraint._warming;
}
};

Expand Down
1 change: 1 addition & 0 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ var Body = require('../body/Body');
_bodiesUpdate(allBodies, delta, timing.timeScale, correction, world.bounds);

// update all constraints
Constraint.preSolveAll(allBodies);
for (i = 0; i < engine.constraintIterations; i++) {
Constraint.solveAll(allConstraints, timing.timeScale);
}
Expand Down

0 comments on commit daf26af

Please sign in to comment.