我正在 XNA 中编写一个飞行模拟器程序(类似于 Star fox 64),但我的旋转出现问题。我的想法是有一个速度矢量和位置矢量。我允许的控件使得欧拉角会引起问题,所以我想使用四元数。
我对图形渲染还不是很熟悉,但我想我可以使用 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;
}