Game Development Reference
In-Depth Information
With this simple checking we can
contain our character within an area
and test for more specific collisions
within that zone. If you set up each
collision box to have a specific numer-
ical value, then your code can easily
determine how to react. We add a box
that the character can jump onto and
give this a collision box that returns, for
example, 12. Then in code we can tell
that the character has bumped into the
box if a 12 is returned from a collision
testing routine. But which side was hit?
You may want your character to react
differently if the box is hit from the side,
the top or the bottom. A side hit could
result in a character falling over if the speed of the hit reaches a certain
value or the character may simply have to enter a stationary animation
cycle. A hit on the top may result in a bounce, again depending on the hit
vector. To decide, we can use a vector from the character centre to the
collision box centre. The component with the greatest absolute magnitude
will give the collision direction that is the most marked visually. When an
intersection of two bounding boxes occurs, one vertex must be completely
inside the other bounding box; hence, all the axes are affected, but if we
want to get the general feel of the collision the absolute biggest
component of the collision vector gives a good starting point.
Figure 13.2 Simple axis-aligned
collision detection.
VECTOR bbcen, bbtestcen, a, tmp;
//Find the bounding box centres
bbcen.x = (bbmax.x - bbmin.x)/2 + bbmin.x;
bbcen.y = (bbmax.y - bbmin.y)/2 + bbmin.y;
bbcen.z = (bbmax.z - bbmin.z)/2 + bbmin.z;
bbtestcen.x = (bbtestmax.x - bbtestmin.x)/2 + bbtestmin.x;
bbtestcen.y = (bbtestmax.y - bbtestmin.y)/2 + bbtestmin.y;
bbtestcen.z = (bbtestmax.z - bbtestmin.z)/2 + bbtestmin.z;
//Calculate vector from testbb to bb
a.x = bbtestcen.x - bbcen.x;
a.y = bbtestcen.y - bbcen.y;
a.z = bbtestcen.z - bbcen.z;