C# 在XNA中旋转三维模型

C# 在XNA中旋转三维模型,c#,matrix,xna,rotation,C#,Matrix,Xna,Rotation,我是XNA的新手,我正在创建一个简单的游戏。很抱歉,这可能很简单,但我找不到任何帮助。游戏中我用Blender制作了一艘飞船,我想通过旋转飞船的X、Y和Z轴来控制飞船。以下是我的代码: protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); RotationMatrix = Matrix.CreateRotationY(MathHelpe

我是XNA的新手,我正在创建一个简单的游戏。很抱歉,这可能很简单,但我找不到任何帮助。游戏中我用Blender制作了一艘飞船,我想通过旋转飞船的X、Y和Z轴来控制飞船。以下是我的代码:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
  RotationMatrix = Matrix.CreateRotationY(MathHelper.PiOver2) * Matrix.CreateRotationY    (rotationY) * Matrix.CreateRotationX(rotationX) * Matrix.CreateRotationZ(rotationZ);

        Matrix shipTransformMatrix = RotationMatrix * Matrix.CreateTranslation(ship.Position);

                        DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
        // TODO: Add your drawing code here


        base.Draw(gameTime);
    }

    public  void DrawModel(Model model, Matrix modelTransform, Matrix[] absoluteBoneTransforms)
    {
        //Draw the model, a model can have multiple meshes, so loop
        foreach (ModelMesh mesh in model.Meshes)
        {
            //This is where the mesh orientation is set
            foreach (BasicEffect effect in mesh.Effects)
            {

                effect.World = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
                effect.Projection = projectionMatrix;
                effect.View = viewMatrix;

            }

            //Draw the mesh, will use the effects set above.
            mesh.Draw();
        }
    }
这将使船舶旋转,但不会沿船舶轴线旋转。如果我旋转Y轴(通过更改rotationY的值),船舶将沿其Y轴旋转。但是如果我旋转X轴或Z轴,飞船会根据世界的X轴和Z轴旋转,而不是它自己的轴。我怎样才能使船绕着自己的轴旋转呢?我需要对矩阵做些不同的事情吗?
感谢

编辑:在这里给了史蒂夫一些荣誉(很好的答案,伙计,我已经有一段时间没有学过很多3D数学了)

本教程将向您展示如何设置Steve建议的内容

原职:

我相信你必须创造一种效果。在你的基本效果循环中旋转

我相信,MSDN的基础教程涵盖了所有这些。你的代码看起来甚至像来自那里

如果没有,请查看此链接,Reimer涵盖了3D中值得了解的所有内容:


使用CreateRotationX、CreateRotationY和CreateRotationZ都会围绕世界轴或全局轴应用旋转。这意味着它将使对象仅绕世界/全局轴旋转,而不是绕对象的局部轴旋转

使用CreateFromAxisAngle可以输入所需的任何旋转轴,包括船舶自身的局部轴

然而,需要改变整体旋转模式,因为围绕任意轴旋转(例如,船的向上旋转)可能会同时改变3个角度值中的任何一个。跟踪所有这些是不必要的困难。有一个更简单的方法:


只需存储矩阵(或四元数)形式的旋转,而不是3个角度。

以下是我最后要做的,以防其他人像我一样被卡住:

Matrix RotationMatrix;
//every time I wanted to rotate around an axis, I would do something like this:
protected void rotateY()
    {
        RotationMatrix *= Matrix.CreateFromAxisAngle(RotationMatrix.Up, MathHelper.ToRadians(1.0f));
        //For the X axis I used RotationMatrix.Right, and for the Z RotationMatrix.Forward
    }
protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);


    Matrix shipTransformMatrix = RotationMatrix * Matrix.CreateTranslation(ship.Position);

                    DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
    // TODO: Add your drawing code here


    base.Draw(gameTime);
}

public  void DrawModel(Model model, Matrix modelTransform, Matrix[] absoluteBoneTransforms)
{
    //Draw the model, a model can have multiple meshes, so loop
    foreach (ModelMesh mesh in model.Meshes)
    {
        //This is where the mesh orientation is set
        foreach (BasicEffect effect in mesh.Effects)
        {

            effect.World = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
            effect.Projection = projectionMatrix;
            effect.View = viewMatrix;

        }

        //Draw the mesh, will use the effects set above.
        mesh.Draw();
    }
}

谢谢我尝试在BasicFect循环中创建一个effect.Rotation,但该效果没有旋转属性。我的问题不是旋转模型,我可以做到,而是让模型的轴随着模型旋转。当我做
RotationMatrix=Matrix.CreateRotationY(MathHelper.PiOver2)*Matrix.CreateRotationY(rotationY)*Matrix.CreateRotationX(rotationX)*Matrix.CreateRotationZ(rotationZ),我先为哪个轴创建一个旋转轴,然后再为哪个轴创建一个旋转轴,但它后面的两个轴不旋转。是否有一个确定的顺序,我必须将矩阵按多个顺序排列,使它们随模型旋转?顺序并不重要(据我所知)。但我确实看到了你的问题。执行以下操作:RotationMatrix=Matrix.CreateRotationY(rotationY)*Matrix.CreateRotationX(rotationX)*Matrix.CreateRotationZ(rotationZ);我猜MathHelper.PiOver2是将你的飞船预先对准某个角度。相反,将旋转Y、X、Z预先设置为开始时希望的值。这将创建您要查找的旋转。当它绕中心轴旋转时,我很确定顺序确实很重要,因为矩阵相乘的方式。矩阵A乘以矩阵B不同于B乘以A。我试过你说的代码,但得到了相同的结果。如果我做
RotationMatrix=Matrix.CreateRotationY(rotationY)*Matrix.CreateRotationX(rotationX)*Matrix.CreateRotationZ(rotationZ)模型的Y轴随之旋转,但如果我这样做,则
旋转矩阵=Matrix.CreateRotationX(rotationX)*Matrix.CreateRotationY(rotationY)*Matrix.CreateRotationZ(rotationZ)X轴随船旋转,而Y轴不随船旋转。我怎么让所有的轴旋转?你说得对davidsbro,顺序很重要。当您绕3个轴旋转时,由于所谓的“万向节锁”,您正在经历的是自由度的损失。您可以通过将四元数操作组合在一起以生成所需的旋转来解决此问题。(来源:)(来源:)谢谢。如何在CreateFromAxisAngle中定义我的船的旋转轴?我试过了,但它只是改变了我的飞船。如何在矩阵中存储旋转?这就是我一直想做的。碰巧我写了一篇关于它的博客文章。谢谢我在你挂上之前就找到了你挂上的链接。哈哈。我读了它,并且能够解决这个问题。非常感谢。这很清楚,也很有帮助