Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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];
tex.GetData<uint>(texData);

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);
share|improve this answer
    
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? –  Michael Jun 3 '12 at 14:44
    
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. –  Benixo Jun 3 '12 at 14:53
    
Just tried it, but dont works :( –  Michael Jun 3 '12 at 14:54
    
yeah i use it :) –  Michael Jun 3 '12 at 14:58
    
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 :) –  Michael Jun 3 '12 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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