无法从偏航角超过 90 glm 的欧拉角创建 quat

Can't create a quat from euler angles with yaw above 90 glm

这里有一些背景: 我正在研究游戏引擎,最近添加了一个物理引擎(在本例中为 physx )。问题是我的变换 class 使用欧拉角进行旋转,而物理引擎的变换 class 使用欧拉角。所以我只是实现了一种方法来将我的变换 class 更改为物理引擎变换并返回。它运行良好,但我发现了一个奇怪的错误。

我得到的行为:

当旋转的 Yaw(欧拉向量的第二个元素)超过 90 度时,它不再使对象在 y 轴上旋转并开始摆弄俯仰和滚动(奇怪的摇晃跳过从 0 到 180 并返回很多)。 调试工具显示旋转不会超过 91,但会达到最大 90.0003 我确实将度数转换为弧度。 示例: 为了显示这个错误,我有一个带有 python 脚本旋转它的立方体:

from TOEngine import * 

class rotate:
    direction = vec3(0,10,0)
    def Start(self):
        pass
    def Update(self,deltaTime):
        transform.Rotate(self.direction*deltaTime*5)       
        pass

引擎本身是用 cpp 编写的,但我有一个与嵌入式 python 一起工作的脚本系统。 TOEngine 只是我的模块,脚本本身只是每帧旋转立方体。 立方体从 0 , 0 , 0 开始旋转并且旋转良好但停止并且 90 度偏航并开始摇晃。

只有在启用物理系统时才会发生这种情况,所以我知道错误一定是在使用 glm 将旋转从 euler 转移到 quat 并返回每一帧的方法中。

这是实际的有问题的代码

void RigidBody::SetTransform(Transform transform)
{
    glm::vec3 axis = transform.rotation;
    rigidbody->setGlobalPose(PxTransform(*(PxVec3*)&transform.position,*(PxQuat*)&glm::quat(glm::radians(transform.rotation))));//Attention Over Here
}

Transform RigidBody::GetTransform()
{
    auto t = rigidbody->getGlobalPose();
    return Transform(*(glm::vec3*)&t.p, glm::degrees(glm::eulerAngles(*(glm::quat*)&t.q)), entity->transform.scale);
}

避免奇怪的类型双关 PxQuat 与glm::quat 基本相同,PxVec3 与glm::vec3 基本相同。我希望这段代码能够在物理引擎变换 class 和我的变换 class 之间转换,方法是将旋转从欧拉角递减更改为带弧度的 quat( 困难的部分).

以及物理系统内部:

void PreUpdate(float deltaTime)override {   //Set Physics simulation changes to the scene
mScene->fetchResults(true);
for (auto entity : Events::scene->entities)
    for (auto component : entity->components)
        if (component->GetName() == "RigidBody")
            entity->transform = ((RigidBody*)component)->GetTransform();    //This is running on the cube entity
}
void PostUpdate(float deltaTime)override {  //Set Scene changes To Physics simulation
for (auto entity : Events::scene->entities)
    for (auto component : entity->components)
        if (component->GetName() == "RigidBody")
            ((RigidBody*)component)->SetTransform(entity->transform);//This is running on the cube entity
mScene->simulate(deltaTime);
} 

PreUpdate 在每帧更新之前运行 PostUpdate 在每帧更新之后运行。顾名思义,更新方法(在上面的脚本中显示)在更新时运行...(在 PreUpdate 和 PostUpdate 之间)。立方体有一个刚体组件。 我期望得到的: 一个旋转的立方体,当它达到偏航 90 度时不会停止旋转。

我知道这个有点复杂。我尽力解释了我认为问题出在将欧拉角更改为 quat 的错误。

关于从 PxQuatglm::quat 的转换,请阅读 https://en.cppreference.com/w/cpp/language/explicit_cast and https://en.cppreference.com/w/cpp/language/reinterpret_cast 上的文档并在 reinterpret_cast 上的页面中查找未定义的行为。据我所知,c 风格的转换不能保证有效,甚至不可取。虽然我在这一点上离题了,但请记住,您有两种转换选择。

glm::quat glmQuat = GenerateQuat();
physx::PxQuat someQuat = *(physx::PxQuat*)(&glmQuat); //< (1)
physx::PxQuat someOtherQuat = ConvertGlmQuatToPxQuat(glmQuat); //< (2)

(1) 此选项可能会导致未定义的行为,但更重要的是,您没有保存副本。该语句肯定会导致 1 次复制构造函数调用。

(2) 由于 return 值优化,此选项也将导致 physx::PxQuat.

的单个构造

所以实际上,通过选择选项 (1),您不会节省任何成本,但会冒未定义行为的风险。使用选项 (2),成本相同,但代码现在符合标准。现在回到原点。

我通常会尽我所能避免使用欧拉角,因为它们容易出错并且比四元数更容易混淆。这就是说这是一个简单的测试,你可以设置它来测试你从欧拉角的四元数转换(暂时让 physx 远离这个)。

您需要生成以下方法。

glm::mat3 CreateRotationMatrix(glm::vec3 rotationDegrees);
glm::mat3 CreateRotationMatrix(glm::quat inputQuat);
glm::quat ConvertEulerAnglesToQuat(glm::vec3 rotationDegrees);

然后您的测试伪代码如下所示。

for (auto angles : allPossibleAngleCombinations) {
    auto expectedRotationMatrix = CreateRotationMatrix(angles);
    auto convertedQuat = ConvertEulerAnglesToQuat(angles);
    auto actualRotationMatrix = CreateRotationMatrix(convertedQuat);
    ASSERT(expectedRotationMatrix, actualRotationMatrix);
}

只有你通过了这个测试,你才能看下一个将它们转换成PxQuat的问题。我猜这个测试对你来说会失败。我要给出的一个建议是输入角度之一(取决于惯例)需要限制范围。比如说,如果您将偏航角限制在 -90 到 90 度之间,那么您的测试很可能会成功。这是因为,欧拉角的非唯一组合可以产生相同的旋转矩阵。