Cocos2d-x physics body's parent node

  • barabasandras1
    Likes 0

    Problem Description

    I’ve got a special sprite class (let’s call it SpecialSprite class – SpecialSprite is inherited from cocos2d::Sprite and has a property for example: _spriteType, it doesn’t really matter what value it is going to be holding, the point is SpecialSprite is an own class I created with some added features compared to normal Sprite).

    I’m using chipmunk. When I add a new sprite (SpecialSprite!!!) and set new PhysicsBody on it (just like in your tut. video) everything is fine. I need to use a collision detection event to examine if a certain SpecialSprite object touches a certain part of the game layer (the bottom of the game area, but this isn’t really important now). I would use this code for it normally:

    bool Game::onContactBegin(PhysicsContact &contact)
    {
        PhysicsBody *a = contact.getShapeA()->getBody();
        PhysicsBody *b = contact.getShapeB()->getBody();

        if ((3 == a->getCollisionBitmask() && 1 == b->getCollisionBitmask()))
        {
            // here comes the tricky part soon
        }
    }

    Explanation: PhysicsBody a = the SpecialSprite object, PhysicsBody b = the game layer’s element I want to examine the collision on.

    If I put a CCLOG() in “ // here comes the tricky part soon” place everything is fine, it works like a charm. The problem is that I have to get the original SpecialSprite node from *a somehow. I tried with a->getNode(), which returns a Node*. Even if I static_cast something is bad. It’s not the SpecialSprite I REALLY put the PhysicsBody on. Whenever I would try for example: CCLOG(“%s”, static_cast(a->getNode()->getSpriteType())); it just returns NULL which means I don’t get the original SpecialSprite its just some kind of dummy or I don’t even know (getSpriteType() returns the SpecialSprite object’s _spriteType value which is initialized in the SpecialSprite init(), so it shouldn’t be NULL).

    Thanks in advance, I hope you understand it.


  • barabasandras1
    Likes 0

    bool Game::onContactBegin(PhysicsContact &contact)
    {
        PhysicsBody *a = contact.getShapeA()->getBody();
        PhysicsBody *b = contact.getShapeB()->getBody();


    Which body is “a” and which is “b”? How do I find that out?

  • barabasandras1
    Likes 0

    I figured out, please, delete this topic!

Login to reply