Sign in to follow this  
Cacks

Matrix Camera Problems

Recommended Posts

Hi guys, I have a camera class, with 2 4x4Matrices, 1 to hold it's position and 1 for it's rotation. I am having problems rotating my camera then translating it. It won't translate in the direction it is pointed. My Matrices are made for OpenGL and have indices 0 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15 I must not be getting the correct axes. To get the camera's x-axis I extract values at indices 1,2&3 of the rotation matrix and place them in a vector, then normalise that vector, does that sound correct? The direction my camera is moving seems to be the direction perpendicular to the intended direction. Any ideas why this would be happening? Thanks for any help given!

Share this post


Link to post
Share on other sites
Quote:
Original post by Cacks
Hi guys,

I have a camera class, with 2 4x4Matrices, 1 to hold it's position and 1 for it's rotation.

I am having problems rotating my camera then translating it. It won't translate in the direction it is pointed.

My Matrices are made for OpenGL and have indices

0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15

I must not be getting the correct axes. To get the camera's x-axis I extract values at indices 1,2&3 of the rotation matrix and place them in a vector, then normalise that vector, does that sound correct?
If the matrix is a proper rotation matrix, the normalization step shouldn't be necessary.

Here's a guess as to the problem. It's true that the basis vectors of the rotation are in the columns of the matrix, that is, x is (0, 1, 2), as you noted. However, in setting up the view matrix the basis vectors at some point have to be transposed, in which case x is (0, 4, 8).

From your post, it's not clear whether you're extracting the basis vectors before or after this transposition takes place. If it's after, that would probably explain the incorrect results. If you're not sure, you might post some of your code. Or, just as an experiment, you could try getting the basis vectors from the rows rather than columns and see if that solves the problem.

Share this post


Link to post
Share on other sites
Hi jyk,

This is my code for setting up my camera:


void Camera::setCamera()
{
glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity(); // reset modelview matrix
glMultMatrixf(rotationMatrix.matrix); // Rotate Camera
glMultMatrixf(translationMatrix.matrix); // Translate camera
}



Should I transpose my rotation matrix befor passing it into glMultMatrixf?

What is the purpose of transpose?

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Cacks
Hi jyk,

This is my code for setting up my camera:

*** Source Snippet Removed ***

Should I transpose my rotation matrix befor passing it into glMultMatrixf?

What is the purpose of transpose?

Thanks
The above suggests that your matrices are already inverted at this point (that is, the rotation matrix is transposed and the translation is negated). Since we still don't know how the actual matrices are being constructed, or at what point you're extracting the basis vectors, it's hard to say what's really going on.

If you want to post some more of your code, I'll be happy to take a look at it and see if I can spot anything.

Share this post


Link to post
Share on other sites
this just popped in my head right now, haven't given it much thought...


Are your [3],[7] and [11] members, simply the negative of the desired position of the camera?
e.g. mat[3] = -cameraPos.x ?

If yes, they should be the negatives of the dot product of the vector position, with the respective camera axis.

E.g. mat[3] = -dot( camera pos, camera X axis )
mat[7] = -dot( camera pos, camera Y axis ), etc...

edit:
I meant the [12],[13] and [14] members.
(The [3],[7],[11] should be zeros(?))

Share this post


Link to post
Share on other sites
Yeah,

using the transpose rotation matrix makes the camera translate in the direction it is pointing:


void Camera::setCamera()
{
glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity(); // reset modelview matrix
glMultMatrixf(rotationMatrix.transpose().matrix);
glMultMatrixf(translationMatrix.matrix); // Translate camera
}




Tyk, when you said:

Quote:

The above suggests that your matrices are already inverted at this point (that is, the rotation matrix is transposed and the translation is negated)


What does that mean?

Should I also be using the transpose of my translation matrix? I am having to negate my translation values at the minute?

Thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this