簡體   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