From bc9a051e73b04d368ec2acf77f4caf675a677721 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 21 Feb 2015 22:19:12 +0000 Subject: [PATCH] fixed collision rendering --- src/render/Render.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/render/Render.js b/src/render/Render.js index 4c51258b..aa596048 100644 --- a/src/render/Render.js +++ b/src/render/Render.js @@ -779,6 +779,7 @@ var Render = {}; options = engine.render.options, pair, collision, + corrected, i, j; @@ -787,6 +788,10 @@ var Render = {}; // render collision positions for (i = 0; i < pairs.length; i++) { pair = pairs[i]; + + if (!pair.isActive) + continue; + collision = pair.collision; for (j = 0; j < pair.activeContacts.length; j++) { var contact = pair.activeContacts[j], @@ -802,11 +807,41 @@ 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 for (i = 0; i < pairs.length; i++) { pair = pairs[i]; + + if (!pair.isActive) + continue; + collision = pair.collision; if (pair.activeContacts.length > 0) { @@ -818,7 +853,12 @@ var Render = {}; normalPosY = (pair.activeContacts[0].vertex.y + pair.activeContacts[1].vertex.y) / 2; } - c.moveTo(normalPosX - collision.normal.x * 8, normalPosY - collision.normal.y * 8); + if (collision.bodyB === collision.supports[0].body) { + c.moveTo(normalPosX - collision.normal.x * 8, normalPosY - collision.normal.y * 8); + } else { + c.moveTo(normalPosX + collision.normal.x * 8, normalPosY + collision.normal.y * 8); + } + c.lineTo(normalPosX, normalPosY); } }