我尝试使用四元数实现旋转系统,但某些事情并没有起作用。当沿单个轴旋转物体时,如果没有进行先前的旋转,则物体沿着给定轴线很好地旋转。然而,在执行一次旋转之后再应用另一次旋转时,第二次旋转并不总是沿着它应该旋转的本地轴线进行-例如,在Z轴周围旋转约90°之后,绕Y轴的旋转仍然发生在全局Y轴周围,而不是与全局X轴对齐的新本地Y轴周围。
哦。那么让我们一步一步地进行。错误一定在这里某处。 步骤1-捕获输入
我认为最好使用欧拉角(或俯仰-偏航-翻滚方案)来捕获玩家输入。目前,箭头键控制俯仰和偏航,而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);
}
m_Entity 是我尝试旋转的物体。它还包含表示对象旋转的四元数和旋转矩阵。
第2步 - 更新四元数
我不确定这是否是应该这样做的方式,但这就是我在 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);
正如您所看到的,我不确定是最好只从更新的欧拉角构建一个新的四元数,还是应该将表示变化的四元数与表示整体当前旋转的四元数相乘,这是我在阅读this guide时得到的印象。如果是后者,我的代码将如下所示:
//Multiply the new Quaternion by the current one.
m_qRotation = Quaternion(Rotation.x, Rotation.y, Rotation.z) * m_qRotation;
m_Rotation 是对象当前以 PYR 格式存储的旋转;Rotation 是由玩家输入所需的变化。无论如何,问题可能在于我的四元数类的实现。以下是整个内容:
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;
}
里面可能有一些让比我聪明的人感到不舒服的东西,但我看不出来。对于将欧拉角转换为四元数,我使用了this source中的“第一种方法”,该方法似乎还表明该方程式会自动创建一个单位四元数(“显然归一化”)。对于乘法四元数,我再次参考了this C++ guide。
步骤3 - 从四元数中导出旋转矩阵
完成上述步骤后,根据R. Martinho Fernandes在this question中的回答,我尝试从四元数构建旋转矩阵,并使用该矩阵更新对象的旋转,使用上述Quaternion :: RotationMatrix()代码在以下行中:
m_qRotation.RotationMatrix(m_RotationMatrix);
我应该注意到,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};
我相信这是“中性”的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();
我已经把之前失败的尝试注释掉了。
结论 - 悲伤的熊猫
这就是玩家输入生命周期的结论,从摇篮到OpenGL管理的坟墓。
显然我没有理解某些东西,因为我得到的行为不是我想要或期望的行为。但我对矩阵数学或四元数并不是特别有经验,所以我没有必要的洞察力来看到我的错误。
有人能帮帮我吗?