Game Development Reference
In-Depth Information
Managing the collision event
There are essentially two ways to handle collision events: either send an event every
update while two objects are touching (and continuously while they're still touching),
or send events both when the objects collide and when the objects separate.
In almost all cases it is wiser to pick the latter option, since it is simply an optimized
version of the first. If we know when the objects start and stop touching, then we
can assume that the objects are still touching between those two moments in time.
So long as the system also informs us of peculiar cases in separation (such as if
one object is destroyed, or teleports away while they're still touching), then we have
everything we need for a collision event system.
Bullet strives to be feature-rich, but also flexible, allowing us to build custom solutions
to problems such as this; so this feature is not built into Bullet by default. In other
words, we will need to build this logic ourselves. Our goals are simple; determine if a
pair of objects have either collided or separated during the step, and if so, broadcast
the corresponding event. The basic process is as follows:
1. For each manifold, check if the two objects are touching (the number of con-
tact points will be greater than zero).
2. If so, add the pair to a list of pairs that we found in this step.
3. If the same pair was not detected during the previous step, broadcast a colli-
sion event.
4. Once we've finished checking the manifolds, create another list of collision
objects that contains only the missing collision pairs between the previous
step and this step.
5. For each pair that is missing, broadcast a separation event.
6. Overwrite the list of collision pairs from the previous step, with the list we cre-
ated for this step.
There are several STL ( Standard Template Library ) objects and functions we can
use to make these steps easier. An std::pair can be used to store the objects in
pairs, and can be stored within an std::set . These sets let us perform rapid com-
parisons between two sets using a helpful function, std::set_difference() .
This function tells us the elements that are present in the first set, but not in the
Search WWH ::

Custom Search