无法在偏航角度超过90 glm的情况下从欧拉角创建一个quat

问题描述 投票:0回答:1

这里有一些背景知识:我正在研究游戏引擎,最近我添加了一个物理引擎(在这种情况下是physx)。问题是我的变换类使用欧拉角进行旋转,物理引擎的变换类使用欧拉角。所以我刚刚实现了一个方法来将我的变换类改为物理引擎变换并返回。它工作得很好,但我发现了一个奇怪的错误。

我得到的行为:

当旋转的偏航(欧拉矢量的第二个元素)高于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度偏转并开始摇动。

这只在物理系统启用时才会发生,因此我知道错误必须在方法中将旋转从euler转移到quat并使用glm返回每一帧。

这是实际有问题的代码:

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

避免奇怪的类型PINGQuat与glm :: quat基本相同,而PxVec3与glm :: vec3基本相同。我希望这个代码能够在物理学的引擎变换类和我的变换类之间进行转换,方法是将旋转角从degu更改为带弧度的四元组(硬部分)。

而物理系统内部:

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度时不会停止旋转。

我知道这个有点复杂。我尽力解释这个错误我认为问题在于将欧拉角改为四分之一。

python c++ rotation glm-math physx
1个回答
0
投票

关于从PxQuatglm::quat的转换,请阅读https://en.cppreference.com/w/cpp/language/explicit_casthttps://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)由于返回值优化,该选项也将导致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°之间,你的测试可能会成功。这是因为,存在可以产生相同旋转矩阵的非唯一的欧拉角组合。

© www.soinside.com 2019 - 2024. All rights reserved.