I am making a script for the camera,
Everything works perfectly, except for a problem with the rotation of the camera allowing the user to look around using the mouse.
The name of the problem (from what I’ve found on the net) is called gimbal lock, a problem that occurs when two axes orientate towards the same or opposite direction, this causes a visual anomaly, in my case the camera rotates on the Z axis, trying a mirroring on the horizontal axis.
To start with this problem I then added an angle control, in short I calculate the angle between the Up vector and the new Orientation of the camera, if this is within a certain range per hour it uses the new orientation, here is the code:
double mouseX, mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
float rotX = sensitivity * ((int)mouseX * (2.0f / width) - 1.0f);
float rotY = sensitivity * ((int)mouseY * (2.0f / -height) + 1.0f);
orientation = glm::rotate(orientation, glm::radians(-rotX), up);
glm::vec3 nOrientation = glm::rotate(orientation, glm::radians(rotY), glm::normalize(glm::cross(orientation, up)));
float angle = glm::degrees(glm::angle(nOrientation, up));
if (angle > 5.0 && angle < 175.0)
orientation = nOrientation;
glfwSetCursorPos(window, (width / 2), (height / 2));
The problem is partly solved, in fact the code prevents me from looking up below 5 degrees and upwards above 175 degrees, but this is only if I move the mouse with a normal speed, in fact if I increase the speed with which I go up or down the problem manifests itself, or so I tried decreasing the range from 5-175 to 30-150, but it still didn’t work.
I am still studying OpenGL and the world of CG, so the way to rotate the camera may be incorrect for some, and instead favour quaternions, but I don’t know what they are yet.