0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

fixed some issues with engine.timeScale

This commit is contained in:
liabru 2014-04-24 16:23:23 +01:00
parent 50224c55dc
commit e8dba617de
3 changed files with 13 additions and 6 deletions

View file

@ -16,8 +16,9 @@ var Resolver = {};
* Description * Description
* @method solvePosition * @method solvePosition
* @param {pair[]} pairs * @param {pair[]} pairs
* @param {number} timeScale
*/ */
Resolver.solvePosition = function(pairs) { Resolver.solvePosition = function(pairs, timeScale) {
var i, var i,
pair, pair,
collision, collision,
@ -59,7 +60,7 @@ var Resolver = {};
bodyA = collision.bodyA; bodyA = collision.bodyA;
bodyB = collision.bodyB; bodyB = collision.bodyB;
normal = collision.normal; normal = collision.normal;
positionImpulse = (pair.separation * _positionDampen) - pair.slop; positionImpulse = ((pair.separation * _positionDampen) - pair.slop) * timeScale;
if (bodyA.isStatic || bodyB.isStatic) if (bodyA.isStatic || bodyB.isStatic)
positionImpulse *= 2; positionImpulse *= 2;

View file

@ -91,7 +91,8 @@ var Engine = {};
correction, correction,
counterTimestamp = 0, counterTimestamp = 0,
frameCounter = 0, frameCounter = 0,
deltaHistory = []; deltaHistory = [],
timeScalePrev = 1;
(function render(timestamp){ (function render(timestamp){
_requestAnimationFrame(render); _requestAnimationFrame(render);
@ -129,9 +130,14 @@ var Engine = {};
delta = delta < engine.timing.deltaMin ? engine.timing.deltaMin : delta; delta = delta < engine.timing.deltaMin ? engine.timing.deltaMin : delta;
delta = delta > engine.timing.deltaMax ? engine.timing.deltaMax : delta; delta = delta > engine.timing.deltaMax ? engine.timing.deltaMax : delta;
// verlet time correction // time correction for delta
correction = delta / timing.delta; correction = delta / timing.delta;
// time correction for time scaling
if (timeScalePrev !== 0)
correction *= engine.timeScale / timeScalePrev;
timeScalePrev = engine.timeScale;
// update engine timing object // update engine timing object
timing.timestamp = timestamp; timing.timestamp = timestamp;
timing.correction = correction; timing.correction = correction;
@ -296,7 +302,7 @@ var Engine = {};
// iteratively resolve position between collisions // iteratively resolve position between collisions
for (i = 0; i < engine.positionIterations; i++) { for (i = 0; i < engine.positionIterations; i++) {
Resolver.solvePosition(pairs.list); Resolver.solvePosition(pairs.list, engine.timeScale * engine.timing.correction);
} }
Resolver.postSolvePosition(allBodies); Resolver.postSolvePosition(allBodies);

View file

@ -168,7 +168,7 @@ var Gui = {};
Composite.setModified(engine.world, true, false, false); Composite.setModified(engine.world, true, false, false);
}); });
physics.add(engine, 'timeScale', 0.1, 2).step(0.1); physics.add(engine, 'timeScale', 0, 1.2).step(0.05);
physics.add(engine, 'velocityIterations', 1, 10).step(1); physics.add(engine, 'velocityIterations', 1, 10).step(1);
physics.add(engine, 'positionIterations', 1, 10).step(1); physics.add(engine, 'positionIterations', 1, 10).step(1);
physics.add(engine, 'enabled'); physics.add(engine, 'enabled');