From daf26af006ee0de5a5597dc91e3d81d196e5dde5 Mon Sep 17 00:00:00 2001 From: liabru Date: Tue, 25 Apr 2017 23:48:37 +0100 Subject: [PATCH] implemented constraint warming --- src/constraint/Constraint.js | 28 +++++++++++++++++++++++++--- src/core/Engine.js | 1 + 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/constraint/Constraint.js b/src/constraint/Constraint.js index 730e2f0e..627353ae 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 33885e9c..42c9de10 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); }