Search code examples
c++openglcameramousemoveglulookat

Opengl mouse Camera issue (gluLookAt)


Hello i am having a strange issue with my mouse movement in openGL. Here is my code for moving the camera with my mouse

void camera(int x, int y)
{
    GLfloat xoff = x- lastX;
    GLfloat yoff = lastY - y; // Reversed since y-coordinates range from bottom to top
    lastX = x;
    lastY = y;

    GLfloat sensitivity = 0.5f;
    xoff *= sensitivity;
    yoff *= sensitivity;

    yaw += xoff;                // yaw is x
    pitch += yoff;              // pitch is y

    // Limit up and down camera movement to 90 degrees
    if (pitch > 89.0)
        pitch = 89.0;
    if (pitch < -89.0)
        pitch = -89.0;

    // Update camera position and viewing angle
    Front.x = cos(convertToRads(yaw) * cos(convertToRads(pitch)));
    Front.y = sin(convertToRads(pitch));
    Front.z = sin(convertToRads(yaw)) * cos(convertToRads(pitch));
}

convertToRads() is a small function i created to convert the mouse coordinates to rads.

With this code i can move my camera how ever i want but if i try to go all the way up when i reach around 45 degrees it rotates 1-2 times around x-axis and then continues to increase y-axis. I can't understand if i have done something wrong so if anyone could help i would appreciate it.


Solution

  • It seems you have misplaced a paranthesis:

    Front.x = cos(convertToRads(yaw) * cos(convertToRads(pitch)));
    

    instead of:

    Front.x = cos(convertToRads(yaw)) * cos(convertToRads(pitch));