This project is read-only.

Box2d world creating

Topics: Physics, Platform UI, Sprites and SpriteBatch
Jan 4, 2014 at 6:00 PM

I have a problem with box2d assembly on cocos2d-xna project.
The thing is when I create a b2World, the m_flags = e_clearForces. And it is not reset anywhere after.
Therefore when I call b2World::Step(), I've got no state change!

Sorry if it is an old issue or a known behavior, but I didn't manage to find anything on this topic. Could you suggest how it should work?
Jan 5, 2014 at 7:07 AM
Hello, could you post some sample code of what you are doing? I don't understand what you are explaining.
Jan 5, 2014 at 11:20 AM
Edited Jan 5, 2014 at 11:21 AM
First I create the b2World, a ball in this world and schedule a timer.
b2Vec2 gravity = new b2Vec2(0.0f, -8.0f);
m_world = new b2World(gravity);
m_body = m_world.CreateBody(ball_body_def);
Then in the timer event I do the following:
void Tick(float dt)
    int velocityIterations = 8;
    int positionIterations = 1;

    // Instruct the world to perform a single step of simulation. It is
    // generally best to keep the time step and iterations fixed.
    m_world.Step(dt, velocityIterations, positionIterations);

    //Iterate over the bodies in the physics world
    for (var b = m_world.BodyList; b != null; b = b.Next)
        if (b.UserData != null)
            //Synchronize the AtlasSprites position and rotation with the corresponding body
            var myActor = ((CCNode)b.UserData);
            myActor.PositionX = b.Position.x * Constants.PTM_RATIO;
            myActor.PositionY = b.Position.y * Constants.PTM_RATIO;
            myActor.Rotation = -1 * CCMacros.CCRadiansToDegrees(b.Angle);
Here I've got no position change. It turned out that b2World::m_flags is initialized with e_clearForce. Therefore the movement that should happen was reset at the end of b2World.Step(...) function.
So I did the following during the initialization:
m_world.Flags = m_world.Flags &~ b2WorldFlags.e_clearForces;
I wonder if this is a known behavior, because I didn't find it in any tutorial or sample code and had to build debug versions of Cocos2d-xna, box2d and monogame.

Jan 6, 2014 at 3:04 AM
well, that is odd. I don't recall doing this in the box2d test cases we have in the framework. We try to copy the default behavior from the cocos2d-x 2.2 box2d ... this may well be a bug. We'll need another test case to verify if this is expected behavior or a bug.