Adeel Ishaq
Adeel Ishaq

Reputation: 101

Collision detection between specific bodies in Cocos2D-x and Box2d

i want to know how to configure the b2contactlistener in right way. i learned many tutorials , but they not helped me very well. At last i detected the collision generally. but not for specific bodies. so kindly guide me how detect collision for specific bodies.while i am using cocos2d-x 2.1.5 and box2d. here is code which i used.

class MyListener : public b2ContactListener
{
   void BeginContact(b2Contact* contact)
{

    b2Fixture* fixtureA = contact->GetFixtureA();
    b2Fixture* fixtureB = contact->GetFixtureB();
    b2Body* body1 = fixtureA->GetBody();
    b2Body* body2 = fixtureB->GetBody();
    cout << "started";


   }
void MyListener::EndContact(b2Contact* contact)
{
    cout << "ended\n";
}


};

Upvotes: 1

Views: 829

Answers (1)

Nadarian
Nadarian

Reputation: 1002

You should check out b2body->setUserData(). You can store there anything you want to determine which body is which object, for example if you have many objects of the same class, say ball and an object of needle then you could store in the userdata of ball object a pointer to the actual ball sprite which has a Tag = 1 and in the needle obj a sprite with tag = 2. Then in your method perform a check if((body1->getUserData())->getTag() == 1) ... and you know everything

Upvotes: 1

Related Questions