Code: Select all
local accumulator = 0
local timestep = 69 / 4096
function love.update (dt)
accumulator = accumulator + dt
dt = timestep
if accumulator >= dt then
while accumulator >= dt * 2 do dt = dt * 2 end
accumulator = accumulator - dt
-- do stuff here with dt
end
end
The timestep value has an exact binary representation and is just under 60 fps. When the accumulator is more than twice the timestep, the timestep is doubled (as many times as needed, but probably usually just once). So it usually has a fixed timestep of ~60 fps, but it will drop to ~30 fps to get out of a death spiral. Not exactly a fixed timestep, I guess, but carefully managed.
Other than problems with tunneling (can be fixed with CCD or not having super-fast things) and jitter (maybe can be fixed by extrapolation or interpolation in draw routine), are there problems with this approach I'm not thinking of? Is this a common approach? How will box2d feel about this? Are there any other good alternatives?