From 0ae2d028129db901a3974ff8ca192e30bd37abe2 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 21 Jun 2014 23:13:06 +0100 Subject: [PATCH] added time scaling to Sleeping --- src/core/Engine.js | 4 ++-- src/core/Sleeping.js | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/core/Engine.js b/src/core/Engine.js index f1b3b7db..09b4159d 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -200,7 +200,7 @@ var Engine = {}; // if sleeping enabled, call the sleeping controller if (engine.enableSleeping) - Sleeping.update(allBodies); + Sleeping.update(allBodies, timing.timeScale); // applies gravity to all bodies Body.applyGravityAll(allBodies, world.gravity); @@ -241,7 +241,7 @@ var Engine = {}; // wake up bodies involved in collisions if (engine.enableSleeping) - Sleeping.afterCollisions(pairs.list); + Sleeping.afterCollisions(pairs.list, timing.timeScale); // iteratively resolve velocity between collisions Resolver.preSolveVelocity(pairs.list); diff --git a/src/core/Sleeping.js b/src/core/Sleeping.js index ec097efd..d8ee00af 100644 --- a/src/core/Sleeping.js +++ b/src/core/Sleeping.js @@ -13,11 +13,14 @@ var Sleeping = {}; _minBias = 0.9; /** - * Description + * Puts bodies to sleep or wakes them up depending on their motion. * @method update * @param {body[]} bodies + * @param {number} timeScale */ - Sleeping.update = function(bodies) { + Sleeping.update = function(bodies, timeScale) { + var timeFactor = timeScale * timeScale * timeScale; + // update bodies sleeping status for (var i = 0; i < bodies.length; i++) { var body = bodies[i], @@ -35,7 +38,7 @@ var Sleeping = {}; // biased average motion estimation between frames body.motion = _minBias * minMotion + (1 - _minBias) * maxMotion; - if (body.sleepThreshold > 0 && body.motion < _motionSleepThreshold) { + if (body.sleepThreshold > 0 && body.motion < _motionSleepThreshold * timeFactor) { body.sleepCounter += 1; if (body.sleepCounter >= body.sleepThreshold) @@ -47,11 +50,14 @@ var Sleeping = {}; }; /** - * Description + * Given a set of colliding pairs, wakes the sleeping bodies involved. * @method afterCollisions * @param {pair[]} pairs + * @param {number} timeScale */ - Sleeping.afterCollisions = function(pairs) { + Sleeping.afterCollisions = function(pairs, timeScale) { + var timeFactor = timeScale * timeScale * timeScale; + // wake up bodies involved in collisions for (var i = 0; i < pairs.length; i++) { var pair = pairs[i]; @@ -72,7 +78,7 @@ var Sleeping = {}; var sleepingBody = (bodyA.isSleeping && !bodyA.isStatic) ? bodyA : bodyB, movingBody = sleepingBody === bodyA ? bodyB : bodyA; - if (!sleepingBody.isStatic && movingBody.motion > _motionWakeThreshold) { + if (!sleepingBody.isStatic && movingBody.motion > _motionWakeThreshold * timeFactor) { Sleeping.set(sleepingBody, false); } }