Game Development Reference
In-Depth Information
Creating collision filters
In the
ball
object, there is a
filter
property inside the fixture definition that we use to
mask collisions. Meaning we determine what bodies can collide with each other. The cue
ball receives a different value for
categoryBits
than the other balls.
fixtureDef.filter.categoryBits = 0x0100;
When we create the cue body, we set a
maskBits
property in its fixture definition as fol-
lows:
fixtureDef.filter.maskBits = 0x0100;
We set this to the same value as the white ball's
categoryBits
.
The result of all this? Now the cue can only hit bodies with the same
categoryBits
,
which here means the cue can only collide with the white ball.
It is possible to add more than one category to a mask with a bitwise
|
option, as seen here:
fixtureDef.filter.maskBits = 0x0100 | 0x0010;
Or to collide with everything except the cue ball, for instance, as seen in the following line:
fixtureDef.filter.maskBits = 0xFFFF & ~0x0100;