Skip to content

Commit

Permalink
removed Body.resetForcesAll, removed Body.applyGravityAll, removed Bo…
Browse files Browse the repository at this point in the history
…dy.updateAll
  • Loading branch information
liabru committed Jan 19, 2015
1 parent 0a15d7d commit 9aa0945
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 65 deletions.
62 changes: 0 additions & 62 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 68 additions & 3 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9aa0945

Please sign in to comment.