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!
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);
精彩评论