Simple newbie problem

I'm doing something obviously dumb, but I can't figure it out. I *almost* have a test example working but not quite. I've got an 800x600 window, where 0,0 is the top left and 799,599 is the bottom right of the screen. I'm trying to put a floor of size 800x20 at location 0, 580, and a player object (an ugly square) at 0, 200. I get the player to fall and eventually hit the floor.....but it's not stopping at the correct spot. Weirdly, I found that if I set the extents on the player to <anything>x10 (meaning a width of any value and a height of 20) it lands perfectly....but if I change the height at all it doesn't. Here's how I'm setting things up:

Code:
        b2AABB worldbb;
   worldbb.minVertex.Set(0, 0);
   worldbb.maxVertex.Set(800, 600);

   gWorld = new b2World(worldbb, b2Vec2(0, 100.0f), true);

   b2BoxDef groundBoxDef;
   groundBoxDef.extents.Set(800 / 2, 20 / 2);
   groundBoxDef.density = 0.0f;

   b2BodyDef groundBodyDef;
   groundBodyDef.position.Set(0, 580);
   groundBodyDef.AddShape(&groundBoxDef);
   gGround = gWorld->CreateBody(&groundBodyDef);

   b2BoxDef boxDef;
   boxDef.extents.Set(gPlayerWidth/2, gPlayerHeight/2);
   boxDef.density = 10.0f;
   boxDef.friction = 0.3f;
   b2BodyDef bodyDef;
   bodyDef.position.Set(0, 200);
   bodyDef.AddShape(&boxDef);
   gPlayer = gWorld->CreateBody(&bodyDef);
   gPlayer->m_invI = 0.0f; // don't rotate


I'm doing 10 iterations and have a time step of 1/60.0. My update loop only contains gWorld->Step(gTimeStep, gIterations). Perhaps my draw function is to blame? Here's how I draw things:

Code:
        b2Vec2 gpos = gGround->GetOriginPosition();
   b2Vec2 ppos = gPlayer->GetOriginPosition();

       // white color, draw at X, Y with width/height of 800, 20
   SetColor(Color::White);
   FillRect(gpos.x, gpos.y, 800, 20);

   SetColor(Color(255, 0, 0));
   FillRect(ppos.x, ppos.y, gPlayerWidth, gPlayerHeight);


Thanks a ton for the help, I really appreciate it.
Your test works fine for me. Maybe it's a matter of interpretation.

Box2D uses discrete collision detection, meaning objects teleport between steps. So it is possible the player is teleporting across the floor.

Or are you just concerned about some penetration? That is also an artifact of discrete collision detection. For your player you can increase b2_contactBaumgarte (max 1.0f) to resolve overlap faster. Right now that factor is global and large values reduce stability for stacking, but you could probably modify the code to special case your player body.

I plan to implement a continous collision detection algorithm at some point, but that will be a while.

For a height of 19, I get the following y-values:

...
548.92
553.33
557.78
562.25 // overshoot
561.49 // overshoot
560.73
560.51
560.51
...

So there are a couple frames of overshoot. With b2_contactBaumgarte = 0.9f, I get

...
548.92
553.33
557.78
562.25 // overshoot
560.51
560.51
...

This gives one less frame of overshoot. One frame of overshoot is the minimum.

Note that there is also a little bit of overlap to prevent jitter.

Box2D is tuned for meters-kilograms-seconds. Obviously, you are using a different unit system, so YMMV.
I've attached a screenshot of what I'm getting. It's overshooting far more than just a few pixels. I'm trying this particular example with a player size of 10x50: at rest, the Y coordinate is 545.01013. Since the height is 50 pixels, that would mean that the bottom of the player is at 595.01013 which is about 15 pixels past the floor, which is at a Y coordinate of 580. You mentioned that the system is set up to be in meters. Perhaps the problem is how I've set my system up? How would you suggest translating a screenspace to something compatible with the physics engine? As I mentioned, I'm using an 800x600 window where 0,0 is the top left coordinate, and an increasing Y coordinate puts you at the bottom of the screen, not top. Thanks for your help.
Without having your code it is hard to know what's wrong. It could be the dimensions/positions of the rendered objects are not lined up with the simulated objects.

Set a breakpoint in b2PolyContact.cpp on line 57 and examine the m_manifold struct. You should be getting a pointCount of 2 and the points should have a small separation value. Set doSleep on the world to false (latest SVN code). What are the separation values at steady state?
I'm getting a pointcount of 2. The separation for both points is -3.9168091. The code I posted is actually all that's in the test app, but I could post the VS2005 project file if you want it. Is there perhaps just a problem with how I'm interpreting the position? In my drawing method, I call GetOriginPosition on the player, which has a Y value of 545.0103. My understanding is that this Y value would be the top of the player, so if he's 50 pixels tall, he'd extend down to 595.0103.
Ahh, I think I have it: I think my drawing is off. Is the value from GetOriginPosition the center of the object? I switched my draw routine to:

Code:
b2Vec2 gpos = gGround->GetOriginPosition();
   b2Vec2 ppos = gPlayer->GetOriginPosition();

   SetColor(Color::White);
   FillRect(gpos.x, gpos.y - 10, mWidth, 20);   // I set the ground height at 20

   SetColor(Color(255, 0, 0));
   FillRect(ppos.x, ppos.y - gPlayerHeight/2, gPlayerWidth, gPlayerHeight);


The difference from my previous code was that for the rectangles, I drew them like so:

Code:
FillRect(gpos.x, gpos.y, mWidth, 20);
   FillRect(ppos.x, ppos.y, gPlayerWidth, gPlayerHeight);


While this works, it puzzles me a bit. Why does it seem like I have to offset the Y position by the half height but not the X position? The X coordinate is in the right place.
So the separation is -3.9 when first hit I'm guessing. It should eventually reduce to -b2_linearSlop = -0.01f.

Yeah, for a single box, the origin and the center of mass are usually going to be the same. You would see a difference if you set b2ShapeDef.localPosition to something nonzero.

I suggest setting a breakpoint in the stepping loop and look at the position of the body and it's shape in the debugger. It should be easy to sort out.
Alright, I've got it. If I set the ground and player position to be at the XY that I want and offset it by the half width/height, and in my draw code if I offset the position in the opposite direction by the same amount, everything seems to look and function as I'm expecting. It seems my problem was not understanding that my position values were based on the center. Thanks for helping, I appreciate it.

Original thread: https://box2d.org/forum/viewtopic.php?t=25