From 365152df6eecff9508104ec2dc2ded92efcf197c Mon Sep 17 00:00:00 2001 From: liabru Date: Mon, 31 Mar 2014 11:22:20 +0100 Subject: [PATCH] constraint early out for small differences --- src/constraint/Constraint.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/constraint/Constraint.js b/src/constraint/Constraint.js index 25c35a4..3ab7d9f 100644 --- a/src/constraint/Constraint.js +++ b/src/constraint/Constraint.js @@ -18,6 +18,7 @@ var Constraint = {}; (function() { var _minLength = 0.000001, + _minDifference = 0.001, _nextId = 0; /** @@ -116,7 +117,11 @@ var Constraint = {}; var difference = (currentLength - constraint.length) / currentLength, normal = Vector.div(delta, currentLength), force = Vector.mult(delta, difference * 0.5 * constraint.stiffness); - + + // if difference is very small, we can skip + if (Math.abs(1 - (currentLength / constraint.length)) < _minDifference) + return; + var velocityPointA, velocityPointB, offsetA, @@ -232,18 +237,19 @@ var Constraint = {}; var body = bodies[i], impulse = body.constraintImpulse; - if (impulse.x !== 0 || impulse.y !== 0 || impulse.angle !== 0) { - // update geometry - Vertices.translate(body.vertices, impulse); + // update geometry and reset + Vertices.translate(body.vertices, impulse); + + if (impulse.angle !== 0) { Vertices.rotate(body.vertices, impulse.angle, body.position); Axes.rotate(body.axes, impulse.angle); - Bounds.update(body.bounds, body.vertices); - - // reset body.constraintImpulse - impulse.x = 0; - impulse.y = 0; impulse.angle = 0; } + + Bounds.update(body.bounds, body.vertices); + + impulse.x = 0; + impulse.y = 0; } };