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:
parent
50224c55dc
commit
e8dba617de
3 changed files with 13 additions and 6 deletions
|
@ -16,8 +16,9 @@ var Resolver = {};
|
|||
* Description
|
||||
* @method solvePosition
|
||||
* @param {pair[]} pairs
|
||||
* @param {number} timeScale
|
||||
*/
|
||||
Resolver.solvePosition = function(pairs) {
|
||||
Resolver.solvePosition = function(pairs, timeScale) {
|
||||
var i,
|
||||
pair,
|
||||
collision,
|
||||
|
@ -59,7 +60,7 @@ var Resolver = {};
|
|||
bodyA = collision.bodyA;
|
||||
bodyB = collision.bodyB;
|
||||
normal = collision.normal;
|
||||
positionImpulse = (pair.separation * _positionDampen) - pair.slop;
|
||||
positionImpulse = ((pair.separation * _positionDampen) - pair.slop) * timeScale;
|
||||
|
||||
if (bodyA.isStatic || bodyB.isStatic)
|
||||
positionImpulse *= 2;
|
||||
|
|
|
@ -91,7 +91,8 @@ var Engine = {};
|
|||
correction,
|
||||
counterTimestamp = 0,
|
||||
frameCounter = 0,
|
||||
deltaHistory = [];
|
||||
deltaHistory = [],
|
||||
timeScalePrev = 1;
|
||||
|
||||
(function render(timestamp){
|
||||
_requestAnimationFrame(render);
|
||||
|
@ -129,9 +130,14 @@ var Engine = {};
|
|||
delta = delta < engine.timing.deltaMin ? engine.timing.deltaMin : delta;
|
||||
delta = delta > engine.timing.deltaMax ? engine.timing.deltaMax : delta;
|
||||
|
||||
// verlet time correction
|
||||
// time correction for delta
|
||||
correction = delta / timing.delta;
|
||||
|
||||
// time correction for time scaling
|
||||
if (timeScalePrev !== 0)
|
||||
correction *= engine.timeScale / timeScalePrev;
|
||||
timeScalePrev = engine.timeScale;
|
||||
|
||||
// update engine timing object
|
||||
timing.timestamp = timestamp;
|
||||
timing.correction = correction;
|
||||
|
@ -296,7 +302,7 @@ var Engine = {};
|
|||
|
||||
// iteratively resolve position between collisions
|
||||
for (i = 0; i < engine.positionIterations; i++) {
|
||||
Resolver.solvePosition(pairs.list);
|
||||
Resolver.solvePosition(pairs.list, engine.timeScale * engine.timing.correction);
|
||||
}
|
||||
Resolver.postSolvePosition(allBodies);
|
||||
|
||||
|
|
|
@ -168,7 +168,7 @@ var Gui = {};
|
|||
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, 'positionIterations', 1, 10).step(1);
|
||||
physics.add(engine, 'enabled');
|
||||
|
|
Loading…
Reference in a new issue