diff --git a/src/body/Body.js b/src/body/Body.js index 30921829..6d17d256 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -402,6 +402,9 @@ var Body = {}; Vertices.rotate(part.vertices, delta, body.position); Axes.rotate(part.axes, delta); Bounds.update(part.bounds, part.vertices, body.velocity); + if (i > 0) { + Vector.rotateAbout(part.position, delta, body.position, part.position); + } } }; @@ -528,16 +531,23 @@ var Body = {}; // transform the body geometry for (var i = 0; i < body.parts.length; i++) { var part = body.parts[i]; + Vertices.translate(part.vertices, body.velocity); - if (body.angularVelocity !== 0) { - Vertices.rotate(part.vertices, body.angularVelocity, body.position); - Axes.rotate(part.axes, body.angularVelocity); - } - Bounds.update(part.bounds, body.vertices, body.velocity); + if (i > 0) { part.position.x += body.velocity.x; part.position.y += body.velocity.y; } + + if (body.angularVelocity !== 0) { + Vertices.rotate(part.vertices, body.angularVelocity, body.position); + Axes.rotate(part.axes, body.angularVelocity); + if (i > 0) { + Vector.rotateAbout(part.position, body.angularVelocity, body.position, part.position); + } + } + + Bounds.update(part.bounds, part.vertices, body.velocity); } }; diff --git a/src/collision/Resolver.js b/src/collision/Resolver.js index beb0e73f..29601bf6 100644 --- a/src/collision/Resolver.js +++ b/src/collision/Resolver.js @@ -91,22 +91,18 @@ var Resolver = {}; var body = bodies[i]; if (body.positionImpulse.x !== 0 || body.positionImpulse.y !== 0) { - // move the body without changing velocity - body.position.x += body.positionImpulse.x; - body.position.y += body.positionImpulse.y; - body.positionPrev.x += body.positionImpulse.x; - body.positionPrev.y += body.positionImpulse.y; - // update body geometry for (var j = 0; j < body.parts.length; j++) { var part = body.parts[j]; Vertices.translate(part.vertices, body.positionImpulse); - Bounds.update(part.bounds, body.vertices, body.velocity); - if (j > 0) { - part.position.x += body.positionImpulse.x; - part.position.y += body.positionImpulse.y; - } + Bounds.update(part.bounds, part.vertices, body.velocity); + part.position.x += body.positionImpulse.x; + part.position.y += body.positionImpulse.y; } + + // move the body without changing velocity + body.positionPrev.x += body.positionImpulse.x; + body.positionPrev.y += body.positionImpulse.y; // dampen accumulator to warm the next step body.positionImpulse.x *= _positionWarming; diff --git a/src/constraint/Constraint.js b/src/constraint/Constraint.js index 4ca596d6..c5bfb233 100644 --- a/src/constraint/Constraint.js +++ b/src/constraint/Constraint.js @@ -251,20 +251,24 @@ var Constraint = {}; // update geometry and reset for (var j = 0; j < body.parts.length; j++) { var part = body.parts[j]; + Vertices.translate(part.vertices, impulse); + if (j > 0) { + part.position.x += impulse.x; + part.position.y += impulse.y; + } + if (impulse.angle !== 0) { Vertices.rotate(part.vertices, impulse.angle, body.position); Axes.rotate(part.axes, impulse.angle); + if (j > 0) { + Vector.rotateAbout(part.position, impulse.angle, body.position, part.position); + } impulse.angle = 0; } - Bounds.update(part.bounds, body.vertices); - - if (j > 0) { - part.position.x += impulse.x; - part.position.y += impulse.y; - } + Bounds.update(part.bounds, part.vertices); } impulse.x = 0; diff --git a/src/geometry/Vector.js b/src/geometry/Vector.js index d8b02ce9..3cf19392 100644 --- a/src/geometry/Vector.js +++ b/src/geometry/Vector.js @@ -77,14 +77,16 @@ var Vector = {}; * @param {vector} vector * @param {number} angle * @param {vector} point + * @param {vector} [output] * @return {vector} A new vector rotated about the point */ - Vector.rotateAbout = function(vector, angle, point) { + Vector.rotateAbout = function(vector, angle, point, output) { var cos = Math.cos(angle), sin = Math.sin(angle); - return { - x: point.x + ((vector.x - point.x) * cos - (vector.y - point.y) * sin), - y: point.y + ((vector.x - point.x) * sin + (vector.y - point.y) * cos) - }; + if (!output) output = {}; + var x = point.x + ((vector.x - point.x) * cos - (vector.y - point.y) * sin); + output.y = point.y + ((vector.x - point.x) * sin + (vector.y - point.y) * cos); + output.x = x; + return output; }; /**