在XNA中旋转3D模型

dav*_*bro 9 c# xna rotation matrix

我是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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这将使船旋转,但不会沿着船的轴旋转.如果我旋转Y轴(通过改变rotationY的值),船将沿Y轴旋转.但是,如果我旋转X轴或Z轴,船会根据世界的X轴和Z轴旋转,而不是自己旋转.如何使船在自己的轴上旋转?我是否需要对矩阵做一些不同的事情?谢谢

Ste*_*e H 6

使用CreateRotationX,CreateRotationY和CreateRotationZ都可以在全球或全局轴上应用旋转.这意味着它会导致对象仅围绕世界/全局轴旋转,而不是对象的局部轴旋转.

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

然而,由于围绕任意轴(例如船的向上)的旋转可能导致一次改变3个角度值中的任何一个,因此需要改变整体旋转范例.跟踪所有这些不必要的困难.有一种更简单的方法:

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