Collision detection can very easily become a severe drain on the computer's resources. The best way to prevent this is by limiting what objects are checked for collision with which other objects. One way to do this is by controlling which segments of the scene graph collision checks are performed on, but this can be impractical because there are so many other things that scene graph organization can be used to accomplish.
Fortunately, we have another solution to this problem. We can use BitMasks
to limit which CollisionNodes
can interact. A BitMask
is a series of 32 bits that can be either 0 or 1. Here is an example of a BitMask:
0000 0000 0000 0000 0000 0010 0000 1000
The BitMask
works by limiting only those CollisionNodes
that have one of the same bits set to 1 to interact. That means if you set bit number 4 to 1 on two CollisionNodes
, they'll be able to collide. If that bit is set to 1 for one of them and 0 for the other, they won't be checked for collision. To clarify, if the two BitMasks
have any bits matching bits set to 1 then the CollisionNodes
will be checked for collision. If the BitMasks
don't share any bits set to 1 then they won't be checked for collision. Here's an example of two BitMasks
that will allow collision:
0000 0000 0000 0000 0000 0011 0000 1000 0000 0000 0000 0000 0000 0000 0000 1000
The following two BitMasks
won't allow collision:
0000 0000 0000 0000 0000 0000 0000 0100 0000 0000 0000 0000 0000 0000 0000 1010
Although it isn't terribly important for our purposes, we should mention that BitMasks
are numbered from right-to-left, and the numbering starts with 0. That means that if you set bit 0 to 1, the right-most bit will be 1.
In Panda3D, we have the option of setting both the From and Into
BitMasks
on each collision node. This means we can have CollisionNodes
that can cause collisions on one set of bits, and be collided with on a different set of bits. This can be particularly useful with things like explosions. Right now, if our cycle detects a collision with something, it won't be able to move through that object. If we use different BitMasks
, we can allow explosions to collide with the cycle without letting the cycle collide with the explosions. That may seem like a small semantic difference, but it means the explosion can detect the cycle without the cycle crashing into the explosion as if it were a brick wall.