This project is read-only.

Collision not exactly right (Fixed)

Apr 12, 2010 at 10:22 PM
Edited Apr 13, 2010 at 4:54 PM

I've followed the "Hello Box2D" from the Box2D.org manual and applied it in XNA. Getting it done was quite easy but there seem to be some problems: after drawing everything, the dynamic box stops too early and kind of floats slightly above the ground. I'm not scaling anything yet. The following picture shows what it looks like once the dynamic box (the red pixel) comes to a stop on the floor (the white pixels):

http://dl.dropbox.com/u/33451/box2d.png

The box in my example is 1 by 1 meters and the floor 25 by 1 meters.

The drawing code for the box pixel:

 

spriteBatch.Draw(MainGame.Game.Pixel, Body.Position, Color.Red);

 

The floor is the same except drawn in a line, from its X position to X + 25 (Somehow the position seems not to be the center of mass).

I'm surely doing something wrong that's simply stupid, but I have no idea.

 

Edit: I've fixed this problem myself, I simply over-read the following note in the manual: "[...] The SetAsBox function takes the half-width and half-height (extents). So in this case the ground box is 100 units wide (x-axis) and 20 units tall (y-axis). [...]". I find the way Box2D handles that quite illogical, it should calculate its own half widths and half heights.
Apr 16, 2010 at 10:03 PM
Edited Apr 16, 2010 at 10:03 PM

That is a common problems I've done myself many times.  I agree that the Box2D parameters for SetAsBox are less than ideal, but we don't want to diverge from the C++ version so we just have to deal with it.  If you want to file a bug on the C++ box2d google code project then it may be fixed in the future.