This project has moved and is read-only. For the latest updates, please go here.

Body from texture alignment

Topics: User Forum
Jan 7, 2012 at 10:55 AM


I wonder if anyone could help me. I have a slight alligment problem with one of my bodies, as the screenshot shows:

The body is created from Texture data using the PolygonTools, here is the code:

       public override void LoadContent(ContentManager content)
           uint[] data = new uint[width * height];
           Vertices verts = PolygonTools.CreatePolygon(data, width/*, texture.Height*/, true);
           Vector2 polygonOrigin = verts.GetCentroid();
           Vector2 vertsTranslate = -polygonOrigin;
           verts.Translate(ref vertsTranslate);
           Vector2 scale = new Vector2(ConvertUnits.ToSimUnits(1));
           verts.Scale(ref scale); 
           _list = BayazitDecomposer.ConvexPartition(verts);
          body = BodyFactory.CreateCompoundPolygon(world,_list, 1);

Later in the game I am also setting the body.Position using the TopLeft ofthe Sprite:

       public override Vector2 TopLeft
           get { return topLeft; }
               topLeft = value;
               float x = topLeft.X + (width / 2);
               float y = topLeft.Y + (height / 2);
               if (body != null)
                   body.Position = new Vector2(ConvertUnits.ToSimUnits(x), ConvertUnits.ToSimUnits(y));

And lastly, here is my draw code:

           Rectangle dest = new Rectangle((int)topLeft.X + (width / 2), (int)topLeft.Y + (height / 2), width, height);
           spriteBatch.Draw(texture, dest, null, Color.White, body.Rotation, new Vector2(texture.Width / 2, texture.Height / 2), SpriteEffects.None, 0);

As you can see, all my other basic shaped bodies align with the texture just fine, perhaps it is something to do with the vert.Translate?
Any help would be much appreciated.