# Create a body of an irregular 2D sprite in Farseer

I'm trying to create a body of a irregular 2D sprite Farseer 3.3.1.
The regular shapes that BodyFactory provides are not that I want. Is there a way that one can create irregular objects? Could it be done using the BodyFactory.CreateCompoundPolygon method?

• I haven't used Farseer, but for Box2D you usually triangulate a complex polygon. – pek Aug 25 '11 at 15:28
• So how did you go about doing that? Any tutorials, articles? – Yousuf Tafhim Aug 25 '11 at 16:42
• See my answer ;) – pek Aug 25 '11 at 16:54

Farseer Physics Engine's support of polygons is otherwise well-documented. Although the documentation may need to be updated to the latest version.

You can create polygons using a list of Vertices, which (simply put) are just the coordinates of all the corners. Vertices can either be defined in code, or be generated on the basis of an image file using some of the tools provided.

Straight from the documentation:

  //load texture that will represent the physics body

//Create an array to hold the data from the texture
uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];

//Transfer the texture data to the array
polygonTexture.GetData(data);

//Find the vertices that makes up the outline of the shape in the texture
Vertices verts = PolygonTools.CreatePolygon(data, polygonTexture.Width, polygonTexture.Height, true);

//For now we need to scale the vertices (result is in pixels, we use meters)
Vector2 scale = new Vector2(0.07f, 0.07f);
verts.Scale(ref scale);

//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
List<Fixture> compund = FixtureFactory.CreateCompoundPolygon(World, _list, 1);
compund[0].Body.BodyType = BodyType.Dynamic;


To get textures (sprites) from polygons generated on-the-fly instead of from an image file, you need a little extra code. In the samples project bundled with the latest version of FPE, there is a class called AssetCreator that can generate polygons on the basis of vertices and a base (fill) texture.

Once you have added the AssetCreator-class and its dependencies to your code, you can use it to generate polygon textures e.g. like this:

        AssetCreator assetCreator = new AssetCreator(graphics.GraphicsDevice);

Vertices vertices = new Vertices();

//polygon body
PolygonShape shape = new PolygonShape(vertices, 1);
sprite = new Sprite(assetCreator.TextureFromShape(shape, MaterialType.Blank, Color.DimGray, 1f));
body = BodyFactory.CreatePolygon(world, vertices, 1f);
Fixture fixture = FixtureFactory.AttachPolygon(vertices, 1f, body);
fixture.Body.BodyType = BodyType.Dynamic;