mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
implemented constraint warming
This commit is contained in:
parent
26a60e4dcf
commit
daf26af006
2 changed files with 26 additions and 3 deletions
|
@ -72,6 +72,27 @@ var Common = require('../core/Common');
|
||||||
return constraint;
|
return constraint;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares for solving by constraint warming.
|
||||||
|
* @private
|
||||||
|
* @method preSolveAll
|
||||||
|
* @param {body[]} bodies
|
||||||
|
*/
|
||||||
|
Constraint.preSolveAll = function(bodies) {
|
||||||
|
for (var i = 0; i < bodies.length; i += 1) {
|
||||||
|
var body = bodies[i],
|
||||||
|
impulse = body.constraintImpulse;
|
||||||
|
|
||||||
|
if (body.isStatic || (impulse.x === 0 && impulse.y === 0 && impulse.angle === 0)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.position.x += impulse.x;
|
||||||
|
body.position.y += impulse.y;
|
||||||
|
body.angle += impulse.angle;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Solves all constraints in a list of collisions.
|
* Solves all constraints in a list of collisions.
|
||||||
* @private
|
* @private
|
||||||
|
@ -247,9 +268,10 @@ var Common = require('../core/Common');
|
||||||
Bounds.update(part.bounds, part.vertices, body.velocity);
|
Bounds.update(part.bounds, part.vertices, body.velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
impulse.angle = 0;
|
// dampen the cached impulse for warming next step
|
||||||
impulse.x = 0;
|
impulse.angle *= Constraint._warming;
|
||||||
impulse.y = 0;
|
impulse.x *= Constraint._warming;
|
||||||
|
impulse.y *= Constraint._warming;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -150,6 +150,7 @@ var Body = require('../body/Body');
|
||||||
_bodiesUpdate(allBodies, delta, timing.timeScale, correction, world.bounds);
|
_bodiesUpdate(allBodies, delta, timing.timeScale, correction, world.bounds);
|
||||||
|
|
||||||
// update all constraints
|
// update all constraints
|
||||||
|
Constraint.preSolveAll(allBodies);
|
||||||
for (i = 0; i < engine.constraintIterations; i++) {
|
for (i = 0; i < engine.constraintIterations; i++) {
|
||||||
Constraint.solveAll(allConstraints, timing.timeScale);
|
Constraint.solveAll(allConstraints, timing.timeScale);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue