Positions Not Centered

Topics: Developer Forum, User Forum
Feb 24, 2010 at 10:11 PM

 

I am creating enemies using a program where I specify the position of all the vertices. I can use vertices, square, or circles. The final geoms look exactly like they should but there is just one problem. All bodies are offcentered.

 

Here are some images showing it:

Here you can see that the body is positioned to low.

Here the body is positioned to high!? (Both of these problems use the exact same code)

Here is how it looks when combined. It's not huge but it needs fixed. Also you can see a little bit of the other enemy how his is also not positioned correctly. He uses the Rectangle code I have.

Here is what it should look like. This is from the program I am using.

 

 

Why are the bodies not in their correct positions? I thought it maybe how they are centered by centroid but still doesn't work.

Here is my code for the polygon objects.

 

                        PathItem pItem = (PathItem)item;
                        Vertices vertices = new Vertices(pItem.LocalPoints);
                        vertices.SubDivideEdges(50);
                        Vector2 relPosition = pItem.Position + vertices.GetCentroid();
                        tempBody = BodyFactory.Instance.CreatePolygonBody(Farseer.Physics, vertices, mass);
                        tempGeom = GeomFactory.Instance.CreatePolygonGeom(Farseer.Physics, tempBody, vertices, -1f);
                        //Set position using LevelMaker.GetCenter(vert)
                        //TODO: Consider putting this in its own special class
                        tempBody.Position = this.position + relPosition;

 

                        PathItem pItem = (PathItem)item;

                        Vertices vertices = new Vertices(pItem.LocalPoints);
                        vertices.SubDivideEdges(50);
                        Vector2 relPosition = pItem.Position + vertices.GetCentroid();

                        tempBody = BodyFactory.Instance.CreatePolygonBody(Farseer.Physics, vertices, mass);

                        tempGeom = GeomFactory.Instance.CreatePolygonGeom(Farseer.Physics, tempBody, vertices, -1f);

                        //Set position using LevelMaker.GetCenter(vert)
                        //TODO: Consider putting this in its own special class
                        tempBody.Position = this.position + relPosition;

this.position is the world position and relPosition should be the body position. I bet it still has something to with centroid but I don't know what to do. Any help is very appreciated :)

 

 

Feb 25, 2010 at 1:18 PM

I can put up all of the code for how I create the enemies if you guys need more. I figured there is something up with the code I have above but I can always put up more :)

Feb 25, 2010 at 7:19 PM

I'm wondering if maybe because your image is slightly smaller than the canvas, your centroid will be slighly lower. Try getting the middle point using new Vector2(tex.Width / 2, tex.Height / 2) and see if that makes a difference, maybe?

Feb 26, 2010 at 2:30 AM

I got it working. I had to set the new origin for the images by subtracting the relative position of the body and the image position. It works now :)

Now to move on to flipping geometries to look left and right!