0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-30 10:20:52 -05:00

constraints now account for timeScale

This commit is contained in:
liabru 2014-04-23 14:26:57 +01:00
parent 613e63699b
commit 537f0912c3
2 changed files with 8 additions and 6 deletions

View file

@ -67,10 +67,11 @@ var Constraint = {};
* Description * Description
* @method solveAll * @method solveAll
* @param {constraint[]} constraints * @param {constraint[]} constraints
* @param {number} timeScale
*/ */
Constraint.solveAll = function(constraints) { Constraint.solveAll = function(constraints, timeScale) {
for (var i = 0; i < constraints.length; i++) { for (var i = 0; i < constraints.length; i++) {
Constraint.solve(constraints[i]); Constraint.solve(constraints[i], timeScale);
} }
}; };
@ -78,8 +79,9 @@ var Constraint = {};
* Description * Description
* @method solve * @method solve
* @param {constraint} constraint * @param {constraint} constraint
* @param {number} timeScale
*/ */
Constraint.solve = function(constraint) { Constraint.solve = function(constraint, timeScale) {
var bodyA = constraint.bodyA, var bodyA = constraint.bodyA,
bodyB = constraint.bodyB, bodyB = constraint.bodyB,
pointA = constraint.pointA, pointA = constraint.pointA,
@ -116,10 +118,10 @@ var Constraint = {};
// solve distance constraint with Gauss-Siedel method // solve distance constraint with Gauss-Siedel method
var difference = (currentLength - constraint.length) / currentLength, var difference = (currentLength - constraint.length) / currentLength,
normal = Vector.div(delta, currentLength), normal = Vector.div(delta, currentLength),
force = Vector.mult(delta, difference * 0.5 * constraint.stiffness); force = Vector.mult(delta, difference * 0.5 * constraint.stiffness * timeScale * timeScale);
// if difference is very small, we can skip // if difference is very small, we can skip
if (Math.abs(1 - (currentLength / constraint.length)) < _minDifference) if (Math.abs(1 - (currentLength / constraint.length)) < _minDifference * timeScale)
return; return;
var velocityPointA, var velocityPointA,

View file

@ -255,7 +255,7 @@ var Engine = {};
// update all constraints // update all constraints
for (i = 0; i < engine.constraintIterations; i++) { for (i = 0; i < engine.constraintIterations; i++) {
Constraint.solveAll(allConstraints); Constraint.solveAll(allConstraints, engine.timeScale);
} }
Constraint.postSolveAll(allBodies); Constraint.postSolveAll(allBodies);