Im trying to create a body of a irregular 2D sprite Farseer 3.3.1. Could it be done with using BodyFactory.CreateCompoundPol开发者_C百科ygon method?
this is a method from one of my projects. Its a little specific to my architecture but should be usable for yourself.
One thing to consider is the scaling. You would be best to replace this with the ConvertUnits.ToSimUnits etc which I am sure you are familiar with.
public static Body CreateBodyFromImage(Game game, World world, string textureName)
{
//Load the passed texture.
Texture2D polygonTexture = game.Content.Load<Texture2D>(textureName);
//Use an array to hold the textures data.
uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];
//Transfer the texture data into the array.
polygonTexture.GetData(data);
//Find the verticals that make up the outline of the passed texture shape.
Vertices vertices = PolygonTools.CreatePolygon(data, polygonTexture.Width);
//For now we need to scale the vertices (result is in pixels, we use meters)
Vector2 scale = new Vector2(0.07f, 0.07f);
vertices.Scale(ref scale);
//Partition the concave polygon into a convex one.
var decomposedVertices = BayazitDecomposer.ConvexPartition(vertices);
//Create a single body, that has multiple fixtures to the polygon shapes.
return BodyFactory.CreateCompoundPolygon(world, decomposedVertices, 1f);
}
精彩评论