Game Development Reference
In-Depth Information
Ignoring Collisions with Categories and Masks
Before a collision callback method is sent, the physics engine first evaluates a body's
Categories
and
Masks
properties. These are used to sort out collisions before the ac-
tual collision detection, resolve, and callback code is performed.
It is preferable to use collision
Categories
and
Masks
to filter out any noncolliding
bodies whenever you can because it's more efficient than returning
NO
from the
Begin
contact callback method.
The masks use the same identifiers as the categories. To you better understand this, it may
help to think of
Masks
as a short name for “collides with categories” in the rest of the text.
There can be up to 32 unique category identifiers, which are simply arbitrary strings that
you enter in SpriteBuilder. Both
Categories
and
Masks
fields can contain multiple
identifiers. An example of these settings is shown in
Figure 4-14
.
Figure 4-14
.
Example usage of collision categories and masks
Tip
While working on your game, you should make note of the category iden-
tifiers and their exact names in a list. This helps you avoid mistakes like incor-
rectly naming an identifier, and you can keep track of how many of the 32 iden-
tifiers you've already used. You should also stick to either plural or singular
names, so you never have to wonder whether the identifier was
enemy
or
en-
emies
. This makes it easier to recall them.
Categories
and
Masks
are both empty by default. The confusing bit here is that an
“empty” category means that this body is actually a member of all previously defined cat-
egories. Likewise, an “empty” mask means that the body is set up to collide with all previ-
ously defined categories. That means physics bodies default to colliding with all other
bodies unless their categories and masks are set to specific identifiers.
Search WWH ::
Custom Search