This project is read-only.

Collision is not working, driving me nuts

Aug 21, 2012 at 8:17 PM
Edited Aug 21, 2012 at 8:18 PM


I'm new to Box2D, but not to C# and programming.
I am trying to implement Box2D into my game engine and I am having a problem with collisions.

I set upp the world like this:



public World world;
world = new World(new Vector2(0, -10f), true);

Then I create a box and a rectangle.

   BodyDef groundBodyDef = new BodyDef();
          groundBodyDef.position = new Vector2(0.0f, -10.0f);

           Body groundBody = world.CreateBody(groundBodyDef);

           PolygonShape groundBox = new PolygonShape();

           groundBox.SetAsBox(50.0f, 10.0f);

           groundBody.CreateFixture(groundBox, 0.0f);

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.position = new Vector2(0, 100);
            boxBodyDef.type = BodyType.Dynamic;

            Body boxBody = world.CreateBody(boxBodyDef);

            PolygonShape boxBox = new PolygonShape();

            boxBox.SetAsBox(10.0f, 10.0f);

            boxBody.CreateFixture(boxBox, 0.0f);
After that in my game loop i take a step like this:

             world.Step(1f / 60f, 6, 2);

When I am tracking the position of the dynamic objects I can se that they are moving.
And graphicly they are too. And the static object is static.

My problem is that the objects don't seem to collide. When i track the vertecies in the shapes the don't seem to move, while the position of the object does move.

This is what i mean:
The red squares are representing the vertecies of the shape/body.
What you can see is that the pink, orange and dark blue squares are passing through the light blue square and the black ground.
It seems like the world/simulator is not updating the bodies geometry, but is updating it's position.

Does anyone have any ideas what I could be doing wrong?
Aug 22, 2012 at 12:15 AM

The vertex geometry is relative to the object, so you should factor in the object's position/rotation if you want to track their absolute positions.

As for why they aren't colliding, it is hard to tell from the sample source but logically they should be (dynamics will collide and perform collision response like rotating, changing velocity, etc.).

One thing I notice is that you're using rather large units.  A box that is 10 x 10 means it is 10 x 10 meters large which is the size of a building.  So it could be that these things have such a large amount of momentum that they are "tunneling" through each other.  You should probably scale things down to a reasonable size (i tend to use 64 pixels = 1 meter, so divide numbers by 64 for physics units).  You can also look at increasing position/velocity iteration counts when you tick the world.

Aug 22, 2012 at 12:49 PM
Edited Aug 22, 2012 at 12:51 PM

Thank you for your reply.

I did get my vertex geometry code working as it should now and I am able to see that they are moving around the world as they should, following the body.
And in my debug console I am following the position and the rotation of the bodies, their values just pass through eachother as if they never had contact.
Falling straight through the static body. 

I did make some changes to my code after my last post, I tried co clean it up even more and decrease the margin of error.
What I ended up with is now this:


physicsWorld = new World(new Vector2(0, -10f), true);
physicsWorld.WarmStarting = true;
physicsWorld.ContinuousPhysics = true;


I enabled warm starting and continous physics.
Also, now I only create 2 bodies, a static rectangle and a dynamic box. As I understood it a dynamic objects does collide and responde to contact with a static one.



            BodyDef rectangleDef = new BodyDef();
            rectangleDef.type = BodyType.Static;
            rectangleDef.position = new Vector2(0, -30);
            rectangleDef.angle = 0;

            Body rectangleBody =       physicsWorld.CreateBody(rectangleDef);

            PolygonShape rectangleShape = new PolygonShape();
            rectangleShape.SetAsBox(70, 5);

            FixtureDef rectangleFixtureDef = new FixtureDef();
            rectangleFixtureDef.shape = rectangleShape;
            rectangleFixtureDef.density = 1;




            BodyDef boxDef = new BodyDef();
            boxDef.type = BodyType.Dynamic;
            boxDef.position = new Vector2(0, 5);
            boxDef.angle = 0;

            Body boxBody = physicsWorld.CreateBody(boxDef);

            PolygonShape boxShape = new PolygonShape();
            boxShape.SetAsBox(1, 1);

            FixtureDef boxFixtureDef = new FixtureDef();
            boxFixtureDef.shape = boxShape;
            boxFixtureDef.density = 1;




I also changed the parameters of the Step method so that it uses many more iterations


            physicsWorld.Step(1f / 60f, 50, 50);


And I put in some debug code to color the background red if there is any contact between the bodies in the world, but it never fires.


              if (physicsWorld.ContactCount > 0)
                loadedLevel.levelBackgroundColor = Color.Red;


I've spent about 8 hours on this issue now and I'm getting out of good ideas to what might be the problem here.
So I would be grateful of any reply.