Polygon Positions

Topics: Developer Forum
Dec 9, 2013 at 9:59 AM
Hello,

My question is trying to understand positions. Right now I am just playing around with farseer for learning purposes.

First I have a 20 x 20 standard box that was created using BodyFactory.CreateRectangle(). I set the position of the body to 350,350.

The next polygon is a custom convex polygon made with the BodyFactory.CreatePolygon() method. It is a triangle created with 3 vertices.

Problem is this:
I am printing out the positions of the box and the triangle to the title bar of the window and they are both different. The box is position 3.5, 3.5 but the triangle is position 0,0. I am trying to understand why this is happening. I decided to try to make the box with CreatePolygon() with 4 vertices just the same way as the triangle and they both say the position is 0,0 but on the screen they clearly are not in the same position. Can anybody tell me whats going on here?
Dec 9, 2013 at 11:35 PM
Update:

I figured out partially what I was doing wrong. I realized that being so used to the top left corner of a graphic/object represents the origin/position of that said graphic/object. In face I forgot that the centroid of the graphic/object is the origin/position. So physically on the screen they are indeed now being set the the same position. But the problem I see now is that the actual position being printed to the title bar is still diffirent on the x axis.

Position being printed is 3.5 for the x-coordinate of the box and 0 for the x-coordinate of the triangle. I am still very confused why they are physically on the screen in the same position on the x-coordinate but they are not showing that in the title bar of the window.
Dec 10, 2013 at 3:05 AM
The short (and largely unhelpful) answer is - it depends :)

What's your code look like?

Can you zip up your test program and share the link?
Dec 10, 2013 at 9:29 AM
HAL_9000,

Thanks hal. Right now this code has the triangle and the box horizontally line up the the same Position.X. But you will see that the string in the title bar says different. I am sure its probably something stupid. Another set of eyes will definitely help. Thanks!!

https://www.dropbox.com/s/zfkxu48e6yy7p8p/SpaceShooter.zip
Dec 10, 2013 at 2:35 PM
It's hard to explain in writing but when you're creating your own vertices you need to create them as if your objects centroid is 0,0. Once the body is made you then set the position to move (or in Farseer speech, transform) the object. The way you were doing it was having the ships position as 0,0 but the vertices off to the side where you thought it should be positioned.

An example might explain it better so in your project replace your ship constructor method with this:
     public MainShip(ref World world, Texture2D texture, Vector2 startPosition) : base(texture)//CONSTRUCTOR
        {
            //First create the vertices of the ship
            Vertices verts = new Vertices();


            Vector2 bodyPosition = ConvertUnits.ToSimUnits(startPosition);
            float halfSize = ConvertUnits.ToSimUnits((float)texture.Height / 2);

            verts.Add(new Vector2(0, -halfSize));//Top center
            verts.Add(new Vector2(halfSize, halfSize));//Bottom right
            verts.Add(new Vector2(-halfSize, halfSize));//The bottom left

            //Create the ships hull
            _body = BodyFactory.CreatePolygon(world, verts, 1);
            _body.BodyType = BodyType.Static;
            _body.Friction = 1f;
            _body.Position = bodyPosition;

        }
I think it would get more complicated for irregular polygons but simple shapes should be fine.
Dec 11, 2013 at 12:31 AM
Thanks Hal!! I completely get what is going on now. I greatly appreciate it.