Skip to content

Commit

Permalink
added time scaling to Sleeping
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jun 21, 2014
1 parent e22ceeb commit 0ae2d02
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 12 additions & 6 deletions src/core/Sleeping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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)
Expand All @@ -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];
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 0ae2d02

Please sign in to comment.