Box2D Forums

It is currently Sat May 18, 2013 9:43 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Aug 19, 2012 12:51 pm 
Offline

Joined: Sun Aug 19, 2012 12:36 pm
Posts: 1
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:
public World world;
world = new World(new Vector2(0, -10f), true);


Then I create a box and a rectangle.

Code:
               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:
            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:
 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?


Top
 Profile  
 
PostPosted: Fri Dec 28, 2012 8:51 pm 
Offline

Joined: Mon Jun 15, 2009 4:34 pm
Posts: 69
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:
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);
            }
        }
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group