diff --git a/examples/events.js b/examples/events.js index 4928e7d..d4ed4a2 100644 --- a/examples/events.js +++ b/examples/events.js @@ -108,15 +108,17 @@ Example.events = function() { Composite.add(world, stack); var shakeScene = function(engine) { + var timeScale = (1000 / 60) / engine.timing.lastDelta; var bodies = Composite.allBodies(engine.world); for (var i = 0; i < bodies.length; i++) { var body = bodies[i]; if (!body.isStatic && body.position.y >= 500) { - // Scale force accounting for mass - var forceMagnitude = (0.03 * body.mass); + // scale force for mass and time applied + var forceMagnitude = (0.03 * body.mass) * timeScale; + // apply the force over a single update Body.applyForce(body, body.position, { x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]), y: -forceMagnitude + Common.random() * -forceMagnitude diff --git a/examples/timescale.js b/examples/timescale.js index d3c47b3..b7fa1fd 100644 --- a/examples/timescale.js +++ b/examples/timescale.js @@ -42,16 +42,18 @@ Example.timescale = function() { Bodies.rectangle(0, 300, 50, 600, { isStatic: true }) ]); - var explosion = function(engine) { + var explosion = function(engine, delta) { + var timeScale = (1000 / 60) / delta; var bodies = Composite.allBodies(engine.world); for (var i = 0; i < bodies.length; i++) { var body = bodies[i]; if (!body.isStatic && body.position.y >= 500) { - // Scale force accounting for mass - var forceMagnitude = (0.05 * body.mass); + // scale force for mass and time applied + var forceMagnitude = (0.05 * body.mass) * timeScale; + // apply the force over a single update Body.applyForce(body, body.position, { x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]), y: -forceMagnitude + Common.random() * -forceMagnitude @@ -153,4 +155,4 @@ Example.timescale.for = '>=0.14.2'; if (typeof module !== 'undefined') { module.exports = Example.timescale; -} +} \ No newline at end of file diff --git a/src/body/Body.js b/src/body/Body.js index ceba18d..5e6ada4 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -813,10 +813,9 @@ var Axes = require('../geometry/Axes'); * @param {vector} force */ Body.applyForce = function(body, position, force) { - var timeScale = body.deltaTime / Body._baseDelta; - body.force.x += force.x / timeScale; - body.force.y += force.y / timeScale; var offset = { x: position.x - body.position.x, y: position.y - body.position.y }; + body.force.x += force.x; + body.force.y += force.y; body.torque += offset.x * force.y - offset.y * force.x; };