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

半世苍凉 提交于 2019-12-31 02:29:08

问题


I am currently trying to get the rotation of an object. I am using C++ and Bullet Physics. This is my code:

btScalar x, y, z;
body[0]->getCenterOfMassTransform().getBasis().getEulerZYX(z, y, x);

However, as I rotate the object around clockwise the number I get from the y (y is vertical in Bullet) axis goes from 0 to -90 to 0 to 90 and finally back to 0 for every quarter rotation. It is close but what I need is for it to go all the way from 0 to 360.


回答1:


Bullet documentation says:

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

and

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

this is because euler angles are ambigous. have you tried solution 2?




回答2:


I had the same problem. I using LibGDX with Bullet engine, so my code sample on Java, but I'm sure, that it will works on C++ too. Here is my solution (for Z axis):

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;

Obtained rotation will be in range (-180, 180). It can be easily converted to [0, 360) range:

if (rotation < 0) rotation += 360;


来源:https://stackoverflow.com/questions/29869130/how-to-get-the-euler-rotation-of-a-rigid-body-between-0-to-360-in-bullet-physics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!