Progrider

Programming discussion

Board index
Help · ✓ Resolved

Character jitters when framerate dips, physics looks fine

Small 2D platformer in C++ with SDL. I multiply velocity by dt every frame. At steady 60 it's smooth. When it dips, the player stutters and jumps come out at a different height, which breaks some levels.

4 replies

Variable dt makes the integration depend on the frame rate. Fixed step with an accumulator: accumulator += frame_time; while (accumulator >= STEP) { update(STEP); accumulator -= STEP; } render(accumulator / STEP); Pass the leftover fraction to render and interpolate between the previous and current states. Otherwise you get a small jitter whenever the update count changes between frames. There's a well known article called Fix Your Timestep that goes through it.
Read the article. Jump heights are identical now. I'm clamping frame_time to 0.25s as well, otherwise dragging the window gives a spiral of updates.
Is this what Godot does with its physics tick?
Yes, _physics_process runs at a fixed rate, and newer versions have physics interpolation you switch on in the project settings. Solved, thanks jf.

Discussion resolved

This discussion is resolved. Its creator can reopen it to accept more replies.