Keep SKNodes from applying force to each other

2020-05-09 08:59发布

问题:

I have two SKNode objects. Their positions change when they collide.

How can I prevent that? At the same time, I still want to be able to respond to them contacting via - (void)didBeginContact;

I tried setting both their mass property to 0.0f but that didn't work.

回答1:

You can achieve that by setting category, collision and contact bit masks.

uint32_t bodyABitMask = 1<<0;
uint32_t bodyBBitMask = 1<<1;

//A mask that defines which categories this physics body belongs to.
[bodyA setCategoryBitMask:bodyABitMask];
[bodyB setCategoryBitMask:bodyBBitMask];

//A mask that defines which categories of physics bodies 
//can collide with this physics body.
[bodyA setCollisionBitMask:0];
[bodyB setCollisionBitMask:0];

//A mask that defines which categories of bodies cause 
//intersection notifications with this physics body.
[bodyA setContactTestBitMask:bodyBBitMask];
[bodyB setContactTestBitMask:bodyABitMask];

In above case bodyA and bodyB cannot collide, but you will receive didBeginContact once they are in contact.