Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been working with the Farseer Physics Engine to try and build a simple 2D, sidescrolling game. Creating basic rectangle blocks worked easily enough, but I discovered when I tried to create some very simple triangle slopes that my existing 'character' avatar would not collide with them. This baffled me for a while until I began using the debug view to discover that while the body of the triangle appeared where I had placed them, the fixture which determined collisions was offset, as shown below (The debugging view is itself offset from the actual sprites because I'm clearly not smart enough to figure out the proper projection matrix):Triangle offset.

It turns out that whatever coordinates I give for the center of the triangle, the fixture appears at (2x, 2y). For example, in the attached image, there is a triangle sloping up to the left at (-4, -1) in the game's coordinate system. The fixture (light green triangle bordered in magenta) is at (-8, -2).

I know this has to be a simple mistake I'm making, but damned if I can figure it out. Here's how I create a new slope in the level:

public override void AddToWorld(FarseerPhysics.Dynamics.World world, Vector2 position)
        {
            body = BodyFactory.CreateBody(world, position);
            body.BodyType = FarseerPhysics.Dynamics.BodyType.Static;

            Vertices vertices = new Vertices();
            if (type == SlopeType.LEFT)
            {
                vertices.Add(new Vector2(position.X - 0.5f, position.Y - 0.5f));
            }
            else
            {
                vertices.Add(new Vector2(position.X + 0.5f, position.Y - 0.5f));
            }
            vertices.Add(new Vector2(Position.X + 0.5f, position.Y + 0.5f));
            vertices.Add(new Vector2(position.X - 0.5f, position.Y + 0.5f));

            FixtureFactory.AttachPolygon(vertices, 1f, body, this);

        }

The position parameter is given to be the center, thus the 0.5 offsets to create the triangle's edges.

Any insight would be greatly appreciated; this engine seems powerful but the documentation is definitely lacking.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

You are creating a body and attaching a fixture at body-local position e.g. (10, 10).

But the body was created at world position (10, 10). That means the fixture is now at (10, 10) + (10, 10) = (20, 20).

Fixtures are always specified in body-local space; they should be roughly around the (0, 0) area regardless of where you intend to move the body.

share|improve this answer
    
Oooh, I see. I never noticed that since the RectangleFactory handles creating vertices for you. Thanks muchly! –  Tom G Jul 22 '11 at 21:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.