That is not how collision handling works. When two bodies are in intersection, physics engine performs logical AND
operator between current's body collisionBitMask
and other's body categoryBitMask
:
When two physics bodies contact each other, a collision may occur.
This body’s collision mask is compared to the other body’s category
mask by performing a logical AND operation. If the result is a nonzero
value, this body is affected by the collision. Each body independently
chooses whether it wants to be affected by the other body. For
example, you might use this to avoid collision calculations that would
make negligible changes to a body’s velocity.
The source.
So the result depending on how you set categoryBitMask
on those two bodies. A default value for categoryBitMask
is 0xFFFFFFFF
, means all bits set. So when you perform & between 0xFFFFFFFF
and 0b10
or 0b01
, the result would be non-zero value, thus the collision.
So for example, setting your bodies like this:
spriteA.physicsBody?.categoryBitMask = 0b01
spriteA.physicsBody?.collisionBitMask = 0b01
and
spriteB.physicsBody?.categoryBitMask = 0b10
spriteB.physicsBody?.collisionBitMask = 0b10
will give you the result you want. Also, this is probably not the exact setup you need, it is just a simple example, and you will have to change values according to your needs. In this case, spriteA will collide only with bodies which have categoryBitMask
set to 0b01
. Same goes for spriteB, it will collide with bodies which have categoryBitMask
set to 0b10
.
Also, in the case if you don't want these sprites to be able to collide with anything, simply set their collisionBitMask
properties to 0.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…