🔒 Locked
Camera rotation problem
Advertisement
Original Post
Hi I am having a problem with my FPS-style camera; it doesn't rotate properly. I calculate the pitch and yaw angles within an "UpdateByMouse"-type of function so that at the end, both of the angles are in degrees and in the range [-360, 360]. Once you've calculated these 2 angles, how do you apply them to the camera's view vector?
You don't need quaternions for this. You could construct an Euler-angle rotation matrix from your yaw and pitch rotations, or construct the basis vectors directly using trig. Which would be easier kind of depends on what you've already got going. If you need more specific advice, you might post some of your camera code.
The one other thing I'll say is that you'll want to apply the rotations in the order pitch->yaw.
The one other thing I'll say is that you'll want to apply the rotations in the order pitch->yaw.
The following is an excerpt from the camera member function, UpdateByMouse:
If all the math is correct, shouldn't this code work properly?
Note that I found the camera on Nehe.
if( MouseX < CenterX){ DeltaMouse = GLfloat(CenterX - MouseX); Cam.ChangeHeading(-0.2f * DeltaMouse);}else if(MouseX > CenterX){ DeltaMouse = GLfloat(MouseX - CenterX); Cam.ChangeHeading(0.2f * DeltaMouse);}if(MouseY < CenterY){ DeltaMouse = GLfloat(CenterY - MouseY); Cam.ChangePitch(-0.2f * DeltaMouse);}else if(MouseY > CenterY){ DeltaMouse = GLfloat(MouseY - CenterY); Cam.ChangePitch(0.2f * DeltaMouse);}Mat4x4 rotY, rotZ, combinedMat; // matrices are initiliased to zeroV3D newViewVector;rotZ->Rotate( pitchInDegrees, 0.0f, 0.0f, 1.0f ); // operation affects ALL elementsrotY->Rotate( yawInDegrees, &Y, 0.0f, 1.0f, 0.0f );combinedMat = rotZ * rotY;// multiply the current view vector by the combined Xform matrix and add it to itselfview += combinedMat * view;If all the math is correct, shouldn't this code work properly?
Note that I found the camera on Nehe.
It looks like you're using column vectors, so I'd think you'd want this:
combinedMat = rotY * rotZ;Instead of this:combinedMat = rotZ * rotY;Also, I may be missing something, but I don't understand what this line is supposed to do:view += combinedMat * view;Setting aside the fact that the direction vectors can be extracted directly from the matrix, I'd think the above should just be:view = combinedMat * world_forward_axis; where 'world_forward_axis' is whatever world axis is considered forward in local space.
Mat4x4 mat;float oglModelview[16] = { 0 };glMatrixMode( GL_MODELVIEW_MATRIX );glPushMatrix();glLoadIdentity();glRotatef( pitchDegrees, 0.0f, 0.0f, 1.0f );glRotatef( yawDegrees, 0.0f, 1.0f, 0.0f );glGetFloatv( GL_MODELVIEW_MATRIX, oglModelview );glPopMatrix();mat->SetFromArray( oglModelview );mat->TransformV3D( &viewVector, &viewVector ); //viewVector will hold the resultOk, I am using OpenGL to calculate the composite rotation matrix and then transform the current view vector by it (i.e. the direction the camera is facing).
Shouldn't this work?
One observation. You probably need to change this:
Also, if you want to just extract the direction vectors directly, they are the first three columns of the matrix.
glRotatef( pitchDegrees, 0.0f, 0.0f, 1.0f );glRotatef( yawDegrees, 0.0f, 1.0f, 0.0f );To this:glRotatef( yawDegrees, 0.0f, 1.0f, 0.0f );glRotatef( pitchDegrees, 0.0f, 0.0f, 1.0f );In OpenGL, transforms are applied in the opposite of the order that they appear in the code, and you want pitch to be applied first.Also, if you want to just extract the direction vectors directly, they are the first three columns of the matrix.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement