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

如何解决为什么我的对象在 OpenGL 中旋转的方向与使用四元数时预期的方向相反?

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

我曾考虑将其发布到 Math stack exchange 上,但鉴于它是 OpenGL/GLUT,我认为在这里会更好地理解。

下面的轴是:绿色 -> Y,红色 -> X,蓝色 -> Z。较暗的边是正方向。我已经旋转了一点以显示正 Z 轴。坐标轴是世界坐标轴。

enter image description here

我已将按下“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 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res