XNA 四元数.createFromAxisAngle
我正在 XNA 中编写一个飞行模拟器程序(类似于 Star Fox 64),但我的旋转遇到了问题。我的想法是有一个速度向量和位置向量。我允许的控制使得欧拉角度会引起问题,所以我想使用四元数。
我还不太熟悉图形渲染,但我认为我可以使用 Quaternion.createFromAxisAngle(vector, 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否坚持将船舶的方向存储为偏航、俯仰和航向?滚动变量?如果没有,将方向存储在四元数或矩阵中将显着简化您的挑战。
在帧的末尾,您将发送effect.World一个表示世界空间方向和方向的矩阵。船舶的位置。该矩阵的前向属性恰好与您在此处计算的速度向量相同。为什么不从effect.World中保存该矩阵,然后下一帧只需围绕它自己的前向向量(用于滚动)旋转该矩阵,并根据它自己的(前向属性*shipSpeed)推进它的平移属性,然后将其发送回下一帧的效果。这将不允许您“知道”实际的偏航、俯仰和倾斜角。你的船的横滚角。但在大多数情况下,在 3D 编程中,如果您认为需要了解某物的角度,那么您的做法可能是错误的。
如果您愿意,这里有一个使用矩阵的片段,可以帮助您朝这个方向前进。
Are you adamant about storing your ship's orientation as yaw, pitch, & roll variables? If not, storing the orientation in a quaternion or matrix will significantly simplify your challenge here.
At the end of the frame, you will send the effect.World a matrix representing the world space orientation & position of the ship. That matrix's forward property happens to be the same vector you calculated here as velocity. Why not save that matrix from the effect.World, then the next frame simply rotate that matrix about it's own forward vector (for roll) and advance it's Translation property based on it's own (forward property * shipSpeed), and sent it back to the effect for next frame. This will disallow you to "know" the actual yaw, pitch, & roll angles of your ship. But for the most part, in 3d programming, if you think you need to know the angle of something, you're probably going about it the wrong way.
Here's a snippet using a matrix to get you going in this direction if you want to.
想要使用 XNA 并以 3D 方式旋转物体?然后阅读以下内容: http://stevehazen.wordpress.com/2010/02/15/matrix-basics-how-to-step-away-from-storing-an-orientation-as-3-angles/
强烈推荐。
Want to work with XNA and rotate stuff in 3D? Then read this: http://stevehazen.wordpress.com/2010/02/15/matrix-basics-how-to-step-away-from-storing-an-orientation-as-3-angles/
Can not recommend it enough.