mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-30 10:20:52 -05:00
added time scaling to Sleeping
This commit is contained in:
parent
e22ceebed5
commit
0ae2d02812
2 changed files with 14 additions and 8 deletions
|
@ -200,7 +200,7 @@ var Engine = {};
|
||||||
|
|
||||||
// if sleeping enabled, call the sleeping controller
|
// if sleeping enabled, call the sleeping controller
|
||||||
if (engine.enableSleeping)
|
if (engine.enableSleeping)
|
||||||
Sleeping.update(allBodies);
|
Sleeping.update(allBodies, timing.timeScale);
|
||||||
|
|
||||||
// applies gravity to all bodies
|
// applies gravity to all bodies
|
||||||
Body.applyGravityAll(allBodies, world.gravity);
|
Body.applyGravityAll(allBodies, world.gravity);
|
||||||
|
@ -241,7 +241,7 @@ var Engine = {};
|
||||||
|
|
||||||
// wake up bodies involved in collisions
|
// wake up bodies involved in collisions
|
||||||
if (engine.enableSleeping)
|
if (engine.enableSleeping)
|
||||||
Sleeping.afterCollisions(pairs.list);
|
Sleeping.afterCollisions(pairs.list, timing.timeScale);
|
||||||
|
|
||||||
// iteratively resolve velocity between collisions
|
// iteratively resolve velocity between collisions
|
||||||
Resolver.preSolveVelocity(pairs.list);
|
Resolver.preSolveVelocity(pairs.list);
|
||||||
|
|
|
@ -13,11 +13,14 @@ var Sleeping = {};
|
||||||
_minBias = 0.9;
|
_minBias = 0.9;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description
|
* Puts bodies to sleep or wakes them up depending on their motion.
|
||||||
* @method update
|
* @method update
|
||||||
* @param {body[]} bodies
|
* @param {body[]} bodies
|
||||||
|
* @param {number} timeScale
|
||||||
*/
|
*/
|
||||||
Sleeping.update = function(bodies) {
|
Sleeping.update = function(bodies, timeScale) {
|
||||||
|
var timeFactor = timeScale * timeScale * timeScale;
|
||||||
|
|
||||||
// update bodies sleeping status
|
// update bodies sleeping status
|
||||||
for (var i = 0; i < bodies.length; i++) {
|
for (var i = 0; i < bodies.length; i++) {
|
||||||
var body = bodies[i],
|
var body = bodies[i],
|
||||||
|
@ -35,7 +38,7 @@ var Sleeping = {};
|
||||||
// biased average motion estimation between frames
|
// biased average motion estimation between frames
|
||||||
body.motion = _minBias * minMotion + (1 - _minBias) * maxMotion;
|
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;
|
body.sleepCounter += 1;
|
||||||
|
|
||||||
if (body.sleepCounter >= body.sleepThreshold)
|
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
|
* @method afterCollisions
|
||||||
* @param {pair[]} pairs
|
* @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
|
// wake up bodies involved in collisions
|
||||||
for (var i = 0; i < pairs.length; i++) {
|
for (var i = 0; i < pairs.length; i++) {
|
||||||
var pair = pairs[i];
|
var pair = pairs[i];
|
||||||
|
@ -72,7 +78,7 @@ var Sleeping = {};
|
||||||
var sleepingBody = (bodyA.isSleeping && !bodyA.isStatic) ? bodyA : bodyB,
|
var sleepingBody = (bodyA.isSleeping && !bodyA.isStatic) ? bodyA : bodyB,
|
||||||
movingBody = sleepingBody === bodyA ? bodyB : bodyA;
|
movingBody = sleepingBody === bodyA ? bodyB : bodyA;
|
||||||
|
|
||||||
if (!sleepingBody.isStatic && movingBody.motion > _motionWakeThreshold) {
|
if (!sleepingBody.isStatic && movingBody.motion > _motionWakeThreshold * timeFactor) {
|
||||||
Sleeping.set(sleepingBody, false);
|
Sleeping.set(sleepingBody, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue