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

Polygon from texture

Topics: User Forum
Dec 4, 2011 at 12:24 PM


I have been following the Documentation and a few post whilst searching the discussions, and I have managed to crate a polygon, but I am a little unsure as to what scale I should use.

Please have a look at my code, I am using 100 pixels per meter with the ConvertUnits class from the samples, and the dimensions of the texture are 882px x 140px, but I am unsure of the scale.

            texture = content.Load<Texture2D>(textureString);

            //Create an array to hold the data from the texture
            uint[] data = new uint[texture.Width * texture.Height];
            //Transfer the texture data to the array
            //Find the vertices that makes up the outline of the shape in the texture
            Vertices verts = PolygonTools.CreatePolygon(data, texture.Width/*, texture.Height*/, true);
            //For now we need to scale the vertices (result is in pixels, we use meters)
            Vector2 scale = new Vector2(0.008f, 0.008f);
            verts.Scale(ref scale);
            //Make sure that the origin of the texture is the centroid (real center of geometry)
            Vector2 polygonOrigin = verts.GetCentroid();
            //Translate the polygon so that it aligns properly with centroid.
            Vector2 vertsTranslate = -polygonOrigin;
            verts.Translate(ref vertsTranslate);
            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            _list = BayazitDecomposer.ConvexPartition(verts);
            //Create a single body with multiple fixtures
            body = BodyFactory.CreateCompoundPolygon(world,_list, 1);

Thanks for your help,


Dec 9, 2011 at 8:10 PM

You can look at this discussion, similar questions but the answer should be the same