Game Development Reference
In-Depth Information
v2 = body2->vVelocityBody +
(body2->vAngularVelocity ^
body2->vCollisionPoint);
v1 = VRotate2D(body1->fOrientation, v1);
v2 = VRotate2D(body2->fOrientation, v2);
vRelativeVelocity = (v1 - v2);
Vrn = vRelativeVelocity * vCollisionNormal;
In determining whether or not the vertex under consideration is in fact colliding with
an edge, you have to check to see if the distance from the vertex is within your collision
tolerance, and you also have to make sure the vertex actually projects onto the edge (that
is, it does not project beyond the endpoints of the edge). Additionally, you need to make
sure the relative normal velocity indicates that the points of contact are moving toward
each other. Here's how this check looks:
if( (proj.Magnitude() > 0.0f) &&
(proj.Magnitude() <= edge.Magnitude()) &&
(dist <= ctol) &&
(Vrn < 0.0) )
haveNodeEdge = true;
After CheckForCollision checks for vertex-vertex and vertex-edge collisions, it goes
on to check for penetration:
if(!haveNodeNode && !haveNodeEdge)
{
for(i=0; i<4 && !interpenetrating; i++)
{
for(j=0; j<4 && !interpenetrating; j++)
{
if(j==3)
edge = vList2[0] - vList2[j];
else
edge = vList2[j+1] - vList2[j];
p = vList1[i] - vList2[j];
dot = p * edge;
if(dot < 0)
{
interpenetrating = true;
}
}
}
}
This check is a standard point-in-polygon check using the vector dot product to deter‐
mine if any vertex of one polygon lies within the bounds of the other polygon. After
this check, the function simply returns the appropriate result. Here again, 0 indicates
no collision or penetration, 1 indicates a collision, and −1 indicates penetration.