# Easing towards stepped value

I'm currently developing this rhythm game in lua (on top of a c++ engine) and I'm having trouble keeping a steady playhead position for the song currently playing.

The playhead position that the engine gives me updates in steps, so I get a fresh one every 2, 3 or 4 frames. But I need to have a steady one for sprites moving smoothly with the rhythm and stuff.

So I just keep track of my own playhead variable, which I initialize in 0 and then constantly add delta time to it. This variable eventually gets out of sync with the actual playhead, so every time I get a fresh playhead (or a "new step"), I correct my variable using the average between these two.

This however results in choppy movement.

I've seen many easing functions out there but I'm not sure which one would work in this case, nor when should I call it.

What would be the best way to ease my playhead variable into the stepped one?

This is the code I excecute every frame:

playhead = playhead + delta_time


if stepped_playhead ~= last_stepped_playhead then
end


In your code, the choppiness is coming from the fact that you are jumping every time there is a syncing frame. Jumping to the halfway point is a good idea, but if the displayed playhead is consistently moving away from the actual playhead (or vice versa), then it will constantly be jumping back slightly (or, alternatively, forward).

frames_since_sync = frames_since_sync + 1


The lines sync_delta = (stepped_playhead - playhead) / frames_since_sync and playhead = playhead + delta_time + sync_delta are main changes here.
Essentially, what we're doing is using the number of frames since the last sync frame as an estimate for the number of frames until the next sync frame. Then we divide the error between our two playheads by that number of frames. This new sync_delta is used to speed up or slow down the displayed playhead so that it should approach the correct position by the time the next sync frame comes around. At the next sync frame, the process is repeated, and the speed adjusted again to try and keep the two playheads near each other.