为什么我的对象在 OpenGL 中的旋转方向与使用四元数时预期的方向相反?
Why does my object in OpenGL rotate in the opposite direction than expected using quaternions?
我正在用 OpenGL/GLUT 使用固定函数管道编写程序(我知道,我知道,这是大学)。在其他实现和互联网的帮助下,我从头开始编写了四元数 class,它基本上工作正常,但它沿每个轴以与我想象的相反的方向旋转。
我考虑过将它发布到 Math stack exchange 上,但考虑到它 OpenGL/GLUT 我认为在这里会更好地理解它。
下面的轴是:绿色 -> Y,红色 -> X,蓝色 -> Z。较暗的边是正方向。我稍微旋转了它以显示正 Z 轴。坐标轴为世界坐标轴。
我已经将按“a”定义为沿 Y 轴正向旋转。右手定则表明这是沿 Y 轴的逆时针旋转。图像显示了按下“a”后立方体的旋转。如您所见,它已顺时针旋转。每个轴都会发生这种情况。
我的四元数class:
#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), 0,
2*(X*Y + W*Z), 1 - 2*(X*X + Z*Z), 2*(Y*Z - W*X), 0,
2*(Z*X - W*Y), 2*(Y*Z + W*X), 1 - 2*(X*X + Y*Y), 0,
0, 0, 0, 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;
}
}
其中旋转是存储为四元数的对象的当前旋转,左乘所以它是局部坐标。
向量是:
Vector3D Vector3D::up() { return Vector3D(0, 1, 0); }
Vector3D Vector3D::right() { return Vector3D(1, 0, 0); }
Vector3D Vector3D::forward() { return Vector3D(0, 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, 0.918102), which agrees with this page 如果我将 y = 1 和角度(弧度)设置为 0.785398(45 度)。所以最后的旋转四元数是正确的,但是我的立方体还是旋转错了方向。这让我觉得我的代码有问题。
这是按住“a”直到顺时针旋转45度后的最终四元数旋转矩阵:
{0.705871, 0, 0.708341, 0, }
{0, 1, 0, 0, }
{-0.708341, 0, 0.705871, 0, }
{0, 0, 0, 1, }
根据同一个网站是{ [ 0, 1, 0 ], 45.1000806 }
,这是我想要的,但仍然是顺时针旋转,而不是逆时针旋转。
您在 row-major order, but glMultMatrixf
expects a column-major matrix. Transposing a rotation matrix is equivalent to inverting 中构建您的矩阵,即沿相反方向旋转。
要修复它,请按列优先顺序构建矩阵,转置它,或使用 glMultTransposeMatrixf
.
我正在用 OpenGL/GLUT 使用固定函数管道编写程序(我知道,我知道,这是大学)。在其他实现和互联网的帮助下,我从头开始编写了四元数 class,它基本上工作正常,但它沿每个轴以与我想象的相反的方向旋转。
我考虑过将它发布到 Math stack exchange 上,但考虑到它 OpenGL/GLUT 我认为在这里会更好地理解它。
下面的轴是:绿色 -> Y,红色 -> X,蓝色 -> Z。较暗的边是正方向。我稍微旋转了它以显示正 Z 轴。坐标轴为世界坐标轴。
我已经将按“a”定义为沿 Y 轴正向旋转。右手定则表明这是沿 Y 轴的逆时针旋转。图像显示了按下“a”后立方体的旋转。如您所见,它已顺时针旋转。每个轴都会发生这种情况。
我的四元数class:
#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), 0,
2*(X*Y + W*Z), 1 - 2*(X*X + Z*Z), 2*(Y*Z - W*X), 0,
2*(Z*X - W*Y), 2*(Y*Z + W*X), 1 - 2*(X*X + Y*Y), 0,
0, 0, 0, 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;
}
}
其中旋转是存储为四元数的对象的当前旋转,左乘所以它是局部坐标。
向量是:
Vector3D Vector3D::up() { return Vector3D(0, 1, 0); }
Vector3D Vector3D::right() { return Vector3D(1, 0, 0); }
Vector3D Vector3D::forward() { return Vector3D(0, 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, 0.918102), which agrees with this page 如果我将 y = 1 和角度(弧度)设置为 0.785398(45 度)。所以最后的旋转四元数是正确的,但是我的立方体还是旋转错了方向。这让我觉得我的代码有问题。
这是按住“a”直到顺时针旋转45度后的最终四元数旋转矩阵:
{0.705871, 0, 0.708341, 0, }
{0, 1, 0, 0, }
{-0.708341, 0, 0.705871, 0, }
{0, 0, 0, 1, }
根据同一个网站是{ [ 0, 1, 0 ], 45.1000806 }
,这是我想要的,但仍然是顺时针旋转,而不是逆时针旋转。
您在 row-major order, but glMultMatrixf
expects a column-major matrix. Transposing a rotation matrix is equivalent to inverting 中构建您的矩阵,即沿相反方向旋转。
要修复它,请按列优先顺序构建矩阵,转置它,或使用 glMultTransposeMatrixf
.