Weird Rotation values updating from bullet

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
QProgrammer
Posts: 11
Joined: Sat Jun 14, 2014 12:06 pm

Weird Rotation values updating from bullet

Post by QProgrammer »

Hello

I am using Bullet physic in my simulation.
I have a cylinder at pos(0,0,0) and rotation(0,0,0)

Then i apply an angular velocity to Cylinder to make it rotating over Y axis :

Code: Select all

 
btScalar AngVelocity = 1;
CylinderRigidBody->setAngularVelocity(btVector3(0,AngVelocity,0));
 
Everything is fine and cylinder rotates as expected.

Problem is this :

I expect to cylender rotation change like this :
(0,1,0) > (0,2,0) > ....... > (0,90,0) > ( 0,91,0 ) > (0,92,0) > (0,93,0) > .....>(0,180,0)....... (0,269,0 ) > (0,270,0)

But, what i get is like this :
(0,0,0) > (0,2,0) > ....... > (0,90,0) > (180,89,180) > (180,88,180 ) >(180,87,180)>.... (180,0,180)>.....(180,-89,180)->(0,-90,0)->(0,-89,0)....

For example instead of (0,91,0) , node rotation is (180,89,180) which make no difference in rendering time, but it cause some Irrlitch functions not to work correctly. (for example RotationToDirection())


This difference happens when i update nodes rotation on scene according to Bullet rigidbodu transformation

I tried these two methods :

Code: Select all

1-
    vector3df Euler;
    const btQuaternion& TQuat = TObject->getOrientation();  
    quaternion q(TQuat.getX(), TQuat.getY(), TQuat.getZ(), TQuat.getW());
    q.toEuler(Euler);
    Euler *= RADTODEG;  
    Node->setRotation(Euler);
 
2-
       irr::core::matrix4 mat;
       irr::f32 * ptr;
       ptr = mat.pointer();
       tr.getOpenGLMatrix(ptr);
       Node->setRotation(mat.getRotationDegrees());

Both of them are working good and i get correct physic simulation in my scene, but Rotation vector is not as simple as i expected.
For example instead of (0,269,0 ) it returns (180,-89,180)


So, is there any solution to update node rotation in a better manner? or simplify Bullet rotation values?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Weird Rotation values updating from bullet

Post by mongoose7 »

So, there's no problem?

The 'w' in a quaternion is a double angle, so the actual angle returned will be in [-90, 90].
kh_
Posts: 78
Joined: Fri May 19, 2006 4:29 pm

Re: Weird Rotation values updating from bullet

Post by kh_ »

Have you seen this before? It is from some of the bullet examples posted (not sure who gets the credit)

/// Converts a Bullet quaternion to an Irrlicht euler angle
void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler)
{
btScalar W = TQuat.getW();
btScalar X = TQuat.getX();
btScalar Y = TQuat.getY();
btScalar Z = TQuat.getZ();
float WSquared = W * W;
float XSquared = X * X;
float YSquared = Y * Y;
float ZSquared = Z * Z;

TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared));
TEuler.setY(asinf(-2.0f * (X * Z - Y * W)));
TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared));
TEuler *= core::RADTODEG;
}


worked for me
QProgrammer
Posts: 11
Joined: Sat Jun 14, 2014 12:06 pm

Re: Weird Rotation values updating from bullet

Post by QProgrammer »

KH wrote:Have you seen this before? It is from some of the bullet examples posted (not sure who gets the credit)

/// Converts a Bullet quaternion to an Irrlicht euler angle
void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler)
{
btScalar W = TQuat.getW();
btScalar X = TQuat.getX();
btScalar Y = TQuat.getY();
btScalar Z = TQuat.getZ();
float WSquared = W * W;
float XSquared = X * X;
float YSquared = Y * Y;
float ZSquared = Z * Z;

TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared));
TEuler.setY(asinf(-2.0f * (X * Z - Y * W)));
TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared));
TEuler *= core::RADTODEG;
}


worked for me

Yes, that's the function i am using. I think should be a way to convert Quaternion To Euler with a normal result between 0-360.
That function returns (180,-89,180) as result for a simple rotation of (0,269,0 ).
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Weird Rotation values updating from bullet

Post by mongoose7 »

I shouldn't post twice but ...

What do you mean "normal"? I have already told you that quaternions use a double angle. So 2*theta is in [-180, 180]. Therefore theta is in [-90, 90]. If you want it elsewhere, make the change yourself.

From the purist point of view, quaternions are "normal" and Euler angles are weird. Anyway, look up the definition of Euler angles and you should see that you are not even using them. You're using (Z, X, Y) but Euler angles use something like (X, Y, X).
Post Reply