4

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

4

1 回答 1

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

    }
于 2011-08-26T09:21:41.187 回答