Skip to content

Commit

Permalink
removed collision.supportCorrected instead using bodyB.position, adde…
Browse files Browse the repository at this point in the history
…d render.options.showSeparations
  • Loading branch information
liabru committed Mar 4, 2015
1 parent 9d3755a commit 2b6a8d3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
13 changes: 5 additions & 8 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion src/collision/SAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ var SAT = {};
supports = [verticesB[0]];

collision.supports = supports;
collision.supportCorrected = Vector.sub(supports[0], collision.penetration);

return collision;
};
Expand Down
89 changes: 63 additions & 26 deletions src/render/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var Render = {};
showBounds: false,
showVelocity: false,
showCollisions: false,
showSeparations: false,
showAxes: false,
showPositions: false,
showAngleIndicator: false,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -782,6 +786,8 @@ var Render = {};
pair,
collision,
corrected,
bodyA,
bodyB,
i,
j;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b6a8d3

Please sign in to comment.