Game Development Reference
In-Depth Information
Chapter 8. Collision Filtering
Collision filtering is an act of informing the physics engine to ignore collisions between
specific types of objects. This could either be an optimization to minimize physics pro-
cessing activity, or an essential component of gameplay. Either way, the mechanism
to implement this in Bullet is the same, and we will explore how to implement such a
system in this chapter using groups and masks
Groups and masks
The code to implement collision filtering is absolutely trivial, but it requires a good
amount of explanation before it can be fully appreciated and understood.
Note
Continue from here using the Chapter8_CollisionFiltering project files.
When we call the addRigidBody() function on our world object, there is an over-
loaded version of the same function with two more parameters that we can input:
• A short representing the object's collision group
• A short representing the object's collision mask
Each object in our world can be a part of zero, one, or more collision groups. Groups
could represent concepts such as players, power-ups, projectiles, enemies, and so
on. Meanwhile, the collision mask indicates which groups this object should collide
with. In this way, we can use collision filtering to generate an essential element of
gameplay logic by preventing the player from being hit by their own weapon pro-
jectiles, or preventing enemies from being able to pick up power-ups.
Bullet treats the short values as a list of bit flags, and uses simple bitwise operations
to perform tests on them. If bitwise operators and bitmasks sound scary or confusing,
then this would be a good moment to break open your computer science manual
of choice and do a little brush up on the subject. They are a commonly used
tool in C++ programming. So common in fact, that we have already used them
Search WWH ::




Custom Search