0
\$\begingroup\$

I'm implementing a framerate independent FPS camera and I'm having a problem with jerky camera. The camera is smooth if I translate it or if I make it rotate at a fixed rate, but as soon as I start to move it with the mouse, it starts stuttering.

This is the part of the code in the main loop that keeps track of the time and calculates the rotation around the y axis:

std::chrono::high_resolution_clock::time_point newTime = std::chrono::high_resolution_clock::now();
uint32_t frameTime = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::microseconds>(newTime - oldTime).count());

oldTime = newTime;

double mouseXInput;
double mouseYInput;

glfwGetCursorPos(window, &mouseXInput, &mouseYInput);
glfwSetCursorPos(window, 0, 0);

float mouseX = static_cast<float>(mouseXInput);
float mouseY = static_cast<float>(mouseYInput);

float rotateSpeedModifier = 0.0000005f;

// Fixed rotation rate
camera.rotation.x += frameTime * 0.0000006f;

// or mouse controled rotation rate (pick just one of the two lines)
camera.rotation.x += frameTime * rotateSpeedModifier * mouseX);

// Overflow and underflow control
if (camera.rotation.x > 2.0f * PI) {
    camera.rotation.x -= 2.0f * PI;
} else if (camera.rotation.x < 0.0f) {
    camera.rotation.x += 2.0f * PI;
}

Note that I disabled the cursor and enabled raw mouse motion at the start of the program (it is supported). I also centered it:

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
glfwSetCursorPos(window, 0, 0);

Since the stuttering only appears when I'm moving the mouse, I'm guessing it's some kind of precision problem. Variable frameTime is anywhere between 400 and 5000. I think using milliseconds doesn't give enough precision. Variables mouseX and mouseY are usually within [-10, 10] if I'm not too aggressive with the mouse, and they are always a whole number which is odd to me. I don't think casting it to float is the problem then either.

What can I do here to improve mouse tracking precision? Is that even what the problem is? How do I fix this?

\$\endgroup\$
2
\$\begingroup\$

Rotation should not depend on frame time at all, but only on the mouse delta. If I take out the frame time out of equation, it works as expected.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.