diff --git a/src/collision/Resolver.js b/src/collision/Resolver.js index 29601bf..f1c2dc1 100644 --- a/src/collision/Resolver.js +++ b/src/collision/Resolver.js @@ -24,13 +24,12 @@ var Resolver = {}; collision, bodyA, bodyB, - vertex, - vertexCorrected, normal, bodyBtoA, tempA = Vector._temp[0], tempB = Vector._temp[1], - tempC = Vector._temp[2]; + tempC = Vector._temp[2], + tempD = Vector._temp[3]; // find impulses required to resolve penetration for (i = 0; i < pairs.length; i++) { @@ -42,14 +41,12 @@ var Resolver = {}; collision = pair.collision; bodyA = collision.parentA; bodyB = collision.parentB; - vertex = collision.supports[0]; - vertexCorrected = collision.supportCorrected; normal = collision.normal; - // get current separation between body edges involved in collision - bodyBtoA = Vector.sub(Vector.add(bodyB.positionImpulse, vertex, tempA), - Vector.add(bodyA.positionImpulse, vertexCorrected, tempB), tempC); + bodyBtoA = Vector.sub(Vector.add(bodyB.positionImpulse, bodyB.position, tempA), + Vector.add(bodyA.positionImpulse, + Vector.sub(bodyB.position, collision.penetration, tempB), tempC), tempD); pair.separation = Vector.dot(normal, bodyBtoA); } diff --git a/src/collision/SAT.js b/src/collision/SAT.js index 314c7f6..951af0e 100644 --- a/src/collision/SAT.js +++ b/src/collision/SAT.js @@ -136,7 +136,6 @@ var SAT = {}; supports = [verticesB[0]]; collision.supports = supports; - collision.supportCorrected = Vector.sub(supports[0], collision.penetration); return collision; }; diff --git a/src/render/Render.js b/src/render/Render.js index 993f1ed..f625734 100644 --- a/src/render/Render.js +++ b/src/render/Render.js @@ -42,6 +42,7 @@ var Render = {}; showBounds: false, showVelocity: false, showCollisions: false, + showSeparations: false, showAxes: false, showPositions: false, showAngleIndicator: false, @@ -202,6 +203,9 @@ var Render = {}; if (options.showIds) Render.bodyIds(engine, bodies, context); + if (options.showSeparations) + Render.separations(engine, engine.pairs.list, context); + if (options.showCollisions) Render.collisions(engine, engine.pairs.list, context); @@ -782,6 +786,8 @@ var Render = {}; pair, collision, corrected, + bodyA, + bodyB, i, j; @@ -809,32 +815,6 @@ var Render = {}; } c.fill(); - c.beginPath(); - - // render corrected positions - for (i = 0; i < pairs.length; i++) { - pair = pairs[i]; - - if (!pair.isActive) - continue; - - collision = pair.collision; - corrected = collision.supportCorrected; - - if (collision.bodyB === collision.supports[0].body) { - c.rect(collision.supportCorrected.x - 1.5, collision.supportCorrected.y - 1.5, 3.5, 3.5); - } else { - c.rect(collision.supportCorrected.x - 1.5 + (2 * collision.penetration.x), collision.supportCorrected.y - 1.5 + (2 * collision.penetration.y), 3.5, 3.5); - } - } - - if (options.wireframes) { - c.strokeStyle = 'rgba(255,165,0,0.7)'; - } else { - c.strokeStyle = 'orange'; - } - c.stroke(); - c.beginPath(); // render collision normals @@ -875,6 +855,63 @@ var Render = {}; c.stroke(); }; + /** + * Description + * @private + * @method separations + * @param {engine} engine + * @param {pair[]} pairs + * @param {RenderingContext} context + */ + Render.separations = function(engine, pairs, context) { + var c = context, + options = engine.render.options, + pair, + collision, + corrected, + bodyA, + bodyB, + i, + j; + + c.beginPath(); + + // render separations + for (i = 0; i < pairs.length; i++) { + pair = pairs[i]; + + if (!pair.isActive) + continue; + + collision = pair.collision; + bodyA = collision.bodyA; + bodyB = collision.bodyB; + + var k = 1; + + if (!bodyB.isStatic && !bodyA.isStatic) k = 0.5; + if (bodyB.isStatic) k = 0; + + c.moveTo(bodyB.position.x, bodyB.position.y); + c.lineTo(bodyB.position.x - collision.penetration.x * k, bodyB.position.y - collision.penetration.y * k); + + k = 1; + + if (!bodyB.isStatic && !bodyA.isStatic) k = 0.5; + if (bodyA.isStatic) k = 0; + + c.moveTo(bodyA.position.x, bodyA.position.y); + c.lineTo(bodyA.position.x + collision.penetration.x * k, bodyA.position.y + collision.penetration.y * k); + } + + if (options.wireframes) { + c.strokeStyle = 'rgba(255,165,0,0.5)'; + } else { + c.strokeStyle = 'orange'; + } + c.stroke(); + }; + /** * Description * @private