Создайте тело нерегулярного 2D-спрайта в Farseer - PullRequest
4 голосов
/ 25 августа 2011

Я пытаюсь создать тело нерегулярного 2D-спрайта Farseer 3.3.1.Можно ли это сделать с помощью метода BodyFactory.CreateCompoundPolygon?

1 Ответ

2 голосов
/ 26 августа 2011

это метод из одного из моих проектов. Это немного специфично для моей архитектуры, но должно быть пригодно для себя.

Одна вещь, которую нужно учитывать, это масштабирование. Лучше всего заменить это на ConvertUnits.ToSimUnits и т. Д., С которыми, я уверен, вы знакомы.

       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);

    }
...