public static TexVertOutput TexToVert(World world, Texture2D texture, float mass, bool useCentroid, float scale) { Vertices verts; TexVertOutput output = new TexVertOutput(); // Creates an array for every pixel in the texture uint[] data = new uint[texture.Width * texture.Height]; texture.GetData(data); verts = PolygonTools.CreatePolygon(data, texture.Width, false); Vector2 centroid = Vector2.Zero; // Origin needs to be altered so it uses the origin of the verts // rather than the texture's centre. if (useCentroid) { centroid = -verts.GetCentroid(); verts.Translate(ref centroid); } else { centroid = ConvertUnits.ToSimUnits(new Vector2(texture.Width, texture.Height) * 0.5f); } float simScale = ConvertUnits.ToSimUnits(scale); Vector2 Scale = new Vector2(simScale, simScale); verts.Scale(ref Scale); verts = SimplifyTools.ReduceByDistance(verts, ConvertUnits.ToSimUnits(4f)); Body body = BodyFactory.CreateCompoundPolygon(world, EarclipDecomposer.ConvexPartition(verts), mass); body.BodyType = BodyType.Dynamic; if (!useCentroid) { body.LocalCenter = centroid; } output.Body = body; output.Origin = ConvertUnits.ToDisplayUnits(centroid); return(output); }
public static TexVertOutput TexToVert(World world, Texture2D texture, float mass, bool useCentroid, float scale) { Vertices verts; TexVertOutput output = new TexVertOutput(); // Creates an array for every pixel in the texture uint[] data = new uint[texture.Width * texture.Height]; texture.GetData(data); verts = PolygonTools.CreatePolygon(data, texture.Width, false); Vector2 centroid = Vector2.Zero; // Origin needs to be altered so it uses the origin of the verts // rather than the texture's centre. if (useCentroid) { centroid = -verts.GetCentroid(); verts.Translate(ref centroid); } else { centroid = ConvertUnits.ToSimUnits(new Vector2(texture.Width, texture.Height) * 0.5f); } float simScale = ConvertUnits.ToSimUnits(scale); Vector2 Scale = new Vector2(simScale, simScale); verts.Scale(ref Scale); verts = SimplifyTools.ReduceByDistance(verts, ConvertUnits.ToSimUnits(4f)); Body body = BodyFactory.CreateCompoundPolygon(world, EarclipDecomposer.ConvexPartition(verts), mass); body.BodyType = BodyType.Dynamic; if (!useCentroid) { body.LocalCenter = centroid; } output.Body = body; output.Origin = ConvertUnits.ToDisplayUnits(centroid); return output; }