Collision is not working, driving me nuts

Hello

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:

Code: Select all

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


Then I create a box and a rectangle.

Code: Select all

               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);

Code: Select all

            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:

Code: Select all

 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.

Image

Does anyone have any ideas what I could be doing wrong?
You are probably not drawing the boxes the correct places. The vertices of the shapes never change, they get translated and rotated by the body's transform (Body.GetTransform())

This code is from Farseer Physics Engine DebugViewXNA, and it shows how you translate the points of the shape into the correct place for all bodies in the world:

Code: Select all

foreach (Body body in World.BodyList)
{
    foreach (Fixture f in body.FixtureList)
    {
        PolygonShape polygon = f.Shape as PolygonShape;
        if (polygon != null)
        {
            Transform xf;
            body.GetTransform(out xf);

            for (int i = 0; i < polygon.Vertices.Count; i++)
            {
                Vector2 tmp = MathUtils.Mul(ref xf, polygon.Vertices[i]);
                DrawPoint(tmp, 0.1f, Color.Red);
            }
        }
    }
}

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