Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xna 在Farseer中创建不规则二维精灵的主体_Xna_Farseer - Fatal编程技术网

Xna 在Farseer中创建不规则二维精灵的主体

Xna 在Farseer中创建不规则二维精灵的主体,xna,farseer,Xna,Farseer,我试图创建一个不规则的2D精灵Farseer 3.3.1的身体。是否可以使用BodyFactory.CreateCompundPolygon方法完成此操作?这是我的一个项目中的方法。它有点特定于我的体系结构,但应该可以自己使用 要考虑的一件事是缩放。您最好用ConvertUnits.ToSimUnits等替换它,我相信您熟悉这些 public static Body CreateBodyFromImage(Game game, World world, string texture

我试图创建一个不规则的2D精灵Farseer 3.3.1的身体。是否可以使用BodyFactory.CreateCompundPolygon方法完成此操作?

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

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

    }
公共静态主体CreateBodyFromImage(游戏、世界、字符串纹理名称)
{
//加载传递的纹理。
Texture2D polyContexture=game.Content.Load(textureName);
//使用数组保存纹理数据。
uint[]数据=新uint[polyContexture.Width*polyContexture.Height];
//将纹理数据传输到阵列中。
获取数据(数据);
//查找构成所传递纹理形状轮廓的垂直线。
顶点顶点=多边形工具.CreatePolygon(数据,polyContexture.Width);
//现在我们需要缩放顶点(结果以像素为单位,我们使用米)
矢量2标度=新矢量2(0.07f,0.07f);
顶点比例(参考比例);
//将凹多边形划分为凸多边形。
var分解顶点=BayazitDecomposer.ConvexPartition(顶点);
//创建一个实体,该实体具有多边形形状的多个装置。
返回BodyFactory.CreateCompoundPolygon(世界,分解顶点,1f);
}

设置车身质心也有问题。我遵循Farseer 3.3.1示例中提供的代码//Vector2 centroid=-textureVertices.GetCentroid()//纹理平移(参考质心)//basketOrigin=-质心;这使得这个物体现在完全静止不动。谢谢@Nick,至少我可以在屏幕上看到精灵。但是碰撞仍然没有发生,如果(kstate.IsKeyDown(Keys.Down))\u component.ApplyForce(ConvertUnits.ToSimUnits(Vector2.UnitY));if(kstate.IsKeyDown(Keys.Up))_component.ApplyForce(ConvertUnits.ToSimUnits(-Vector2.UnitY));这会平移对象,但不会旋转对象。知道为什么会这样吗?@Yousuf,你在运行DebugViewXNA吗?这将是明智的,是100%的,你的身体是在同一位置作为你的精灵,这是正确的规模。我假设_component是使用该方法创建的实体,对吗?@Yousuf,我会检查DebugView,确保事情在你认为的地方。此外,还要确保您在之后设置了身体,例如将IsEnabled设置为true、BodyType设置为dynamic等。