Gar*_*ckW 18 c++ opengl 3d quaternions sfml
所以我正在编写一个程序,其中对象围绕着spaceim风格移动,以便学习如何在3D空间中平滑地移动物体.在稍微弄清楚欧拉角度之后,看起来它们并不适合任意方向的自由形式3D运动,所以我决定继续寻找最适合这项工作的东西 - 四元数.我打算让对象始终围绕其局部XYZ轴旋转,而不是围绕全局XYZ轴旋转.
我试图使用四元数实现旋转系统,但有些东西不起作用.沿单轴旋转物体时,如果没有进行先前的旋转,则物体沿给定轴旋转精细.但是,当执行一个接一个的旋转后,第二个旋转并不总是沿着它应该旋转的局部轴 - 例如,绕Z轴旋转约90°后,围绕Y轴旋转仍然发生在全局Y轴周围,而不是与全局X轴对齐的新的局部Y轴.
呵呵.让我们一步一步来看看.错误必须在某处.
第1步 - 捕获输入
我认为最好使用Euler角度(或Pitch-Yaw-Roll方案)来捕捉玩家输入.此时,箭头键控制俯仰和偏航,而Q和E控制滚动.我捕获了播放器输入(我正在使用SFML 1.6):
///SPEEDS
float ForwardSpeed = 0.05;
float TurnSpeed = 0.5;
//Rotation
sf::Vector3<float> Rotation;
Rotation.x = 0;
Rotation.y = 0;
Rotation.z = 0;
//PITCH
if (m_pApp->GetInput().IsKeyDown(sf::Key::Up) == true)
{
Rotation.x -= TurnSpeed;
}
if (m_pApp->GetInput().IsKeyDown(sf::Key::Down) == true)
{
Rotation.x += TurnSpeed;
}
//YAW
if (m_pApp->GetInput().IsKeyDown(sf::Key::Left) == true)
{
Rotation.y -= TurnSpeed;
}
if (m_pApp->GetInput().IsKeyDown(sf::Key::Right) == true)
{
Rotation.y += TurnSpeed;
}
//ROLL
if (m_pApp->GetInput().IsKeyDown(sf::Key::Q) == true)
{
Rotation.z -= TurnSpeed;
}
if (m_pApp->GetInput().IsKeyDown(sf::Key::E) == true)
{
Rotation.z += TurnSpeed;
}
//Translation
sf::Vector3<float> Translation;
Translation.x = 0;
Translation.y = 0;
Translation.z = 0;
//Move the entity
if (Rotation.x != 0 ||
Rotation.y != 0 ||
Rotation.z != 0)
{
m_Entity->ApplyForce(Translation, Rotation);
}
Run Code Online (Sandbox Code Playgroud)
m_Entity是我想要旋转的东西.它还包含表示对象旋转的四元数和旋转矩阵.
第2步 - 更新四元数
我不是100%确定这是它应该完成的方式,但这是我在Entity :: ApplyForce()中尝试做的事情:
//Rotation
m_Rotation.x += Rotation.x;
m_Rotation.y += Rotation.y;
m_Rotation.z += Rotation.z;
//Multiply the new Quaternion by the current one.
m_qRotation = Quaternion(m_Rotation.x, m_Rotation.y, m_Rotation.z);// * m_qRotation;
m_qRotation.RotationMatrix(m_RotationMatrix);
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我不确定是否最好从更新的Euler角度构建新的四元数,或者我是否应该将表示更改的四元数乘以表示整体当前旋转的四元数,这是印象我在阅读本指南时得到了.如果是后者,我的代码看起来像这样:
//Multiply the new Quaternion by the current one.
m_qRotation = Quaternion(Rotation.x, Rotation.y, Rotation.z) * m_qRotation;
Run Code Online (Sandbox Code Playgroud)
m_Rotation是以PYR格式存储的对象的当前旋转; 轮换是球员输入所要求的变化.无论哪种方式,问题可能出在我的Quaternion类的实现中.这是整个事情:
Quaternion::Quaternion(float Pitch, float Yaw, float Roll)
{
float Pi = 4 * atan(1);
//Set the values, which came in degrees, to radians for C++ trig functions
float rYaw = Yaw * Pi / 180;
float rPitch = Pitch * Pi / 180;
float rRoll = Roll * Pi / 180;
//Components
float C1 = cos(rYaw / 2);
float C2 = cos(rPitch / 2);
float C3 = cos(rRoll / 2);
float S1 = sin(rYaw / 2);
float S2 = sin(rPitch / 2);
float S3 = sin(rRoll / 2);
//Create the final values
a = ((C1 * C2 * C3) - (S1 * S2 * S3));
x = (S1 * S2 * C3) + (C1 * C2 * S3);
y = (S1 * C2 * C3) + (C1 * S2 * S3);
z = (C1 * S2 * C3) - (S1 * C2 * S3);
}
//Overload the multiplier operator
Quaternion Quaternion::operator* (Quaternion OtherQuat)
{
float A = (OtherQuat.a * a) - (OtherQuat.x * x) - (OtherQuat.y * y) - (OtherQuat.z * z);
float X = (OtherQuat.a * x) + (OtherQuat.x * a) + (OtherQuat.y * z) - (OtherQuat.z * y);
float Y = (OtherQuat.a * y) - (OtherQuat.x * z) - (OtherQuat.y * a) - (OtherQuat.z * x);
float Z = (OtherQuat.a * z) - (OtherQuat.x * y) - (OtherQuat.y * x) - (OtherQuat.z * a);
Quaternion NewQuat = Quaternion(0, 0, 0);
NewQuat.a = A;
NewQuat.x = X;
NewQuat.y = Y;
NewQuat.z = Z;
return NewQuat;
}
//Calculates a rotation matrix and fills Matrix with it
void Quaternion::RotationMatrix(GLfloat* Matrix)
{
//Column 1
Matrix[0] = (a*a) + (x*x) - (y*y) - (z*z);
Matrix[1] = (2*x*y) + (2*a*z);
Matrix[2] = (2*x*z) - (2*a*y);
Matrix[3] = 0;
//Column 2
Matrix[4] = (2*x*y) - (2*a*z);
Matrix[5] = (a*a) - (x*x) + (y*y) - (z*z);
Matrix[6] = (2*y*z) + (2*a*x);
Matrix[7] = 0;
//Column 3
Matrix[8] = (2*x*z) + (2*a*y);
Matrix[9] = (2*y*z) - (2*a*x);
Matrix[10] = (a*a) - (x*x) - (y*y) + (z*z);
Matrix[11] = 0;
//Column 4
Matrix[12] = 0;
Matrix[13] = 0;
Matrix[14] = 0;
Matrix[15] = 1;
}
Run Code Online (Sandbox Code Playgroud)
那里可能有些东西可以让别人比我更畏缩,但我看不到它.为了从欧拉角转换为四元数,我根据这个来源使用了"第一种方法" ,这也似乎表明方程自动创建一个单位四元数("明确标准化").为了乘以四元数,我再次使用了这个C++指南.
第3步 - 从四元数中导出旋转矩阵
一旦完成,按照R. Martinho Fernandes对这个问题的回答,我尝试从四元数中构建一个旋转矩阵,并使用它来更新我的对象的旋转,使用上面的Quaternion :: RotationMatrix()代码,如下所示:
m_qRotation.RotationMatrix(m_RotationMatrix);
Run Code Online (Sandbox Code Playgroud)
我应该注意m_RotationMatrix GLfloat m_RotationMatrix[16],根据glMultMatrix 的必需参数,我相信我以后应该在显示对象时使用.它被初始化为:
m_RotationMatrix = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
Run Code Online (Sandbox Code Playgroud)
我认为是"中性"OpenGL旋转矩阵(每4个值一起代表一列,对吗?再次,我从glMultMatrix页面得到这个).
第4步 - 显示!
最后,我们得到函数运行每个循环的对象应该显示它.
glPushMatrix();
glTranslatef(m_Position.x, m_Position.y, m_Position.z);
glMultMatrixf(m_RotationMatrix);
//glRotatef(m_Rotation.y, 0.0, 1.0, 0.0);
//glRotatef(m_Rotation.z, 0.0, 0.0, 1.0);
//glRotatef(m_Rotation.x, 1.0, 0.0, 0.0);
//glRotatef(m_qRotation.a, m_qRotation.x, m_qRotation.y, m_qRotation.z);
//[...] various code displaying the object's VBO
glPopMatrix();
Run Code Online (Sandbox Code Playgroud)
评论说,我已经离开了我先前在那里失败的尝试.
结论 - 悲伤的熊猫
这是玩家输入生命周期的结论,从摇篮到OpenGL管理的坟墓.
我显然不理解某些东西,因为我得到的行为不是我想要或期望的行为.但是我对矩阵数学或四元数并不是特别有经验,所以我没有必要的洞察力来查看错误.
有人可以帮帮我吗?
Nic*_*las 23
你所做的就是用四元数有效地实现欧拉角.那没有用.
欧拉角的问题在于,当你计算矩阵时,每个角都是相对于它前面的矩阵的旋转.你想要的是获取一个物体的当前方向,并沿某个轴应用一个旋转,产生一个新的方向.
你不能用欧拉角来做到这一点.您可以使用矩阵,也可以使用四元数(因为它们只是矩阵的旋转部分).但你不能假装它们是欧拉角.
这是由不存储角度,在完成所有.相反,您只需要一个四元数来表示对象的当前方向.当您决定对其应用旋转(某个轴的某个角度)时,您可以构造一个四元数,表示该旋转绕该轴旋转一个角度.然后右键将该四元数与当前方向四元数相乘,生成新的当前方向.
渲染对象时,使用当前方向作为方向.