Quaternion Camera Help

Started by
1 comment, last by Coldon 15 years, 11 months ago
I've been trying to write a very basic quaternion camera for some time now but i have gotten a little stuck. I'm crazy busy at work atm and don't have too much time to spend playing around with it. I've read most of the quaternion tutorials on the web, but I've found everyone of them lacking in some regard. I just want an FPS style camera with forward and backward movement - (forget about strafe) My rotation seems to work and is as follows (h is heading, p is pitch):

void rotate(float h, float p)
{
        // Make the Quaternions that will represent our rotations (from angles h,p)
        quaternion qH(h,worldYAxis);
        quaternion qP(p,worldXAxis);
 
        //Combine the pitch and heading rotations (order is important)
        qR = qP * qH;   
 
        //update rotation matrix                
        qR.toMatrix(rotationMatrix);
 
        //get the normalized direction vector (3rd row of resultant rotation matrix)
        direction.x = rotationMatrix[8];
        direction.y = rotationMatrix[9];
        direction.z = rotationMatrix[10];
 
        direction.normalize();
 
        cout << "direction : " << direction.x << "," << direction.y << "," << direction.z << endl << endl;
}


is my calculation of the forward vector correct? I'm taking the third row of the resultant rotation matrix as the forward vector. I have a feeling i need to invert the sign somewhere as the movement isn't 100% correct. here is the movement function:


void move(float lr, float fb)
{       
        //update translation matrix
        translationMatrix[12] += fb * direction.x;
        translationMatrix[13] += fb * direction.y;
        translationMatrix[14] += fb * direction.z;
 
        //update position
        position.x = translationMatrix[12];
        position.y = translationMatrix[13];
        position.z = translationMatrix[14];
}


to set the view i do this:


void setView()
{
        //Load Model View Matrix
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
                
        //Apply Rotation 
        glMultMatrixf(rotationMatrix);
 
        //Apply Translation     
        glMultMatrixf(translationMatrix);
}


Sorry for being lazy and not figuring it out myself but its one of those things that bugging me and i just don't have the time to sit and work it out right now. thanks in advance.

"In theory, theory and practice are the same. In Practice, they never are."
My Technical Blog : http://www.takinginitiative.net/

Advertisement
I think you'll want to take another look at this code:
//Apply Rotation glMultMatrixf(rotationMatrix); //Apply Translation     glMultMatrixf(translationMatrix);
You should be using the inverses of these matrices (I'll have to leave an explanation of 'why' for another post...). Also, depending on how you're handling things, you may need to add a Y rotation of 180 degrees to adjust for OpenGL's '-z goes into the screen' convention.

The resulting code might look something like this:
glRotatef(180, 0, 1, 0);glMultMatrixf(inverse(rotationmatrix));glMultMatrixf(inverse(translationMatrix));
You can use a general matrix inverse function, or you can take advantage of the fact that the inverse of a rotation matrix is its transpose, and the inverse of a translation matrix is the same matrix with the translation components negated.

Once you've made this change, your rotations will likely be backwards; just reverse the signs of the Euler angles somewhere along the way and things should be fine.

I know I didn't provide a lot of information here, but maybe this will at least get you pointed in the right direction.

Oh, also, the use of quaternions really serves no purpose here. It doesn't hurt, but if I were you I'd just stick with matrices.
okay thanks, i'm not sure of the reasoning behind the inverse matrices but its worth a try, and i am taking into account the right hand system (thats why i thought that i'm missing a negative sign in my direction vector).

"In theory, theory and practice are the same. In Practice, they never are."
My Technical Blog : http://www.takinginitiative.net/

This topic is closed to new replies.

Advertisement