在XNA中旋转3D模型

时间:2012-06-11 01:08:20

标签: c# matrix xna rotation

我是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轴旋转,而不是自己旋转。如何使船在自己的轴上旋转?我是否需要对矩阵做一些不同的事情? 感谢

3 个答案:

答案 0 :(得分:6)

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

使用CreateFromAxisAngle可以输入您想要的任何旋转轴,包括船舶自身的本地轴。

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

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

答案 1 :(得分:2)

编辑:在这里给Steve一些功劳(很棒的答案伙伴,因为我做了很多3D数学,已经有一段时间了。)

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

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Rotation_-_translation.php

原帖:

我相信你必须在BasicEffect循环中创建一个效果。

我相信所有这些都在MSDN的基础教程中有所介绍。你的代码甚至看起来像是从那里来的。

http://msdn.microsoft.com/en-us/library/bb203897(v=xnagamestudio.31)

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

http://www.riemers.net/eng/Tutorials/XNA/Csharp/series1.php

答案 2 :(得分:1)

这是我最终做的事情,以防万一其他人像我一样陷入困境:

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();
    }
}
相关问题