Physics Body Help

  • warning717
    Likes 0

    Problem Description

    How do I get my physics body to float?

    How do I change the shape/size of it?

    How do I make it draggable?

     

  • Sonar Systems admin
    Likes 0

    Float as in water floating?

    Just assign another physics body to it to change the shape.

    For dragging something like this would be a great starting point.

    http://www.cocos2d-x.org/wiki/User_Tutorial-Accurate_Hit_Testing

  • warning717
    Likes 0

    I was thinking as floating in the air. Such as changing Y axis by 2 every second

  • Sonar Systems admin
    Likes 0

    You could run a move action every 2 second thats moves it up and down.

  • warning717
    Likes 0

    Ok so ive applied force to the object and that works just fine. Now im having an issue with importing a Physics Body from the editor. Using the tutorial,

    https://www.youtube.com/watch?v=NH95JTCCQvo

    I am getting undefined references from MyBodyParser::bodyFormJson, MyBodyParser::getInstance() twice and MyBodyParser::parseJsonFile

        Node *sprite = rootNode->getChildByTag( 15 );
    
        MyBodyParser::getInstance()->parseJsonFile( "sprite.json" );
    
        auto spriteBody = MyBodyParser::getInstance()->bodyFormJson(sprite, "sprite", PhysicsMaterial(1, 1, 0));
        if ( spriteBody != nullptr )
        {
            spriteBody->setDynamic( true );
            sprite->setPhysicsBody( spriteBody );
        }
        
        spriteBody->applyImpulse(Vect(500, 50));
        this->addChild( sprite );

     

  • Sonar Systems admin
    Likes 0

    Could you post a screenshot of the error.

  • warning717
    Likes 0

  • Sonar Systems admin
    Likes 0

    Have you downloaded and included MyBodyParser

  • warning717
    Likes 0

    Yeah i got it from the video (github) and just placed it into the classes folder

  • Sonar Systems admin
    Likes 0

    Have you included the MyBodyParser header?

  • warning717
    Likes 0

    Yes, both files, MyBodyParser.cpp and MyBodyParser.h

  • Sonar Systems admin
    Likes 0

    But have you included it in code.

  • warning717
    Likes 0

    I put #include “MyBodyParser.h” in if thats what you mean?

  • Sonar Systems admin
    Likes 0

    Yh, have you tried downloading the project from git hub and seeing if that works 

  • warning717
    Likes 0

    Same issue, also it appears that some of the code from the video differs from that from Github. There is also a json folder which I have also tried importing with no luck :(

  • warning717
    Likes 0

    It Works!

    Strange, in the past ive added MyBodyParser.cpp to Android.mk but the problem persisted. I have just realized that that was due to the fact that it wasn’t saving(inside of the physics body editor) as a json but instead as some random file. I was able to save it as a json now and re-added it to Android.mk and it worked! smiley

    For some reason the physics body is larger than the image and setScale doesnt work with physics body. What is the proper way of scaling the physics body to the smaller size of the sprite?

  • Sonar Systems admin
    Likes 0

    You can't scale the physics body but you need to do it in Physics Body Editor 

  • warning717
    Likes 0

    When in Physics Body Editor the physics body is the same size as the sprite used. Once imported into cocos2dx the physics body is 3 times larger than the sprite. I made sure to use the same sprite.

  • Sonar Systems admin
    Likes 0

    Have you scaled the sprite at all?

  • warning717
    Likes 0

    Yeah so how do I scale the physics body within the physics body editor?

  • Sonar Systems admin
    Likes 0

    You don’t scale it but create it to fit.

    Send us a screenshot of Physics Body Editor with the body created around your sprite.

  • warning717
    Likes 1

    I got it! I just kept resizing it in Photoshop, bringing it to the physics editor and exporting until it was perfect.

  • warning717
    Likes 0

    Now how do you make them draggable?

  • Sonar Systems admin
    Likes 0

    Check this tutorial out for detecting touch, then on move move your sprite accordingly

     

    http://www.cocos2d-x.org/wiki/User_Tutorial-Accurate_Hit_Testing

  • warning717
    Likes 0

    Thanks!

Login to reply