This project is read-only.

Body position incorrectly updated after World.Step() RESOLVED

Aug 2, 2010 at 2:54 AM

Hi all,

I'm getting close to wrapping up Box2D integration in my current game but am running into a nagging issue that I just can't resolve on my own. I built a simple app to test everything out and all was well, but when I ported it over this little bugger showed up.

The problem is that after World.Step(), the position of my player body is altered inside a zero gravity world with absolutely no forces being applied. The end result is that the player body drifts toward the top-left corner of the screen.

After initialization (and prior to the world updating) everything aligns perfectly. Here's a look at how I'm setting things up:

 

public void initializeWorld()
{    
    world = new World(Vector2.Zero, true);
}

public void initializeBody()
{    
    BodyDef bodyDef = new BodyDef();
    bodyDef.position = Vector2.Zero;
    bodyDef.type = BodyType.Dynamic;

    PolygonShape polygon = new PolygonShape();    
    polygon.SetAsBox(0.5f, 0.5f);

    FixtureDef fixtureDef = new FixtureDef();    
    fixtureDef.shape = polygon;    
    fixtureDef.restitution = 0.0f;

    body = gameScreen.World.CreateBody(bodyDef);          
    body.AllowSleeping(false);    
    body.CreateFixture(fixtureDef);    
    body.SetLinearDamping(15);    
    body.SetAngularDamping(15);     
    body.SetUserData(this);
}

public override void Update(GameTime gameTime, ...and other unrelated params)
{    
    world.Step((float)gameTime.ElapsedGameTime.TotalSeconds, 8, 3);
}

 

The only way I can improve the alignment after the world updates is by using a smaller value for the position iterations parameter. Even then, it's no where near being aligned correctly.

Finally, here's what it all looks like before and after World.Step() is called.

before World.Step()

after World.Step()

Should I just being tweaking an offset of the body until it's aligned as intended? For what it's worth, the amount of misalignment in the second photo is {X:0.3733204 Y:0.05612019}.

If anyone can shed some light on this, I'd greatly appreciate it. Thanks.

Aug 3, 2010 at 2:43 AM

UPDATE: 

I've noticed that the player's body's sweep.c member is the value being changed. (May be obvious to some but it wasn't to me.)

Also, I have many static bodies that move around the screen via SetTransform calls. When I remove the creation of these bodies, my player body acts as it should.

I'm still baffled.

Aug 3, 2010 at 12:45 PM
What were you trying to accomplish with SetTransform? I think Box2D makes some assumptions about static bodies that break if you move them.
Aug 3, 2010 at 2:18 PM
The static bodies represent tiles in my scrolling engine. As the user moves along the map, these tiles need to shift. I hadn't considered that calling SetTransform would break the physics system but if what you say is true, this must be the issue. I was apprehensive about making these tile bodies dynamic due how many of them they are. Although, I guess could probably just destroy or spawn them as their sprites are being culled or drawn. Thanks for your input. I appreciate it.
Aug 4, 2010 at 1:59 AM

RESOLVED:

The order by which I was moving game objects, moving the camera and updating the World was incorrect.

World.Step() is now the last thing that occurs in my game loop and all is well.