在Farseer中创建一个不规则2D精灵的主体

时间:2011-08-25 11:37:40

标签: xna farseer

我试图创建一个不规则的2D精灵Farseer 3.3.1的主体。可以使用BodyFactory.CreateCompoundPolygon方法吗?

1 个答案:

答案 0 :(得分:2)

这是我的一个项目的方法。它有点特定于我的架构,但应该可以自己使用。

要考虑的一件事是缩放。你最好用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);

    }