I have already looked at the Documentation on there Website, but the code provieded to create a shape out of an Texture is outdated for the newest version. The body is very simple so maybe i can do it with just 2 rectange but i have no idea how to do, because I just started with Farseer. Here my texture: Texture


First, you need to get the color data as uint's in an array, so:

Texture tex = Content.Load<Texture>("texture");
uint[] texData = new uint[tex.Width * tex.Height];

After, you can use Farseer's TextureConverter to convert the data into Vertices (from FarseerPhysics.Common)

Vertices vertices;
vertices = TextureConverter.DetectVertices(texData, tex.Width);

Finally, you want to decompose those Vertices into something usable so they can be used to create polygons, using one of the many Farseer Decomposers

List<Vertices> vertexList = FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(vertices);

There are many different methods under FarseerPhysics.Common.Decomposition, you can play around with the different methods to see which one you like best. Now you have the list of vertices needed for BodyFactory.CreateCompoundPolygon(...), which will return a body with the shape of your texture!

You might need to scale your vertices appropriately (since the result is in pixels and Farseer uses "meters") so you can do something like this

Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1));
foreach (Vertices vert in vertexList)
  vert.Scale(ref vertScale);
  • \$\begingroup\$ but why do you do there: Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)); Why 1? Why not 2? or should I use the texture width? \$\endgroup\$ – Michael Jun 3 '12 at 14:44
  • \$\begingroup\$ Do you use the ConvertUnits tool found in the Farseer demos? What that does is scale all the vertices by 1, but not 1 pixel, one "meter" length, meters being defined somewhere at the beginning of your code. Its not changing the length of the vertices to "1" either, its scaling it x1, so basically keeping it the same size as it is, but relative to your world distance measurements. \$\endgroup\$ – Benixo Jun 3 '12 at 14:53
  • \$\begingroup\$ Just tried it, but dont works :( \$\endgroup\$ – Michael Jun 3 '12 at 14:54
  • \$\begingroup\$ yeah i use it :) \$\endgroup\$ – Michael Jun 3 '12 at 14:58
  • \$\begingroup\$ Yeah i called it, its alwaly rotating and collides with although its half the screen away. But as you can see my texture is nearly a rectange, so i tried it just with an rectange there it worked okey, but though the corners the collision is not perfectly :) \$\endgroup\$ – Michael Jun 3 '12 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.