Create a body of irregular 2D sprite in Farseer

Im trying to create the body of an irregular 2D sprite Farseer 3.3.1. Can this be done using the BodyFactory.CreateCompoundPolygon method?

+4
source share
1 answer

This is a method from one of my projects. It depends a bit on my architecture, but should be useful for you.

Consideration should be given to scaling. Your best bet is to replace this with 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); } 
+2
source

Source: https://habr.com/ru/post/1368942/


All Articles