Converting matrix to vectors

View previous topic View next topic Go down

Converting matrix to vectors

Post  cgen2 on Fri May 22, 2009 7:41 pm

How do I convert an OpenGl matrix to vectors?

I am using Bullet (sweet) in my render routine fetching the matrix like so :
Code:

sio2PhysicRender( sio2->_SIO2physic, sio2->_SIO2window->d_time, 1 );         
sio2TransformBindMatrix( _myObject->_SIO2transform );
_myObject->_btRigidBody->getWorldTransform().getOpenGLMatrix( _myObject->_SIO2transform->mat );

I want to update the SIO2transform so I have a current vector based transform.

I have extracted pitch and roll angle from the matrix but matrices are hard work, vectors would be much easier.

Any ideas gratefully accepted ....



[code]

cgen2

Posts : 38
Join date : 2008-12-14

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  sio2interactive on Fri May 22, 2009 8:18 pm

The sio2ObjectRender already update the _SIO2transform->loc for you... scale is not supported in Bullet and for the rotation you just have to convert euler to angle.

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Converting matrix to vectors

Post  cgen2 on Fri May 22, 2009 8:50 pm

But of course SIO2 has done the hard work already. I should have known. Many thanks

cgen2

Posts : 38
Join date : 2008-12-14

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  cgen2 on Fri May 22, 2009 10:08 pm

But wait, I don't need location (yet) so sio2ObjectRender doesn't provide anything I don't have with my existing code. I presume I must convert from the OpenGL matrix to get the rotation angles; there is no rotation Euler available?

So there is no easy way. I have to calculate the angles ... which I've done ... but it looks ugly.

C'est la guerre

cgen2

Posts : 38
Join date : 2008-12-14

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  cgen2 on Fri May 22, 2009 10:37 pm

Here's the ugliness I mentioned:
Code:
// Calculate angles from matrix
   angle_y = -asinf( _myObject->_SIO2transform->mat[ 2 ] );   // Calculate Y-axis angle
   tmpc = cosf( angle_y );
   if( _myObject->_SIO2transform->mat[ 0 ] < 0 )                        // Quadrant 2 & 3
   {
       if( _myObject->_SIO2transform->mat[ 2 ] > 0 )
      angle_y = -acosf( _myObject->_SIO2transform->mat[ 2 ] ) - DEGREES90_IN_RAD;   // Quadrant 3
       else
      angle_y = acosf( - _myObject->_SIO2transform->mat[ 2 ] ) + DEGREES90_IN_RAD;   // Quadrant 2               
   }
   angle_y *= RAD_2_DEGREES;

   if( fabs( tmpc ) > 0.005 )                           // Gimball lock?
   {
   // No, so get X-axis angle
   angle_x  = atan2( - _myObject->_SIO2transform->mat[ 6 ]  / tmpc,
                _myAObject->_SIO2transform->mat[ 10 ] / tmpc ) * RAD_2_DEGREES;
               
   angle_z = atan2( - _myObject->_SIO2transform->mat[ 1 ] / tmpc,
               _myObject->_SIO2transform->mat[ 0 ] / tmpc ) * RAD_2_DEGREES;
   }
   else                                          // Gimball lock has occurred
   {
      angle_x = 0.0;                                 // Set X-axis angle to zero & calculate Z-axis angle
      angle_z = atan2( _myObject->_SIO2transform->mat[ 4 ],
               _myObject->_SIO2transform->mat[ 5 ] ) * RAD_2_DEGREES;
   }

Now if there's a better way I'd like to hear it.

cgen2

Posts : 38
Join date : 2008-12-14

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  oioioi on Sat May 23, 2009 10:37 am

_myObject->_SIO2Transform->rot->z
rot is a vec3

oioioi

Posts : 136
Join date : 2008-12-02
Location : Norway

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  cgen2 on Sat May 23, 2009 3:07 pm

But _myObject->_SIO2Transform->rot vector is not updated by eg sio2ObjectRender, just _SIO2Transform->loc. I need the vector (and rotation angles) for calculating the forces and velocity on my object.

cgen2

Posts : 38
Join date : 2008-12-14

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  sio2interactive on Sat May 23, 2009 4:55 pm

cgen2: Are you sure that the code above work? Anyway since bullet send you an un-scaled matrix I think that the following code would work:

_SIO2transform->rot->x = acosf( _SIO2transform->mat[ 0 ] ) / SIO2_PI * 180.0f;
_SIO2transform->rot->y = acosf( _SIO2transform->mat[ 5 ] ) / SIO2_PI * 180.0f;
_SIO2transform->rot->z = acosf( _SIO2transform->mat[ 10 ] ) / SIO2_PI * 180.0f;

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Converting matrix to vectors

Post  cgen2 on Sun May 24, 2009 12:26 am

SIO2: I was sure until I looked at the delta with your suggestion. The deltas are significant (> 0.5 degrees at some angles). I didn't calc the absolute error but my ugly complex routine is probably guilty. It certainly has more scope for error.

I ever so slightly enhanced your suggestion removing the run time divide by substituting SIO2_RAD_TO_DEG for / SIO2_PI * 180.0f. Smile

Now I need to make this work over a full rotation, +/- 180 degrees ...

cgen2

Posts : 38
Join date : 2008-12-14

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  cgen2 on Sun May 24, 2009 5:47 am

Taking the SIO2 suggestion a little further I propose to use code something like this to extract pitch, roll and yaw angles over a full +/- 180 degree range:
Code:
// Calculate object pitch, roll and yaw
            angle_x = acosf( _object->_SIO2transform->mat[ 0 ] ) * SIO2_RAD_TO_DEG;
            if ( _object->_SIO2transform->mat[ 8 ] < 0.0f )
               angle_x = - angle_x;            
            _FSobjectdescriptor->Pitch = angle_x;
            angle_y = acosf( _myobject->_SIO2transform->mat[ 5 ] ) * SIO2_RAD_TO_DEG;
            if ( _myobject->_SIO2transform->mat[ 9 ] < 0.0f )
               angle_y = - angle_y;
            _FSobjectdescriptor->Roll = angle_y;
            angle_z = acosf( _myobject->_SIO2transform->mat[ 10 ] ) * SIO2_RAD_TO_DEG;
            if ( _myobject->_SIO2transform->mat[ 6 ] < 0.0f )
               angle_z = - angle_z;            
            _FSobjectdescriptor->Yaw = angle_z;
But the yaw (z axis) is not right. Why?

cgen2

Posts : 38
Join date : 2008-12-14

View user profile

Back to top Go down

Re: Converting matrix to vectors

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum