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

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, when I rotate the object clockwise, the number I get from the y-axis (y in the vertical in Bullet) goes from 0 to -90 to 0 to 90, and finally returns to 0 for every quarter rotation. It's close, but I need it to go all the way from 0 to 360.

+3


source to share


2 answers


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 the Euler angles are ambiguous. have you tried solution 2?

+1


source


I had the same problem. I'm using LibGDX with Bullet engine, so my example code is in Java, but I'm sure it will work in C ++ too. Here is my solution (for the 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 defined, and using angle with gimble pole otherwise.
float rotation = (gimbalPole == 0) ? roll : angle * gimbalPole;

      



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

if (rotation < 0) rotation += 360;

      

0


source







All Articles