If the categoryBitMask
for two bodies ANDed together is zero, they will not generate contact events. For instance this happens with playerCategory (1) and enemyCategory (4): 1 & 4 = 0
<== no contact events are generated, ever, between players and enemies. In that case it won't even get to check the contactBitMask
flags.
If you want two bodies to generate contact events, they both have to have the same category bitmask flag set.
The default for categoryBitMask
is 0xFFFFFFFF
so that all bodies can contact with each other. Same for collisionBitMask
because generally you want all bodies to contact and collide by default.
The only bit mask you really need to modify in most cases is the contactBitMask
, whose default is 0, meaning no contact events are generated by default (to improve performance).
Only change the other bitmasks where changing contactBitMask
alone does not suffice.
For example when you want contact events, but no collision feedback (= change in velocity/position of colliding bodies when they come in contact), then make sure that those body's collisionBitMask
ANDed together is 0. This allows bodies to generate contact events, but pass through each other. Useful to create triggers, ie when the player enters an area you can let him pass but also trigger a game event.
Or when you want to use the same contact bitmasks for two different categories of bodies who should only collide/contact with other bodies sharing the same category - ie regular and "ghost" characters who should have the same contact behavior with everything else in the game, except they shouldn't contact/collide with their corporeal (non-ghost) counterparts. In that case, use the same contactBitMask
and collisionBitMask
for both bodies. Then set all flags in categoryBitMask
for all other categories, but don't set the flags for the two categories representing the corporeal and ghost entities. Ie if corporeal category is 2 and ghost category is 8, the categoryBitMask should be: 0xFFFFFFFF - 2 - 8 = 0xFFFFFFF5
(lower 8 bits would be: 11110101)
Long story short: categoryBitMask
flags should be cleared from 0xFFFFFFFF
(rather than set to a specific bit) so that individual bodies in specific categories don't contact with each other but still contact with all other bodies.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…