There is a problem with my camera controls via the mouse. Looking around kind of works, but there is something wrong. It currently works like this: any time movement is detected right of the center of the screen, the camera turns right, even if the mouse is moving left. It's similar for all directions. It's supposed to work like any FPS camera, where moving in a direction turns the camera in that direction irrespective of the mouse position. What's wrong?
Assistant
Step-by-step analysis of the problem:
Understanding the issue: The camera is turning in the opposite direction of the mouse movement. This suggests that the issue lies in the way the mouse movement is being calculated and applied to the camera's rotation.
Examining the mouse callback function: The mouse_callback function is where the mouse movement is being calculated and applied to the camera's rotation. The key lines of code are:
These lines calculate the offset of the mouse movement from the previous position to the current position.
3. **Identifying the issue**: The problem lies in the fact that the `xOffset` and `yOffset` are being calculated as `lastX - xPos` and `yPos - lastY`, respectively. This means that when the mouse moves to the right, `xOffset` will be negative, and when the mouse moves to the left, `xOffset` will be positive. This is the opposite of what we want.
4. **Fixing the issue**: To fix this, we need to change the way the `xOffset` and `yOffset` are calculated. We should calculate them as `xPos - lastX` and `lastY - yPos`, respectively. This will ensure that the `xOffset` and `yOffset` are positive when the mouse moves to the right and down, and negative when the mouse moves to the left and up.
# Fixed solution:
```cpp
void mouse_callback(GLFWwindow* window, double xPos, double yPos)
{
if (firstMouse)
{
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
float xOffset = (float)(xPos - lastX);
float yOffset = (float)(lastY - yPos); // y-axis is reversed
lastX = xPos;
lastY = yPos;
yaw -= xOffset * sensitivity;
pitch -= yOffset * sensitivity;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(direction);
}
Changed the calculation of xOffset and yOffset: We now calculate xOffset as xPos - lastX and yOffset as lastY - yPos. This ensures that the xOffset and yOffset are positive when the mouse moves to the right and down, and negative when the mouse moves to the left and up.
Cast xPos and yPos to float: We cast xPos and yPos to float to ensure that the subtraction operation is performed using floating-point arithmetic.
Tests and example uses:
Test the camera movement: Run the program and move the mouse to test the camera movement. The camera should now turn in the correct direction based on the mouse movement.