Skip to content

Commit

Permalink
fixed more issues with timing.timeScale
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed May 4, 2014
1 parent a8d7a40 commit 186d839
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ var Body = {};
* @method updateAll
* @param {body[]} bodies
* @param {number} deltaTime
* @param {number} timeScale
* @param {number} correction
* @param {bounds} worldBounds
*/
Body.updateAll = function(bodies, deltaTime, correction, worldBounds) {
Body.updateAll = function(bodies, deltaTime, timeScale, correction, worldBounds) {
for (var i = 0; i < bodies.length; i++) {
var body = bodies[i];

Expand All @@ -184,7 +185,7 @@ var Body = {};
|| body.bounds.max.y < worldBounds.min.y || body.bounds.min.y > worldBounds.max.y)
continue;

Body.update(body, deltaTime, correction);
Body.update(body, deltaTime, timeScale, correction);
}
};

Expand All @@ -193,13 +194,14 @@ var Body = {};
* @method update
* @param {body} body
* @param {number} deltaTime
* @param {number} timeScale
* @param {number} correction
*/
Body.update = function(body, deltaTime, correction) {
var deltaTimeSquared = deltaTime * deltaTime * body.timeScale;
Body.update = function(body, deltaTime, timeScale, correction) {
var deltaTimeSquared = Math.pow(deltaTime * timeScale * body.timeScale, 2);

// from the previous step
var frictionAir = 1 - body.frictionAir,
var frictionAir = 1 - body.frictionAir * timeScale * body.timeScale,
velocityPrevX = body.position.x - body.positionPrev.x,
velocityPrevY = body.position.y - body.positionPrev.y;

Expand Down
11 changes: 6 additions & 5 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ var Resolver = {};
* @method solveVelocity
* @param {pair[]} pairs
*/
Resolver.solveVelocity = function(pairs) {
var impulse = {};
Resolver.solveVelocity = function(pairs, timeScale) {
var impulse = {},
timeScaleSquared = timeScale * timeScale;

for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];
Expand Down Expand Up @@ -219,8 +220,8 @@ var Resolver = {};

// coulomb friction
var tangentImpulse = tangentVelocity;
if (tangentSpeed > normalForce * pair.friction)
tangentImpulse = normalForce * pair.friction * tangentVelocityDirection;
if (tangentSpeed > normalForce * pair.friction * timeScaleSquared)
tangentImpulse = normalForce * pair.friction * timeScaleSquared * tangentVelocityDirection;

// modify impulses accounting for mass, inertia and offset
var oAcN = Vector.cross(offsetA, normal),
Expand All @@ -230,7 +231,7 @@ var Resolver = {};
tangentImpulse *= share;

// handle high velocity and resting collisions separately
if (normalVelocity < 0 && normalVelocity * normalVelocity > _restingThresh) {
if (normalVelocity < 0 && normalVelocity * normalVelocity > _restingThresh * timeScaleSquared) {
// high velocity so clear cached contact impulse
contact.normalImpulse = 0;
contact.tangentImpulse = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ var Engine = {};
i;

// increment timestamp
timing.timestamp += delta;
timing.timestamp += delta * timing.timeScale;
timing.correction = correction;

// create an event object
Expand All @@ -216,7 +216,7 @@ var Engine = {};
Body.applyGravityAll(allBodies, world.gravity);

// update all body position and rotation by integration
Body.updateAll(allBodies, delta * timing.timeScale, correction, world.bounds);
Body.updateAll(allBodies, delta, timing.timeScale, correction, world.bounds);

// update all constraints
for (i = 0; i < engine.constraintIterations; i++) {
Expand Down Expand Up @@ -256,7 +256,7 @@ var Engine = {};
// iteratively resolve velocity between collisions
Resolver.preSolveVelocity(pairs.list);
for (i = 0; i < engine.velocityIterations; i++) {
Resolver.solveVelocity(pairs.list);
Resolver.solveVelocity(pairs.list, timing.timeScale);
}

// iteratively resolve position between collisions
Expand Down

0 comments on commit 186d839

Please sign in to comment.