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

removed Body.resetForcesAll, removed Body.applyGravityAll, removed Body.updateAll

This commit is contained in:
liabru 2015-01-19 00:30:06 +00:00
parent 0a15d7d1b1
commit 9aa0945f03
2 changed files with 68 additions and 65 deletions

View file

@ -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

View file

@ -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