Problem with Matrix, Roll, Pitch and Yaw

danisegarra
Posts: 4
Joined: Mon Nov 02, 2009 4:23 pm

Problem with Matrix, Roll, Pitch and Yaw

Post by danisegarra »

Hello:
I'm new working with Bullet Engine.
Actually I'm programing an iPhone game using SIO2 Engine that uses Bullt Engine for physics.

I have a planet (physic object centered in 0,0,0) that I am moving applying torques and I want to put a light that ever iluminate the same face of the planet.
To calculate the position of the light, I obtain the rotations of the planet of this way:

Code: Select all

btTransform _btTransform;
_btTransform.setIdentity();
_btTransform.setFromOpenGLMatrix( _SIO2object->_SIO2transform->mat ); 
btMatrix3x3 mat = btMatrix3x3(_btTransform.getRotation());
btScalar Yaw, Pitch, Roll;
mat.getEulerYPR(Yaw, Pitch, Roll);
and after this, I obtain the transformation matrix multiplying

Code: Select all

M1*M2*M3*M4
where
M1 = ( 1, 0, 0, 0
            0, cos(Roll), -sin(Roll), 0
            0, sin(Roll),  cos(Roll), 0
            0, 0, 0, 1 )

M2 = ( cos(Pitch), 0, sin(Pitch), 0
            0, 1, 0, 0
           -sin(Pitch), 0, cos(Pitch), 0
            0, 0, 0, 1 )

M3 = ( cos(Yaw), -sin(Yaw), 0, 0
            sin(Yaw), cos(Yaw), 0, 0
            0, 0, 1, 0
            0, 0, 0, 1 )

M4 = ( lightPosition.x
           lightPosition.y
           lightPosition.z )
I apply the position of the matrix result to the light.

The obtained result works great in a lot of cases ever iluminating the same face of the planet, but if I rotate the planet enough to turn it upside down the light goes crazy. When the planet is upside down, when the light should be moving left it's moving right, and when the light should be moving up it's moving down.
I am pretty sure that the matrix are the correct ones (I don't multiply for the position of the planet because it's centered in 0,0,0).
I don't know if the problem is about the transformation matrix, the Roll, Pitch and Yaw values, the sin and cos results or I forbid something. :cry:
Anyone have an idea?
Thank you so much!

Sorry for my bad explanation :oops:
Mattg
Posts: 12
Joined: Thu Oct 22, 2009 12:50 am

Re: Problem with Matrix, Roll, Pitch and Yaw

Post by Mattg »

I think you could just do this to get what you want.

Code: Select all

btTransform _btTransform;
_btTransform.setIdentity();
_btTransform.setFromOpenGLMatrix( _SIO2object->_SIO2transform->mat );
btVector3 lightPosition( lightposx, lightposy, lightposz);
btVector3 worldLightPosition = _btTransform.getRotation() * lightPosition;
I'd avoid euler angles if I could, I think the problem with your code could be that the angles are applied in the wrong order.
danisegarra
Posts: 4
Joined: Mon Nov 02, 2009 4:23 pm

Re: Problem with Matrix, Roll, Pitch and Yaw

Post by danisegarra »

Thank you so much, Mattg!
It works great.