objects crossing through one another (even in hello world)

Hi everyone,
Im having a strange problems with objects (circles and boxes) crossing through the bottom part of the ground box and going out of screen. The gravity is set to (0,20) (note the difference in the coordinate system from opengl, but other than that everything is pretty much the same as the hello world program).
the small box does slow at the bottom of the larger one but then just keeps on going instead of resting on top of it. I am banging my hand on this for a few hours now...
here the code:
Code:
b2AABB worldAABB;
   worldAABB.minVertex.Set(-1000,-1000);
   worldAABB.maxVertex.Set(1000.0f, 1000.0f);

   // Define the gravity vector.
   b2Vec2 gravity(0.0f, 20.0f);

   // Do we want to let bodies sleep?
   bool doSleep = true;

   // Construct a world object, which will hold and simulate the rigid bodies.
   world = new b2World(worldAABB, gravity, doSleep);

   // Define the ground box shape.
   b2BoxDef groundBoxDef;

   // The extents are the half-widths of the box.
   groundBoxDef.extents.Set(100.0f, 20.0f);

   // Set the density of the ground box to zero. This will
   // make the ground body static (fixed).
   groundBoxDef.density = 0.0f;

   // Define the ground body.
   b2BodyDef groundBodyDef;
   groundBodyDef.position.Set(250.0f, 100.0f);

   // Part of a body's def is its list of shapes.
   groundBodyDef.AddShape(&groundBoxDef);

   // Call the body factory which allocates memory for the ground body
   // from a pool and creates the ground box shape (also from a pool).
   // The body is also added to the world.
   ground = world->CreateBody(&groundBodyDef);

   // Define another box shape for our dynamic body.
   b2BoxDef boxDef;
   boxDef.extents.Set(4.0f, 4.0f);

   // Set the box density to be non-zero, so it will be dynamic.
   boxDef.density = 1.0f;

   // Override the default friction.
   boxDef.friction = 0.3f;

   // Define the dynamic body. We set its position,
   // add the box shape, and call the body factory.
   b2BodyDef bodyDef;
   bodyDef.position.Set(260.0f, 100.0f);
   bodyDef.AddShape(&boxDef);
   body1 = world->CreateBody(&bodyDef);


I'm using HGE (hge.relishgames.com) as my games engine instead of opengl and I dont know if it the cause of my problems (I doube it, though I cant find any other difference). it uses a different coordinate system
the glut - 0,0 is the top most vertex of the screen and 800,600 is the rightmost bottom one.
the exe with the demo can be downloaded here:
http://www.mediafire.com/?71hdjgw2bgy

BTW - when i place the smaller box above the larger one, it does rest on its head as it should - only when it is inside the box it failes to do so.
Box2D shapes are treated as solids, so the solver tries to keep them from overlapping. If you want to create a hollow box then use four solid boxes to create the walls. Make the walls thick because Box2D doesn't have continuous collision detection yet.
what about a hollow circle?
Thanks for replying Erin.
I'm not sure I got your answer though - do you mean that this is expected behavior? if the boxes are solid doesn't it mean that they shouldnt cross through one another?
Also, I plan on using a circle and not a box in my real program - from giving it a try it seems to happen with circles as well. what would be the way to prevent this from happening with circls? would giving the floor polygon a larger thickness values fix the issue?

thanks, sorry if I am a bit confused 🙂
Your code:

Quote:
groundBodyDef.position.Set(250.0f, 100.0f);
...
bodyDef.position.Set(260.0f, 100.0f);


You are giving both boxes the same y-coordinate so they start out completely overlapped. What happens if you place the dynamic box completely above the ground box?

Boxes, circles, and polygons are completely solid in Box2D. The solver tries to remove all overlap. This is the expected behavior. Your test case is equivent to trying to push a match box into a solid brick.

If you want to make a hollow shape, for now I recommend approximating it with a sequence of fairly thin boxes. In the future I will provide a line segment soup shape (non-convex mesh).
@Erin
I assume that the different shapes are used only for collision checking, and that the physics calculations are independent. If that is so, then it shouldn't be two hard to create 'negative' shapes, which are actually gaps. That would make it easy to create non-convex shapes without the slow-down of meshes 😀
The basic collision algorithm I use is the separating axis test (SAT). This test only works for solid convex shapes. Even if I have a mesh, I would need to collide a convex shape against each nearby convex line segment. So for example, I would collide a circle with all the line segments that overlap the circle's axis aligned bounding box.

So I can't just invert the world to make a hollow box.

Another issue is the AABB overlap. You can see how these work in the test bed. An inverted box would need an infinite bounding box.
Basically, I mean that you test collision as normal, but if it collides with a negative shape, disregard any collisions between the two bodies. The collision test would be identical, but the result would be different. Also, the AABB could be specified by the user when creating a negative shape. BTW, I emailed you back.
I suppose something like that might be possible. I never intended Box2D to work that way and I have never seen a physics engine that works that way.

For Box2D I plan to support non-convex collision with compound shapes and line segment meshes.
Thanks everyone, it's all clear now and I could get it to work properly.
The only thing that i dont fully understand is what is the difference between my code and the hello world demo? do you only create a ground box there instead of an entire world box like I am? Im sorry, I'm just not used to the GLUT coordinates system where there is also negative x and y on the screen so i just want to make sure.

thanks again for the help and explanations 🙂

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