diff --git a/src/body/Body.js b/src/body/Body.js index c46a5a7..a8a8f2b 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -113,19 +113,30 @@ var Body = {}; * Description * @method resetForcesAll * @param {body[]} bodies - * @param {vector} gravity */ - Body.resetForcesAll = function(bodies, gravity) { + Body.resetForcesAll = function(bodies) { for (var i = 0; i < bodies.length; i++) { var body = bodies[i]; - if (body.isStatic || body.isSleeping) - continue; - // reset force buffers body.force.x = 0; body.force.y = 0; body.torque = 0; + } + }; + + /** + * Description + * @method applyGravityAll + * @param {body[]} bodies + * @param {vector} gravity + */ + Body.applyGravityAll = function(bodies, gravity) { + for (var i = 0; i < bodies.length; i++) { + var body = bodies[i]; + + if (body.isStatic || body.isSleeping) + continue; // apply gravity body.force.y += body.mass * gravity.y * 0.001; diff --git a/src/core/Engine.js b/src/core/Engine.js index e17c0b2..5a308a4 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -169,7 +169,13 @@ var Engine = {}; Metrics.reset(engine.metrics); + if (engine.enableSleeping) + Sleeping.update(world.bodies); + + Body.applyGravityAll(world.bodies, world.gravity); + MouseConstraint.update(engine.mouseConstraint, world.bodies, engine.input); + Body.updateAll(world.bodies, delta * engine.timeScale, correction, world.bounds); // update all constraints @@ -226,13 +232,10 @@ var Engine = {}; } Resolver.postSolvePosition(world.bodies); - if (engine.enableSleeping) - Sleeping.update(world.bodies); - Metrics.update(engine.metrics, engine); // clear force buffers - Body.resetForcesAll(world.bodies, world.gravity); + Body.resetForcesAll(world.bodies); return engine; }; diff --git a/src/core/Sleeping.js b/src/core/Sleeping.js index 5ba54e7..da359b2 100644 --- a/src/core/Sleeping.js +++ b/src/core/Sleeping.js @@ -23,6 +23,12 @@ var Sleeping = {}; var body = bodies[i], motion = body.speed * body.speed + body.angularSpeed * body.angularSpeed; + // wake up bodies if they have a force applied + if (body.force.x > 0 || body.force.y > 0) { + Sleeping.set(body, false); + continue; + } + var minMotion = Math.min(body.motion, motion), maxMotion = Math.max(body.motion, motion);