C# XNA四元数.createFromAxisAngle

C# XNA四元数.createFromAxisAngle,c#,xna,quaternions,C#,Xna,Quaternions,我正在用XNA编写一个飞行模拟器程序(类似于Star fox 64),我的旋转有问题。我的想法是有一个速度向量和位置向量。我允许的控件使得Euler的角度会引起问题,所以我想使用四元数 我还不太熟悉图形渲染,但我想我可以使用Quaternion.createFromAxisAngle(vector,roll)(roll是围绕速度向量的旋转),但它不能正常工作。基本上,每当我尝试旋转俯仰或偏航时,什么都不会发生。当我翻滚时,它会工作,但不像我预期的那样。这是createFromAxisAngle方

我正在用XNA编写一个飞行模拟器程序(类似于Star fox 64),我的旋转有问题。我的想法是有一个速度向量和位置向量。我允许的控件使得Euler的角度会引起问题,所以我想使用四元数

我还不太熟悉图形渲染,但我想我可以使用Quaternion.createFromAxisAngle(vector,roll)(roll是围绕速度向量的旋转),但它不能正常工作。基本上,每当我尝试旋转俯仰或偏航时,什么都不会发生。当我翻滚时,它会工作,但不像我预期的那样。这是createFromAxisAngle方法的正确用法吗?谢谢

以下是我船的更新位置代码:

    public override void UpdatePositions()
    {
        float sinRoll = (float)Math.Sin(roll);
        float cosRoll = (float)Math.Cos(roll);
        float sinPitch = (float)Math.Sin(pitch);
        float cosPitch = (float)Math.Cos(pitch);
        float sinYaw = (float)Math.Sin(yaw);
        float cosYaw = (float)Math.Cos(yaw);

        m_Velocity.X = (sinRoll * sinPitch - sinRoll * sinYaw * cosPitch - sinYaw * cosPitch);
        m_Velocity.Y = sinPitch * cosRoll;
        m_Velocity.Z = (sinRoll * sinPitch + sinRoll * cosYaw * cosPitch + cosYaw * cosPitch);
        if (m_Velocity.X == 0 && m_Velocity.Y == 0 && m_Velocity.Z == 0)
            m_Velocity = new Vector3(0f,0f,1f);
        m_Rotation = new Vector3((float)pitch, (float)yaw, (float)roll);

        m_Velocity.Normalize();
        //m_QRotation = Quaternion.CreateFromYawPitchRoll((float)yaw,(float)pitch,(float)roll); This line works for the most part but doesn't allow you to pitch up correctly while banking.
        m_QRotation = Quaternion.CreateFromAxisAngle(m_Velocity,(float)roll);
        m_Velocity = Vector3.Multiply(m_Velocity, shipSpeed);

        m_Position += m_Velocity;
    }

您是否坚持将船舶方向存储为偏航、俯仰和横摇变量?如果没有,将方向存储在四元数或矩阵中将大大简化您的挑战

在帧的末尾,您将发送effect.World矩阵,表示船舶的世界空间方向和位置。该矩阵的正向性质恰好与你在这里计算的速度向量相同。为什么不从effect.World中保存该矩阵,然后下一帧只需围绕其自身的正向向量(用于滚动)旋转该矩阵,并基于其自身的平移属性(正向属性*shipSpeed),将其发送回下一帧的效果。这将不允许您“知道”船舶的实际偏航角、俯仰角和横摇角。但在大多数情况下,在3d编程中,如果你认为你需要知道某个东西的角度,你可能会走错方向

下面是一个使用矩阵的片段,如果您愿意,可以让您朝这个方向前进

//class scope field
Matrix orientation = Matrix.Identity;


//in the update
Matrix changesThisFrame = Matrix.Identity;

//for yaw
changesThisFrame *= Matrix.CreatFromAxisAngle(orientation.Up, inputtedYawAmount);//consider tempering the inputted amounts by time since last update.

//for pitch
changesThisFrame *= Matrix.CreateFromAxisAngle(orientation.Right, inputtedPitchAmount);

//for roll
changesThisFrame *= Matrix.CreateFromAxisAngle(orientation.Forward, inputtedRollAmount);

orientation *= changesThisFrame;

orientation.Translation += orientation.Forward * shipSpeed;


//then, somewhere in the ship's draw call

effect.World = orientation;

想要使用XNA并在3D中旋转东西吗?然后读一读:

推荐还不够。

谢谢Seb,这是我的博客!=)有一天我写了这篇文章,惊讶地发现有那么多人觉得它很有用。我对托利盖伊问题的回答是对博客本质的一个微型版本。