Jonathan
Jonathan

Reputation: 2383

Determine if a body has collided - Cocos2d/Box2D

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!

Upvotes: 0

Views: 559

Answers (1)

Andrew
Andrew

Reputation: 24846

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);

Upvotes: 1

Related Questions