开发者

Determine if a body has collided - Cocos2d/Box2D

开发者 https://www.devze.com 2023-04-05 17:28 出处:网络
Is it possible to determine if a b2body has collided with another b2body from a different layer? Also, how would I do that?

Is it possible to determine if a b2body has collided with another b2body from a different layer? Also, how would I do that?

E.G.

I have a ball on my main game scene layer that is fired at开发者_运维知识库 a bomb in my level one layer. They collide and the bomb disappears.

Please let me know if I need to be more clear

Thanks!


If you want bodies to collide they must belong to the same b2World. On what layer they are drawn and how does not matter to the physics. To determine when collision happens subclass b2ContactListener and implement callback functions:

class MyContactListener : public b2ContactListener
{
public:
MyContactListener() : b2ContactListener() {}

void    BeginContact (b2Contact *contact);
void    EndContact (b2Contact *contact);
void    PreSolve (b2Contact *contact, const b2Manifold *oldManifold);
void    PostSolve (b2Contact *contact, const b2ContactImpulse *impulse);

};

then add the object of this class to your b2World:

MyContactListener *listener = new MyContactListener();
world->SetContactListener(listener);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号