Cocos2dx physic body polygon error

  • Minh Kokox
    Likes 0

    Problem Description

    Hello,

    I had an error with creating polygon physic body for a sprite:

    {

    auto mSprite = Sprite::create("");
        std::array<Point, 6> mBodyPoint = {
            Point(x1, y1),
            Point(x2, y2),
            Point(x3, y3),
            Point(x4, y4), 
            Point(x5, y5),
            Point(x6, y6)
            }
        auto mBody = PhysicsBody::createPolygon(mBodyPoint.data(), mBodyPoint.size(), PhysicsMaterial(1, 0.8, 1));
        mBody->setDynamic(true);
        mSprite->setPhysicsBody(mBody);
        mSprite->setPosition(Vec2(xx1,yy2));
        addChild(mSprite);

    }

    It worked with ‘createEdgePolygon’.

    Thanks for your help!

  • Sonar Systems admin
    Likes 0

    What error did you receive.

  • Minh Kokox
    Likes 0

    Here:

    _________________________________________________________

    Debug error!

    Program: C:/.../.../...exe

    R6010

    – abort() has been called

    __________________________________________________________

    I used Visual Studio 2013 on win10pro. You should try this code on your computer to see the error!

  • Sonar Systems admin
    Likes 0

    Does a fresh project work for you.

  • Minh Kokox
    Likes 0

    I don’t understand! My project is failed now.

    This code makes error with ‘createPolygon’ but works with ‘createEdgePolygon’. I need ‘createPolygon’ for my sprite.

  • Sonar Systems admin
    Likes 0

    ???     

  • Minh Kokox
    Likes 0

    You haven’t understood me yet!

    Please try this code above, you will understand my problem!!!!


  • Sonar Systems admin
    Likes 0

    I am unable to test it ATM, could you show me a screenshot of the error.

  • Sonar Systems admin
    Likes 0

    What’s the Physics helper?

  • Minh Kokox
    Likes 0

    I don’t know!

  • Sonar Systems admin
    Likes 0

    Where did you get the code from?

  • Minh Kokox
    Likes 0

    In: [Cocos2dx file]\tests\cpp-tests\Classes\PhysicsTest\PhysicsTest.cpp

    …….

    void PhysicsDemoPump::onEnter()
    {

    …..

     Vec2 vec[4] =
        {
            VisibleRect::leftTop() + Vec2(102, -148),
            VisibleRect::leftTop() + Vec2(148, -161),
            VisibleRect::leftBottom() + Vec2(148, 20),
            VisibleRect::leftBottom() + Vec2(102, 20)
        };
       
        // pump
        auto pump = Node::create();
        auto center = PhysicsShape::getPolyonCenter(vec, 4);
        pump->setPosition(center);
        auto pumpB = PhysicsBody::createPolygon(vec, 4, PHYSICSBODY_MATERIAL_DEFAULT, -center);
        pump->setPhysicsBody(pumpB);
        this->addChild(pump);

    …..

    }

    …….

    I used ‘Point’ instead of ‘Vec2’ but they are the same.

  • Minh Kokox
    Likes 0

    I saw completely all of them. This tutorial has custom physic bodies video but ‘poligon body’ is better than ‘BodyParser physic body’.

    I can change its size by changing data in:

    std::array<Point, n> mBodyPoint {  …..   }  if it dose not make error.

    So I want to use it.

  • Sonar Systems admin
    Likes 0

    Did you follow a tutorial?

Login to reply