Public Group

# Matrices and rotations

This topic is 4920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to get how matrices can be used for 3d graphics. My engine now has an entity baseclass. Each entity has a Matrix4x4 member variable and functions like GetUpVector(), ... Now after reading a lot of tutorials I'm confused. I read that it is possible to combine translations and rotations by multiplying their matrices. So far so good ... I've implemented functions that create translation and rotation matrices. I can move left, right, forward, backward up and down ... I don't get how a rotation matrix can be used to update the matrix of the entity though. And how do I get euler angles that describe how the object is rotated? I think that I'm not getting what the advantage of those transformation matrices is. Probably there is something I have not read yet because tutorial authors think it doesn't have to be stated explicitly. Or in other words ... I think I might be approaching the whole thing from a wrong euler angle.

##### Share on other sites
I don't get how a rotation matrix can be used to update the matrix of the entity though.

You just multiply the two matrices or the matrix and the object's (homogeneous) coordinates.

And how do I get euler angles that describe how the object is rotated?

Assuming your euler angles are rotation around X,Y,Z in that order, and your rotation matrix is R:
    | r11 r12 r13 |R = | r21 r22 r23 |    | r31 r32 r33 |

Then you have:
θY = asin(r13)if θY < π/2   if θY > -π/2      θX = atan2(-r23, r33)      θZ = atan2(-r12, r11)   else      θX = -atan2(r21, r22)      θZ = 0else   θX = atan2(r21, r22)   θZ = 0

I think that I'm not getting what the advantage of those transformation matrices is.

You can't directly transform your points with euler angles - you always have to, implicitely or explicitely, convert to transformation matrices.

Or in other words ... I think I might be approaching the whole thing from a wrong euler angle.

Yes, euler angles have some severe limitations (gimbal lock), which make quaternions and attractive solution - you are essentially switching to a rotation axis + rotation angle representation.

##### Share on other sites
Thanks very much for the reply. I'm trying stuff based on it now.
Just multiplying them? Cool.
Makes sense ... guess my problem was that I always thought in 4x4 matrices.
For rotations I only use the 3x3 part and it should work? ... right?
Dumb question ... I know. I'm on it already.

[Edited by - Clueless on December 31, 2004 8:14:28 AM]

##### Share on other sites
Another bump ...

I guess don't really understand the formula above. What am I doing wrong?

The right-vector is in:
m_Matrix.f[0][0]
m_Matrix.f[1][0]
m_Matrix.f[2][0]

The up-vector is in:
m_Matrix.f[0][1]
m_Matrix.f[1][1]
m_Matrix.f[2][1]

The look-vector is in:
m_Matrix.f[0][2]
m_Matrix.f[1][2]
m_Matrix.f[2][2]

Here is the code I use.

GLfloat CEntity::GetXRotation(){    GLfloat y;    y = asin( m_Matrix.f[0][2] );    if( y < math::PI/2.0f )    {        if( y > math::PI/-2.0f )        {            return atan2( -m_Matrix.f[1][2], m_Matrix.f[2][2] );        }        else        {            return -atan2( m_Matrix.f[1][0], m_Matrix.f[1][1] );        }    }    else    {        return atan2( m_Matrix.f[1][0], m_Matrix.f[1][1] );    }}GLfloat CEntity::GetYRotation(){    return asin( m_Matrix.f[0][2] );}GLfloat CEntity::GetZRotation(){    GLfloat y;    y = asin( m_Matrix.f[0][2] );    if( y < math::PI/2.0f )    {        if( y > math::PI/-2.0f )        {            return atan2( -m_Matrix.f[0][1], m_Matrix.f[0][0] );        }        else        {            return 0.0f;        }    }    else    {        return 0.0f;    }}

The functions always return either 0 (all of them).
or -Pi (all of them).

Sigh :-(

I also tried rotating the world matrix another way.
By multiplying the world matrix with the rotation matrix of the camera.
    Matrix4x4 cameramatrix = m_pCamera->GetRotation4x4();    GLfloat matrix[16];    for( int i = 0; i < 16; i++ )    {    GLfloat matrix[16];    for( int i = 0; i < 16; i++ )    {         matrix = cameramatrix.f[i/4][i%4];    }    glMultMatrixf(matrix);

It also doesn't change anything (there is no rotation whatsoever)

[EDIT]Ok ... one error is that I rotate the matrix AFTER I translate to the position.
Something happens now. But it is weird[/EDIT]

[Edited by - Clueless on December 31, 2004 2:58:20 PM]

• 18
• 11
• 16
• 9
• 51
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999784
×