I'm implementing friction into my game, and I'd like it to be framerate-independent. Here's my first solution, in Lua style pseudo-code:
local pos = 0
local vel = 100
local deceleration = 80
function Game:update(dt)
vel = move_toward(vel, 0.0, deceleration * dt)
pos = pos + vel * dt
end
with move_toward
being identical to the way it works in engines like Godot. Here's the implementation just in case but it's not that relevant.
function move_toward(from, to, delta)
if math.abs(to - from) <= delta then
return to
else
return from + sign(to - from) * delta
end
end
Now, this seems to work fine but I noticed that there's still some very small disparities when I try to run this at different framerates: video link (top is 60 FPS, middle is 30 FPS, bottom is 15 FPS). I can't seem to figure out why that is.
I'm not trying to make a perfect or realistic simulation, just one that's good enough for games. I'm familiar with algebra and analysis, but I'd like something that is simple to implement, it's better if I can avoid complicated formulas or bits of code.