Cocos2d-x PhysicsContact Object Reference

116 Views Asked by At

I'm trying to make a videogame with C++ in Cocos2d-x but I have an issue with PhysicContact.

In my gamescene I have a contact listener that check for collisions with the character and objects' PhysicBody of class Item. It all works fine, but now I want to recognize what object has collided because I want to call a method of the object's class called getThrow().

class Item : public cocos2d::Layer
{
public:
 Sprite* itemArt;
 int itemType;
 PhysicsBody* itemCollider;

 void createArt(int type);
 void getThrow();

 Item(int type);
};

I have tried it with the PhysicContact information, first I obtain the object PhysicBody and then its Node, but with this I just obtain the object's Sprite and at this point I don't know how to reach the object to call his method.

bool Level0::onContactBegin(PhysicsContact &contact)
{
 auto bodyA = contact.getShapeA()->getBody()->getNode();
 auto bodyB = contact.getShapeB()->getBody()->getNode();

    //HERE IS WHERE I WANT TO RUN bodyB->getThrow()

 return true;
}

I also have tried with getUserData() and getUserObject() but I don't know how to call a method with an object pointer.

I don't know if I'm trying something incorrectly, but I would appreciate your answers.

Thanks in advance! (If more details are needed just tell me, I'm new at this big community)

1

There are 1 best solutions below

0
On

You need to do this:

((Item*) b->getNode())->getThrow();