Skip to content

Commit 0ae2d02

Browse files
committed
added time scaling to Sleeping
1 parent e22ceeb commit 0ae2d02

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/core/Engine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ var Engine = {};
200200

201201
// if sleeping enabled, call the sleeping controller
202202
if (engine.enableSleeping)
203-
Sleeping.update(allBodies);
203+
Sleeping.update(allBodies, timing.timeScale);
204204

205205
// applies gravity to all bodies
206206
Body.applyGravityAll(allBodies, world.gravity);
@@ -241,7 +241,7 @@ var Engine = {};
241241

242242
// wake up bodies involved in collisions
243243
if (engine.enableSleeping)
244-
Sleeping.afterCollisions(pairs.list);
244+
Sleeping.afterCollisions(pairs.list, timing.timeScale);
245245

246246
// iteratively resolve velocity between collisions
247247
Resolver.preSolveVelocity(pairs.list);

src/core/Sleeping.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ var Sleeping = {};
1313
_minBias = 0.9;
1414

1515
/**
16-
* Description
16+
* Puts bodies to sleep or wakes them up depending on their motion.
1717
* @method update
1818
* @param {body[]} bodies
19+
* @param {number} timeScale
1920
*/
20-
Sleeping.update = function(bodies) {
21+
Sleeping.update = function(bodies, timeScale) {
22+
var timeFactor = timeScale * timeScale * timeScale;
23+
2124
// update bodies sleeping status
2225
for (var i = 0; i < bodies.length; i++) {
2326
var body = bodies[i],
@@ -35,7 +38,7 @@ var Sleeping = {};
3538
// biased average motion estimation between frames
3639
body.motion = _minBias * minMotion + (1 - _minBias) * maxMotion;
3740

38-
if (body.sleepThreshold > 0 && body.motion < _motionSleepThreshold) {
41+
if (body.sleepThreshold > 0 && body.motion < _motionSleepThreshold * timeFactor) {
3942
body.sleepCounter += 1;
4043

4144
if (body.sleepCounter >= body.sleepThreshold)
@@ -47,11 +50,14 @@ var Sleeping = {};
4750
};
4851

4952
/**
50-
* Description
53+
* Given a set of colliding pairs, wakes the sleeping bodies involved.
5154
* @method afterCollisions
5255
* @param {pair[]} pairs
56+
* @param {number} timeScale
5357
*/
54-
Sleeping.afterCollisions = function(pairs) {
58+
Sleeping.afterCollisions = function(pairs, timeScale) {
59+
var timeFactor = timeScale * timeScale * timeScale;
60+
5561
// wake up bodies involved in collisions
5662
for (var i = 0; i < pairs.length; i++) {
5763
var pair = pairs[i];
@@ -72,7 +78,7 @@ var Sleeping = {};
7278
var sleepingBody = (bodyA.isSleeping && !bodyA.isStatic) ? bodyA : bodyB,
7379
movingBody = sleepingBody === bodyA ? bodyB : bodyA;
7480

75-
if (!sleepingBody.isStatic && movingBody.motion > _motionWakeThreshold) {
81+
if (!sleepingBody.isStatic && movingBody.motion > _motionWakeThreshold * timeFactor) {
7682
Sleeping.set(sleepingBody, false);
7783
}
7884
}

0 commit comments

Comments
 (0)