如何在子弹物理学中让刚体的欧拉旋转在 0 到 360 之间

How to get the Euler rotation of a rigid body between 0 to 360 in Bullet Physics?

本文关键字:旋转 之间 子弹 物理学      更新时间:2023-10-16

我目前正在尝试获取对象的旋转。我正在使用C++和子弹物理。这是我的代码:

btScalar x, y, z;
body[0]->getCenterOfMassTransform().getBasis().getEulerZYX(z, y, x);
但是,当我顺时

针旋转对象时,我从 y(y 在 Bullet 中是垂直的)轴获得的数字从 0 到 -90 到 0 到 90,最后每旋转四分之一回到 0。它很接近,但我需要的是让它从 0 一直到 360。

项目符号文档说:

void    getEulerZYX (btScalar &yaw, btScalar &pitch, btScalar &roll, unsigned int solution_number=1) const 

solution_number Which solution of two possible solutions ( 1 or 2) are possible values 

这是因为欧拉角是模棱两可的。 你试过解决方案 2 吗?

我遇到了同样的问题。我使用带有 Bullet 引擎的 LibGDX,所以我的代码示例在 Java 上,但我确信,它也适用于C++。这是我的解决方案(针对 Z 轴):

body.getWorldTransform().getRotation(mRotation);
// That gives you an angle in all range but excluding (85, 95) and (-95, 85). For other axis you can try to get Pitch or Yaw.
float roll = mRotation.getRoll();
// That gives you an angle in range [0, 240). Clockwise and counterclockwise directions isn't detected. 
float angle = mRotation.getAngleAround(0, 0, 1);
// Usually 0, but on (85, 95) and (-95, 85) becomes 1 and -1. 
int gimbalPole = mRotation.getGimbalPole();
// Using roll (pitch/yaw for other axis) if it's defined, and using angle with gimble pole otherwise.
float rotation = (gimbalPole == 0) ? roll : angle * gimbalPole;

获得的旋转将在(-180,180)范围内。它可以很容易地转换为[0, 360)范围:

if (rotation < 0) rotation += 360;