Game Development Reference
In-Depth Information
CollisionPairs removedCollisionPairs;
// Use the STL set difference function to find collision pairs that
// existed during the previous tick but not any more
std::set_difference( bulletPhysics->m_previousTickCollisionPairs.begin(),
bulletPhysics->m_previousTickCollisionPairs.end(),
currentTickCollisionPairs.begin(), currentTickCollisionPairs.end(),
std::inserter( removedCollisionPairs, removedCollisionPairs.begin() ) );
for ( CollisionPairs::const_iterator it = removedCollisionPairs.begin(),
end = removedCollisionPairs.end(); it != end; ++it )
{
btRigidBody const * const body0 = it->first;
btRigidBody const * const body1 = it->second;
bulletPhysics->SendCollisionPairRemoveEvent( body0, body1 );
}
bulletPhysics->m_previousTickCollisionPairs = currentTickCollisionPairs;
}
This code does three things: First it collects all of the collision pairs from the physics
system. A collision pair is any two objects whose physics shapes overlap in the phys-
ics world. So a box sitting on the floor is a collision pair, just like an arrow passing
through a tent is a collision pair. The code finds all the pairs of objects that are
touching each other during this tick.
Next, it compares the collision pairs with the previous tick
s collision pairs. If there
are any new ones, then an event is sent indicating that the two objects came into
contact with one another. If there are any pairs that existed in the previous tick but
no longer exist, an event is sent to tell the game system that the objects separated
from each other. Both of these events are quite useful in a game.
The great thing about using an event system for handling collision and separation is
that the physics system doesn
'
t have to interpret the event and figure out what to do
with it. That should be up to the other game subsystems. The sound system, for
example, might listen for collisions and play sounds based on the force and type of
object. You might have a damage manager that controls things like hit point reduc-
tion or spawning a destruction event. Either way, the physics system doesn
'
'
t have to
know or care about all these other things in your game.
The final thing that this internal tick callback does is store the list of collision pairs.
This saves them for you so you can compare them during the next tick.
 
Search WWH ::




Custom Search