Converting matrix to vectors
3 posters
Converting matrix to vectors
How do I convert an OpenGl matrix to vectors?
I am using Bullet (sweet) in my render routine fetching the matrix like so :
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]
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
Re: Converting matrix to vectors
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.
Re: Converting matrix to vectors
But of course SIO2 has done the hard work already. I should have known. Many thanks
cgen2- Posts : 38
Join date : 2008-12-14
Re: Converting matrix to vectors
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
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
Re: Converting matrix to vectors
Here's the ugliness I mentioned:
Now if there's a better way I'd like to hear it.
- 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
Re: Converting matrix to vectors
_myObject->_SIO2Transform->rot->z
rot is a vec3
rot is a vec3
oioioi- Posts : 136
Join date : 2008-12-02
Location : Norway
Re: Converting matrix to vectors
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
Re: Converting matrix to vectors
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;
_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;
Re: Converting matrix to vectors
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.
Now I need to make this work over a full rotation, +/- 180 degrees ...
I ever so slightly enhanced your suggestion removing the run time divide by substituting SIO2_RAD_TO_DEG for / SIO2_PI * 180.0f.
Now I need to make this work over a full rotation, +/- 180 degrees ...
cgen2- Posts : 38
Join date : 2008-12-14
Re: Converting matrix to vectors
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;
cgen2- Posts : 38
Join date : 2008-12-14
Permissions in this forum:
You cannot reply to topics in this forum
|
|