Code: Select all
local ta = 0.0 -- tick accumulator
local tr = 1/60 -- tick rate (seconds)
function love.update(dt)
ta = ta + dt
-- first method
if ta >= tr then
-- one full tick elapsed, do stuff using tickrate
ta = ta - tr
end
end
If, however your updates take constantly too long, then this will spiral out of control in the sense that the ticks will be delayed, and the logic will run slower.
Code: Select all
local ta = 0.0 -- tick accumulator
local tr = 1/60 -- tick rate (seconds)
function love.update(dt)
ta = ta + dt
-- secondmethod
while ta >= tr do
-- any number of full ticks elapsed, do stuff using tickrate
ta = ta - tr
end
end
If anything, i'd do this one:
Code: Select all
local ta = 0.0 -- tick accumulator
local tr = 1/60 -- tick rate (seconds)
function love.update(dt)
ta = ta + dt
-- third method
local mult = 0
while ta >= tr do
mult = mult + 1
ta = ta - tr
end
if mult > 0 then
-- do stuff once, but pass everything the tickrate * mult value, instead of just the tickrate.
end
end