PolygonShape issue

Topics: User Forum
Mar 21, 2011 at 9:49 PM
Edited Mar 21, 2011 at 9:50 PM

 

Using Farseer 3.3...  When I create a box at 0,10 and a width/height of 2,2 with the following code, it appears in the correct place in my level:

Vector2 p1 = new Vector2(0.0f, 10.0f);

Body bumper = BodyFactory.CreateBody(World, p1);
bumper.BodyType = BodyType.Static;

PolygonShape box = new PolygonShape(1);
box.SetAsBox(1.0f, 1.0f);

bumper.CreateFixture(box);

However, when I try to create an identical box using this code, it appears about 10 meters above the box created using box.setAsBox:

_vertices = new Vertices(4);

_vertices.Add(new Vector2((Body.Position.X - width / 2.0f), (Body.Position.Y - height / 2.0f)));
_vertices.Add(new Vector2((Body.Position.X + width / 2.0f), (Body.Position.Y - height / 2.0f)));
_vertices.Add(new Vector2((Body.Position.X + width / 2.0f), (Body.Position.Y + height / 2.0f)));
_vertices.Add(new Vector2((Body.Position.X - width / 2.0f), (Body.Position.Y + height / 2.0f)));

box = new PolygonShape(_vertices, 1.0f);

I stepped through the .SetAsBox code and didn't see anything of note but is there somewhere in the Farseer code that I'm missing that is doing an additional transformation?
Screen shot: http://www.dreambuildrepeat.com/temp/FarseerScreenie.jpg

Thanks,
Pat
Mar 29, 2011 at 12:54 AM

I hate to bump threads but this really doesn't make sense to me...  Anybody?

Developer
Mar 29, 2011 at 7:44 AM

Fixtures, shapes etc. are always in local coordinates and not world coordinates. Otherwise using transformations would be quite painful.

Here is an excerpt from the SetAsBox code:

Vertices vertices = new Vertices(4);
vertices.Add(new Vector2(-hx, -hy));
vertices.Add(new Vector2(hx, -hy));
vertices.Add(new Vector2(hx, hy));
vertices.Add(new Vector2(-hx, hy));

So you are the one applying the additional transformation. Just leave the body position out of your box definition and you should be fine.

Mar 29, 2011 at 2:10 PM

Thank you Elsch, works perfectly now.  Onward and upward!