diff --git a/src/constraint/Constraint.js b/src/constraint/Constraint.js index 730e2f0..627353a 100644 --- a/src/constraint/Constraint.js +++ b/src/constraint/Constraint.js @@ -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 @@ -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; } }; diff --git a/src/core/Engine.js b/src/core/Engine.js index 33885e9..42c9de1 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -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); }