Opengl Camera and multiplying matrixes

Go To


I Am currently having alot of problems with the camera I am making. The problem occurs with my matrix rotation I am doing as this website says to avoid gimble lock..

One of the first problems you will note is that the order you apply these rotations matter. As previously stated, a rotation matrix is an orientation transform. Each transform defines a new coordinate system, and the next transform is based on an object in the new space. For example, if we apply the roll first, we have now changed what the axis for the subsequent yaw is.

And when i perform this for example if I am wanted to pitch around the current x axis the x axis also changes in my axis to rotation method which is obviously wrong. Ive look around alot and cant find any solution. I have tried alot of differenet version of the axis angle rotation matrix..

void FrustumCamera::xAxisRotation(float angle)
    Vector3<float> x = m_orientation.getXAxis();
    Matrix4<float> matrix = m_orientation.axisAngleRotation(x,angle);
    m_orientation = matrix*m_orientation;
void FrustumCamera::yAxisRotation(float angle)
    Vector3<float> y = m_orientation.getYAxis();

    Matrix4<float> matrix = m_orientation.axisAngleRotation(y,angle);
    m_orientation = matrix*m_orientation;


Matrix4<Type> Matrix4<Type>::operator*(Matrix4& matrix)
    Matrix4<Type> temp(m_matrix);
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            Type total = 0;
            for(int k=0;k<4;k++)

                total += m_matrix[i][k]*matrix.getAt(k,j);;

    return temp;
template <class Type>
Matrix4<Type> Matrix4<Type>::axisAngleRotation(Vector3<Type> axis, const Type angle)
    Type radians = angle * (double)degToRad;
    Matrix4<Type> temp;
    float c = cosf(radians);
    float s = sinf(radians);
    float t = 1.0f - c;

    float x = axis.x;
    float y = axis.y;
    float z = axis.z;

    temp.setAt(0,0, c+x*x*(t));
    temp.setAt(0,1, x*y*(t)-z*s);
    temp.setAt(0,2, x*z*(t)+y*s);
    temp.setAt(0,3, 0.0f);

    temp.setAt(1,0, y*x*(t)+z*s);
    temp.setAt(1,1, c+y*y*(t));
    temp.setAt(1,2, y*z*(t)-x*s);
    temp.setAt(1,3, 0.0f);

    temp.setAt(2,0, z*x*(t)-y*s);
    temp.setAt(2,1, z*y*(1-c)+x*s);
    temp.setAt(2,2, c+z*z*(t));
    temp.setAt(2,3, 0.0f);

    temp.setAt(3,0, 0.0f);
    temp.setAt(3,1, 0.0f);
    temp.setAt(3,2, 0.0f);
    temp.setAt(3,3, 1.0f);

    return temp;
void OpenGLRenderer::startDraw(unsigned long mask)
    //sortBuffer();                                     // sort draw queue
    clearBuffers(mask);                                 // clear buffers
    glTranslatef(-1*m_frustumCamera->getViewMatrix().getTranslationAxis().x,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().y,-1*m_frustumCamera->getViewMatrix().getTranslationAxis().z);// load identity

2012-04-04 02:39
by Chris Condy
What kind of camera are you trying to make? FPS style camera, or 6DOF (spaceship) - Tim 2012-04-04 02:52
6DOF(spaceship) without the z axis rotation (roll - Chris Condy 2012-04-04 02:56


I think order of multiplication can cause the problem, instead of

m_orientation = matrix*m_orientation;


m_orientation = m_orientation * matrix;
2012-04-04 04:57
by LebRon
Ive just tried this, it did help a little bit. The camera still rotates strangely I fear my translations of the camera are wrong. But I have tried every possible way of placing the translations still no luck - Chris Condy 2012-04-04 05:23