0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-26 13:49:01 -05:00

separated gravity function, applying forces now wakes sleeping bodies

This commit is contained in:
liabru 2014-03-10 21:12:14 +00:00
parent 88bd7bc29f
commit 6f5e0d0036
3 changed files with 29 additions and 9 deletions

View file

@ -113,19 +113,30 @@ var Body = {};
* Description * Description
* @method resetForcesAll * @method resetForcesAll
* @param {body[]} bodies * @param {body[]} bodies
* @param {vector} gravity
*/ */
Body.resetForcesAll = function(bodies, gravity) { Body.resetForcesAll = function(bodies) {
for (var i = 0; i < bodies.length; i++) { for (var i = 0; i < bodies.length; i++) {
var body = bodies[i]; var body = bodies[i];
if (body.isStatic || body.isSleeping)
continue;
// reset force buffers // reset force buffers
body.force.x = 0; body.force.x = 0;
body.force.y = 0; body.force.y = 0;
body.torque = 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 // apply gravity
body.force.y += body.mass * gravity.y * 0.001; body.force.y += body.mass * gravity.y * 0.001;

View file

@ -169,7 +169,13 @@ var Engine = {};
Metrics.reset(engine.metrics); 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); MouseConstraint.update(engine.mouseConstraint, world.bodies, engine.input);
Body.updateAll(world.bodies, delta * engine.timeScale, correction, world.bounds); Body.updateAll(world.bodies, delta * engine.timeScale, correction, world.bounds);
// update all constraints // update all constraints
@ -226,13 +232,10 @@ var Engine = {};
} }
Resolver.postSolvePosition(world.bodies); Resolver.postSolvePosition(world.bodies);
if (engine.enableSleeping)
Sleeping.update(world.bodies);
Metrics.update(engine.metrics, engine); Metrics.update(engine.metrics, engine);
// clear force buffers // clear force buffers
Body.resetForcesAll(world.bodies, world.gravity); Body.resetForcesAll(world.bodies);
return engine; return engine;
}; };

View file

@ -23,6 +23,12 @@ var Sleeping = {};
var body = bodies[i], var body = bodies[i],
motion = body.speed * body.speed + body.angularSpeed * body.angularSpeed; 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), var minMotion = Math.min(body.motion, motion),
maxMotion = Math.max(body.motion, motion); maxMotion = Math.max(body.motion, motion);