【问题标题】:Why does my object in OpenGL rotate in the opposite direction than expected using quaternions?为什么我在 OpenGL 中的对象旋转方向与使用四元数的预期相反?
【发布时间】:2021-07-27 18:25:54
【问题描述】:

我正在使用 OpenGL/GLUT 使用固定功能管道编写程序(我知道,我知道,这是大学)。我在其他实现和互联网的帮助下从头开始编写了 Quaternion 类,它基本上工作正常,但它沿每个轴的旋转方向与我认为的相反。

我曾想过将其发布在数学堆栈交换上,但鉴于它是 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),      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;
    }
}

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 },这是我想要的,但旋转仍然是顺时针,而不是逆时针。

【问题讨论】:

    标签: c++ matrix opengl rotation quaternions


    【解决方案1】:

    您在row-major order 中构建矩阵,但glMultMatrixf expects 是列主矩阵。转置一个旋转矩阵是equivalent to inverting它,即反向旋转。

    要修复它,可以按列优先顺序构建矩阵、转置它,或者使用glMultTransposeMatrixf

    【讨论】:

    • 谢谢你。在阅读了一篇关于它的数学堆栈交换的帖子后,我昨天尝试转置它,但显然做错了。你的回答让我再次探索,现在它工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多