Skip to content

Commit

Permalink
fixed collision rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Feb 21, 2015
1 parent 84d9f59 commit bc9a051
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/render/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ var Render = {};
options = engine.render.options,
pair,
collision,
corrected,
i,
j;

Expand All @@ -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],
Expand All @@ -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) {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit bc9a051

Please sign in to comment.