diff --git a/src/body/Body.js b/src/body/Body.js index 912dad6d..7756e6c5 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -168,10 +168,11 @@ var Body = {}; * @method updateAll * @param {body[]} bodies * @param {number} deltaTime + * @param {number} timeScale * @param {number} correction * @param {bounds} worldBounds */ - Body.updateAll = function(bodies, deltaTime, correction, worldBounds) { + Body.updateAll = function(bodies, deltaTime, timeScale, correction, worldBounds) { for (var i = 0; i < bodies.length; i++) { var body = bodies[i]; @@ -184,7 +185,7 @@ var Body = {}; || body.bounds.max.y < worldBounds.min.y || body.bounds.min.y > worldBounds.max.y) continue; - Body.update(body, deltaTime, correction); + Body.update(body, deltaTime, timeScale, correction); } }; @@ -193,13 +194,14 @@ var Body = {}; * @method update * @param {body} body * @param {number} deltaTime + * @param {number} timeScale * @param {number} correction */ - Body.update = function(body, deltaTime, correction) { - var deltaTimeSquared = deltaTime * deltaTime * body.timeScale; + Body.update = function(body, deltaTime, timeScale, correction) { + var deltaTimeSquared = Math.pow(deltaTime * timeScale * body.timeScale, 2); // from the previous step - var frictionAir = 1 - body.frictionAir, + var frictionAir = 1 - body.frictionAir * timeScale * body.timeScale, velocityPrevX = body.position.x - body.positionPrev.x, velocityPrevY = body.position.y - body.positionPrev.y; diff --git a/src/collision/Resolver.js b/src/collision/Resolver.js index 8e535f1a..53a6b819 100644 --- a/src/collision/Resolver.js +++ b/src/collision/Resolver.js @@ -173,8 +173,9 @@ var Resolver = {}; * @method solveVelocity * @param {pair[]} pairs */ - Resolver.solveVelocity = function(pairs) { - var impulse = {}; + Resolver.solveVelocity = function(pairs, timeScale) { + var impulse = {}, + timeScaleSquared = timeScale * timeScale; for (var i = 0; i < pairs.length; i++) { var pair = pairs[i]; @@ -219,8 +220,8 @@ var Resolver = {}; // coulomb friction var tangentImpulse = tangentVelocity; - if (tangentSpeed > normalForce * pair.friction) - tangentImpulse = normalForce * pair.friction * tangentVelocityDirection; + if (tangentSpeed > normalForce * pair.friction * timeScaleSquared) + tangentImpulse = normalForce * pair.friction * timeScaleSquared * tangentVelocityDirection; // modify impulses accounting for mass, inertia and offset var oAcN = Vector.cross(offsetA, normal), @@ -230,7 +231,7 @@ var Resolver = {}; tangentImpulse *= share; // handle high velocity and resting collisions separately - if (normalVelocity < 0 && normalVelocity * normalVelocity > _restingThresh) { + if (normalVelocity < 0 && normalVelocity * normalVelocity > _restingThresh * timeScaleSquared) { // high velocity so clear cached contact impulse contact.normalImpulse = 0; contact.tangentImpulse = 0; diff --git a/src/core/Engine.js b/src/core/Engine.js index 0d2d2b15..827c6649 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -191,7 +191,7 @@ var Engine = {}; i; // increment timestamp - timing.timestamp += delta; + timing.timestamp += delta * timing.timeScale; timing.correction = correction; // create an event object @@ -216,7 +216,7 @@ var Engine = {}; Body.applyGravityAll(allBodies, world.gravity); // update all body position and rotation by integration - Body.updateAll(allBodies, delta * timing.timeScale, correction, world.bounds); + Body.updateAll(allBodies, delta, timing.timeScale, correction, world.bounds); // update all constraints for (i = 0; i < engine.constraintIterations; i++) { @@ -256,7 +256,7 @@ var Engine = {}; // iteratively resolve velocity between collisions Resolver.preSolveVelocity(pairs.list); for (i = 0; i < engine.velocityIterations; i++) { - Resolver.solveVelocity(pairs.list); + Resolver.solveVelocity(pairs.list, timing.timeScale); } // iteratively resolve position between collisions