四元数的摄像头。我如何让它正常旋转? [英] Quaternion camera. How do I make it rotate correctly?
问题描述
我有一个3D摄像头,作为四元数存储当前的旋转,我无法正确地转动它。我希望相机逐渐围绕其局部轴基于鼠标移动每帧(第一人称射击风格)旋转,而旋转是错误的。那种它的工作原理,但相机似乎滚围绕其轴线向前当它不应该。
I have a 3D camera with its current rotation stored as a quaternion, and I'm having trouble rotating it correctly. I want the camera to rotate incrementally around its local axes based on mouse movement each frame (first-person-shooter-style), but the rotation is wrong. It kind of works, but the camera seems to "roll" around its forward axis when it shouldn't.
我更新了旋转的每一帧具有这种功能:
I update the rotation each frame with this function:
void Camera::rotate(const Quat& q)
{
// m_rot is the current rotation
m_rot = m_rot * q;
}
下面是我的四元数乘法函数:
Here's my quaternion multiplication function:
Quat Quat::operator*(const Quat &rhs) const
{
// quaternion elements in w,x,y,z order
Vector4d res;
res[0] = m_q[0]*rhs.m_q[0] - m_q[1]*rhs.m_q[1] -
m_q[2]*rhs.m_q[2] - m_q[3]*rhs.m_q[3];
res[1] = m_q[0]*rhs.m_q[1] + m_q[1]*rhs.m_q[0] +
m_q[2]*rhs.m_q[3] - m_q[3]*rhs.m_q[2];
res[2] = m_q[0]*rhs.m_q[2] - m_q[1]*rhs.m_q[3] +
m_q[2]*rhs.m_q[0] + m_q[3]*rhs.m_q[1];
res[3] = m_q[0]*rhs.m_q[3] + m_q[1]*rhs.m_q[2] -
m_q[2]*rhs.m_q[1] + m_q[3]*rhs.m_q[0];
return Quat(res);
}
难道我做错了什么,或者这是某种形式的浮点错误的事情吗?
Am I doing something wrong, or is this some kind of floating-point error thing?
推荐答案
想通了这个问题。对于鼠标controled第一人称的相机像一个我要为我想绕本地x轴看上下,但全球Y轴看一边到另一边。
Figured out the problem. For a mouse-controled first-person camera like the one I'm going for, I want to rotate around the local x-axis to look up and down, but the global y-axis for looking side to side.
因此,这是正确的x轴:
So this is correct for the x-axis:
m_rot = m_rot * q;
不过,我需要做的Y轴:
But I need to do this for the y-axis:
m_rot = d * m_rot;
这篇关于四元数的摄像头。我如何让它正常旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!