繁体   English   中英

为什么我在 OpenGL 中的 object 的旋转方向与使用四元数时预期的相反?

[英]Why does my object in OpenGL rotate in the opposite direction than expected using quaternions?

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

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

其中,当 object 在显示 function 中更新自身时,调用:

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); }

最后在主显示循环中绘制 object:

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

您以行优先顺序构建矩阵,但glMultMatrixf需要一个列优先矩阵。 转置一个旋转矩阵相当于反转它,即沿相反方向旋转。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM