Pregunta

I only called the body->CreateFixture(&fixtureDef) once but the Testbed keeps on creating bodies. Here is my code:

#ifndef FOOTEST_H
#define FOOTEST_H
#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f    

class FooTest : public Test
    {
        public:
        FooTest() { } //do nothing, no scene yet

        void Step(Settings* settings)
        {
            //run the default physics and rendering
            Test::Step(settings);

        b2BodyDef myBodyDef;
        myBodyDef.type = b2_dynamicBody;
        myBodyDef.position.Set(0, 20);
        b2Body *dynamicBody = m_world->CreateBody(&myBodyDef);

        b2PolygonShape polygonShape;
        b2FixtureDef myFixtureDef;
        myFixtureDef.shape = &polygonShape;
        myFixtureDef.density = 1;

        for(int i=0;i<4;i++){
        b2Vec2 pos(sinf(i*90*DEGTORAD), cos(i*90*DEGTORAD));
        polygonShape.SetAsBox(1, 1, pos, 0);
        dynamicBody->CreateFixture(&myFixtureDef);
        }


        myBodyDef.type = b2_staticBody;
        myBodyDef.position.Set(0, 0);

        b2Body *staticBody = m_world->CreateBody(&myBodyDef);

        b2EdgeShape edgeShape;
        edgeShape.Set( b2Vec2(-15,0), b2Vec2(15,3) );

        myFixtureDef.shape = &edgeShape;
        staticBody->CreateFixture(&myFixtureDef);
        }

        static Test* Create()
        {
            return new FooTest;
        }
    };  
#endif

I was following this tutorial: iForce2D The tutorial's screenshots show just one instance of the created fixture. Am I missing something here?

¿Fue útil?

Solución

The "Testbed" calls Step repeatedly, and you're creating instances in each call.
Note that the tutorial does all setup in the FooTest constructor, so it's only run once.

You've moved the setup from the constructor to the Step function for some reason.
Just a bit earlier in that tutorial, it says "if you are not doing anything special for rendering or physics control, you don't need to override Step()".

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top