繁体   English   中英

在XNA中旋转3D模型

[英]Rotate 3D Model in XNA

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

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

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

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

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

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

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

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

原帖:

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

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM