如何解决为什么我的对象在 OpenGL 中旋转的方向与使用四元数时预期的方向相反?
我正在使用固定函数管道编写带有 OpenGL/GLUT 的程序(我知道,我知道,这是大学)。我在其他实现和互联网的帮助下从头开始编写了 Quaternion 类,它基本上工作正常,但它沿每个轴的旋转方向与我认为的相反。
我曾考虑将其发布到 Math stack exchange 上,但鉴于它是 OpenGL/GLUT,我认为在这里会更好地理解。
下面的轴是:绿色 -> Y,红色 -> X,蓝色 -> Z。较暗的边是正方向。我已经旋转了一点以显示正 Z 轴。坐标轴是世界坐标轴。
我已将按下“a”定义为 Y 轴的正向旋转。右手法则指出这是 Y 轴的逆时针旋转。图像显示按“a”后立方体的旋转。如您所见,它已顺时针旋转。每个轴都会发生这种情况。
我的四元数类:
#define _USE_MATH_DEFINES
#include <cmath>
#include "Quaternion.h"
#include "Utility.h"
Quaternion::Quaternion() : X(0),Y(0),Z(0),W(1) {}
Quaternion::Quaternion(Vector3D axis,float angle) {
float mag = Vector3D::magnitude(axis);
angle = utility::toRadians(angle);
float sine = sinf(angle * 0.5f);
// Divide by magnitude for pure quaternion
X = axis.X * sine / mag;
Y = axis.Y * sine / mag;
Z = axis.Z * sine / mag;
W = cosf(angle * 0.5f);
}
Quaternion::Quaternion(float x,float y,float z,float w) :
X(x),Y(y),Z(z),W(w) {}
float Quaternion::magnitude(const Quaternion &q) {
return sqrtf(q.X * q.X + q.Y * q.Y + q.Z * q.Z + q.W * q.W);
}
Quaternion Quaternion::normalise(const Quaternion &q) {
float mag = magnitude(q);
return Quaternion(q.X / mag,q.Y / mag,q.Z / mag,q.W / mag);
}
std::array<float,16> Quaternion::toMatrix(const Quaternion& q) {
float X = q.X;
float Y = q.Y;
float Z = q.Z;
float W = q.W;
return {
1 - 2*(Z*Z + Y*Y),2*(X*Y - W*Z),2*(Z*X + W*Y),2*(X*Y + W*Z),1 - 2*(X*X + Z*Z),2*(Y*Z - W*X),2*(Z*X - W*Y),2*(Y*Z + W*X),1 - 2*(X*X + Y*Y),1
};
}
Quaternion Quaternion::conjugate(const Quaternion& q) {
return Quaternion(-q.X,-q.Y,-q.Z,q.W);
}
Quaternion operator*(Quaternion lhs,Quaternion rhs) {
return lhs *= rhs;
}
Quaternion& Quaternion::operator*=(const Quaternion& rhs) {
float rhsX = rhs.getX(),rhsY = rhs.getY(),rhsZ = rhs.getZ(),rhsW = rhs.getW();
Quaternion q;
q.X = W * rhsX + X * rhsW + Y * rhsZ - Z * rhsY;
q.Y = W * rhsY - X * rhsZ + Y * rhsW + Z * rhsX;
q.Z = W * rhsZ + X * rhsY - Y * rhsX + Z * rhsW;
q.W = W * rhsW - X * rhsX - Y * rhsY - Z * rhsZ;
*this = q;
return *this;
}
// Quaternion->Vector multiplication is not commutiative,must be Q*V
Vector3D operator*(Quaternion lhs,Vector3D rhs) {
Quaternion pure = Quaternion(rhs.X,rhs.Y,rhs.Z,0);
Quaternion right = pure * Quaternion::conjugate(lhs); // v * q-1
Quaternion left = lhs * right; // q * (v * q-1)
return Vector3D(left.getX(),left.getY(),left.getZ());
}
float Quaternion::getX() const { return X; }
float Quaternion::getY() const { return Y; }
float Quaternion::getZ() const { return Z; }
float Quaternion::getW() const { return W; }
std::ostream& operator<<(std::ostream& ostream,Quaternion& q)
{
ostream << q.X << " " << q.Y << " "
<< q.Z << " " << q.W << " " << std::endl;
return ostream;
}
按“a”(和类似的)调用:
void GameManager::handleKeyboardInput() {
// ...
if (keyboard->isPressed('a')) {
ship->rotate(Axis::y,Direction::positive,dt);
}
// ...
glutPostRedisplay();
}
当对象在显示函数中更新自身时,调用:
void Ship::rotate(const Axis axis,const Direction direction,const float dt) {
int sign = direction == Direction::positive ? 1 : -1;
float speed = sign * ROTATION_SPEED;
if (axis == Axis::x) {
rotation = Quaternion(Vector3D::right(),speed * dt) * rotation;
}
else if (axis == Axis::y) {
rotation = Quaternion(Vector3D::up(),speed * dt) * rotation;
}
else if (axis == Axis::z) {
rotation = Quaternion(Vector3D::forward(),speed * dt) * rotation;
}
}
其中,rotation 是存储为四元数的对象当前旋转,左乘后为局部坐标。
向量是:
Vector3D Vector3D::up() { return Vector3D(0,1,0); }
Vector3D Vector3D::right() { return Vector3D(1,0); }
Vector3D Vector3D::forward() { return Vector3D(0,1); }
最后在主显示循环中绘制对象:
glPushMatrix();
glMultMatrixf(Quaternion::toMatrix(rotation).data());
glColor3f(1.0,1.0,1.0);
glutWireCube(8);
glPopMatrix();
简单的解决方案是翻转我的旋转符号,但我更想知道哪里出了问题。谢谢。
编辑:对于它的价值,我可以确认,如果我按住“a”使立方体旋转 45 度(因此比第二个图像旋转得更多),我的立方体的旋转(x、y、z、 w) = (0,0.389125,0.918102),which agrees with this page 如果我将 y = 1 和角度(弧度)设置为 0.785398(45 度)。所以最终的旋转四元数是正确的,但我的立方体仍然以错误的方向旋转。这让我觉得我的代码有问题。
这是按住“a”直到顺时针旋转45度后的最终四元数旋转矩阵:
{0.705871,0.708341,}
{0,}
{-0.708341,0.705871,}
根据同一个网站是{ [ 0,0 ],45.1000806 }
,这是我想要的,但仍然是顺时针旋转,而不是逆时针。
解决方法
您在 row-major order 中构建矩阵,但 glMultMatrixf
expects 是列主矩阵。转置一个旋转矩阵就是equivalent to inverting,即向相反方向旋转。
要解决此问题,请按列主序构建矩阵、转置它或使用 glMultTransposeMatrixf
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。