Keep SKNodes from applying force to each other

我的梦境 提交于 2019-12-20 07:37:03

问题


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.



来源:https://stackoverflow.com/questions/21800857/keep-sknodes-from-applying-force-to-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!