From 9aa0945f034768ede4e45c235078274d16812913 Mon Sep 17 00:00:00 2001 From: liabru Date: Mon, 19 Jan 2015 00:30:06 +0000 Subject: [PATCH] removed Body.resetForcesAll, removed Body.applyGravityAll, removed Body.updateAll --- src/body/Body.js | 62 ---------------------------------------- src/core/Engine.js | 71 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 65 deletions(-) diff --git a/src/body/Body.js b/src/body/Body.js index ac4dd75..1166c0b 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -404,68 +404,6 @@ var Body = {}; Bounds.update(body.bounds, body.vertices, body.velocity); }; - /** - * Zeroes the `body.force` and `body.torque` force buffers. - * @method resetForcesAll - * @param {body[]} bodies - */ - Body.resetForcesAll = function(bodies) { - for (var i = 0; i < bodies.length; i++) { - var body = bodies[i]; - - // reset force buffers - body.force.x = 0; - body.force.y = 0; - body.torque = 0; - } - }; - - /** - * Applys a mass dependant force to all given bodies. - * @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; - body.force.x += body.mass * gravity.x * 0.001; - } - }; - - /** - * Applys `Body.update` to all given `bodies`. - * @method updateAll - * @param {body[]} bodies - * @param {number} deltaTime - * The amount of time elapsed between updates - * @param {number} timeScale - * @param {number} correction - * The Verlet correction factor (deltaTime / lastDeltaTime) - * @param {bounds} worldBounds - */ - Body.updateAll = function(bodies, deltaTime, timeScale, correction, worldBounds) { - for (var i = 0; i < bodies.length; i++) { - var body = bodies[i]; - - if (body.isStatic || body.isSleeping) - continue; - - // don't update out of world bodies - if (body.bounds.max.x < worldBounds.min.x || body.bounds.min.x > worldBounds.max.x - || body.bounds.max.y < worldBounds.min.y || body.bounds.min.y > worldBounds.max.y) - continue; - - Body.update(body, deltaTime, timeScale, correction); - } - }; - /** * Performs a simulation step for the given `body`, including updating position and angle using Verlet integration. * @method update diff --git a/src/core/Engine.js b/src/core/Engine.js index 660d68f..4a4664e 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -110,10 +110,10 @@ var Engine = {}; Sleeping.update(allBodies, timing.timeScale); // applies gravity to all bodies - Body.applyGravityAll(allBodies, world.gravity); + _bodiesApplyGravity(allBodies, world.gravity); // update all body position and rotation by integration - Body.updateAll(allBodies, delta, timing.timeScale, correction, world.bounds); + _bodiesUpdate(allBodies, delta, timing.timeScale, correction, world.bounds); // update all constraints for (i = 0; i < engine.constraintIterations; i++) { @@ -177,7 +177,7 @@ var Engine = {}; Metrics.update(engine.metrics, engine); // clear force buffers - Body.resetForcesAll(allBodies); + _bodiesClearForces(allBodies); // clear all composite modified flags if (world.isModified) @@ -247,6 +247,71 @@ var Engine = {}; } }; + /** + * Zeroes the `body.force` and `body.torque` force buffers. + * @method bodiesClearForces + * @private + * @param {body[]} bodies + */ + var _bodiesClearForces = function(bodies) { + for (var i = 0; i < bodies.length; i++) { + var body = bodies[i]; + + // reset force buffers + body.force.x = 0; + body.force.y = 0; + body.torque = 0; + } + }; + + /** + * Applys a mass dependant force to all given bodies. + * @method bodiesApplyGravity + * @private + * @param {body[]} bodies + * @param {vector} gravity + */ + var _bodiesApplyGravity = 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; + body.force.x += body.mass * gravity.x * 0.001; + } + }; + + /** + * Applys `Body.update` to all given `bodies`. + * @method updateAll + * @private + * @param {body[]} bodies + * @param {number} deltaTime + * The amount of time elapsed between updates + * @param {number} timeScale + * @param {number} correction + * The Verlet correction factor (deltaTime / lastDeltaTime) + * @param {bounds} worldBounds + */ + var _bodiesUpdate = function(bodies, deltaTime, timeScale, correction, worldBounds) { + for (var i = 0; i < bodies.length; i++) { + var body = bodies[i]; + + if (body.isStatic || body.isSleeping) + continue; + + // don't update out of world bodies + if (body.bounds.max.x < worldBounds.min.x || body.bounds.min.x > worldBounds.max.x + || body.bounds.max.y < worldBounds.min.y || body.bounds.min.y > worldBounds.max.y) + continue; + + Body.update(body, deltaTime, timeScale, correction); + } + }; + /** * An alias for `Runner.run`, see `Matter.Runner` for more information. * @method run