0

I am confused about rotating a camera around its own y-axis. I am using the OpenGL graphics library, so -z goes into the screen. Here is my code so far:

if (KEY_DOWN(F_WindowInfo.KeyStates['Q']))
{
    float Yaw = -1.0f;
    m3 RotationMatrix;
    RotationMatrix.Rc[0][0] = Math_Cosine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[0][2] = Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[1][1] = 1.0f;
    RotationMatrix.Rc[2][0] = -Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[2][2] = Math_Cosine(Math_DegToRad(Yaw));

    F_WindowInfo.Camera->ForwardVector = 
        Math_Normalize(&(RotationMatrix * F_WindowInfo.Camera->ForwardVector));

}
if (KEY_DOWN(F_WindowInfo.KeyStates['E']))
{
    float Yaw = 1.0f;
    m3 RotationMatrix;
    RotationMatrix.Rc[0][0] = Math_Cosine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[0][2] = Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[1][1] = 1.0f;
    RotationMatrix.Rc[2][0] = -Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[2][2] = Math_Cosine(Math_DegToRad(Yaw));

    F_WindowInfo.Camera->ForwardVector = 
        Math_Normalize(&(RotationMatrix * (F_WindowInfo.Camera->ForwardVector)));
}
F_WindowInfo.Camera->ViewMatrix = Math_LookAtMatrix(&F_WindowInfo.Camera->Position,
    &(F_WindowInfo.Camera->Position + F_WindowInfo.Camera->ForwardVector), 
    &F_WindowInfo.Camera->UpVector);

and the Math_LookAtMatrix() function:

m4
Math_LookAtMatrix(v3* Eye, v3* Target, v3* Up)
{
    v3 ZAxis = Math_Normalize(&(*Target - *Eye));
    v3 XAxis = Math_Normalize(&Math_CrossProduct(&ZAxis, Up));
    v3 YAxis = Math_CrossProduct(&XAxis, &ZAxis);

    m4 Result;
    Result.Rc[0][0] = XAxis.x;
    Result.Rc[0][1] = YAxis.x;
    Result.Rc[0][2] = ZAxis.x;
    Result.Rc[1][0] = XAxis.y;
    Result.Rc[1][1] = YAxis.y;
    Result.Rc[1][2] = ZAxis.y;
    Result.Rc[2][0] = -XAxis.z;
    Result.Rc[2][1] = -YAxis.z;
    Result.Rc[2][2] = -ZAxis.z;
    Result.Rc[3][0] = -Math_InnerProduct(&XAxis, Eye);
    Result.Rc[3][1] = -Math_InnerProduct(&YAxis, Eye);
    Result.Rc[3][2] = Math_InnerProduct(&ZAxis, Eye);
    Result.Rc[3][3] = 1.0f;
    return Result;
}

Note that ForwardVector, UpVector, and Position are all 3 dimension vectors and RotationMatrix is a 3x3 matrix.

ForwardVector starts at (0.0f, 0.0f, -1.0f)

Upvector is (0.0f, 1.0f, 0.0f)

The problem I am having is that the rotation I am currently doing makes it look like the world is being moved around the world y-axis instead of the camera just rotating around its own local y-axis. Can someone please let me know what I am doing wrong?

EDIT: I believe my lookAtMatrix function is wrong, i removed the - signs from Result.Rc[3][0] and Result.Rc[3][1] and got the Q and E movement that I originally wanted. Now the x-axis is just backwards.

Kevin
  • 11
  • 2
  • Aside, Why do you have the same thing twice? Put your rotation matrix in a function and call it with diferents yaw angle – Amadeus Feb 27 '19 at 01:09
  • Im confused too. I didnot undestand what do you mean by: "The problem I am having is that the rotation I am currently doing makes it look like the world is being moved around the y-axis instead of the camera just rotating around its own y-axis. **_I am aware that the world is in fact moving around and the view is stationary_, but I mean in terms of what the user feels, not reality._**" – Amadeus Feb 27 '19 at 01:12
  • Amadeus, that comment was simply to prevent the response that there is no such thing as a camera in OpenGL. I was looking through other posts to try to find what was wrong before posting this and that appeared to be a common response. – Kevin Feb 27 '19 at 01:24
  • Amadeus, I am calling it twice to reduce complexity while I work out what is wrong. Once I fix the code, I will organize it better. – Kevin Feb 27 '19 at 01:26
  • you still do not explain what do you mean by: " I am aware that the world is in fact moving around and the view is stationary, but I mean in terms of what the user feels, not reality." What is wrong with tee result of your code? – Amadeus Feb 27 '19 at 01:34
  • by the way, this simple matrix will not do what you expect (rotating around camera y-axis). To do that, the better tool is a quaternion – Amadeus Feb 27 '19 at 01:37
  • I am doing this to teach myself, so I was going to use rotation matrices first and then quaternions once I got rotation matrices down and I want rotations on all three axes, that way I can avoid Gimbal lock. The result of my code makes it not appear as if the camera is rotating in place. Instead it looks like the camera is being swung around the world, like the rotation is happening on the actual world y-axis and not the camera's local y-axis. – Kevin Feb 27 '19 at 01:43
  • To rotate around camera y-axis, you need to transform your forward vector (that seemed to be in world coordinate) to camera coordinate, using, lookAt matrix, do the rotaion and the move it back to world coordinate, to be able to use in a lookAt matrix again. This would be much simpler if you use quaternion directly – Amadeus Feb 27 '19 at 01:46
  • see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) and look for the difference between local and global transformations (bullet #5) ... then in the end are few examples of camera and player control with C++ code... – Spektre Mar 12 '19 at 12:35

0 Answers0