custom fixed timestep
Posted: Mon Aug 06, 2018 9:50 pm
Hey everyone, I've been trying to rewrite love.run() function to implement fixed timestep the way I want it to work. Basically I set a dt constant value (FRAME_DT). After that I execute love.load(), then enter a game loop. Then in the game loop I want to do this: check time since programme started, call love.update(FRAME_DT), call love.draw(), check the amount of time it took (update and draw). If it took less time than dt seconds, wait dt - time seconds, else don't wait. Rinse and repeat. It seemed to work okay but if I change my FRAME_DT it seems to changes my objects speed. That should'nt happen since love.update use FRAME_DT to adapt objects speed to the timestep… Here is the code I wrote :
I know there are other methods for this, but the reason is wrote here is not so much how to do this another way, but to understand why it does not work the way I think it is supposed to. Seems like either I made a really dumb mistake I can't see or because something I don't know about timers, the way love works... Anyway, thanks for taking the time to read this!
Code: Select all
function love.run()
-- Custom fixed delta time for use in the game loop
local FRAME_DT = 1/60 -- Frame delay (in seconds)
-- Initialize game
if love.load then love.load() end
-- Game loop
local running = true
while (running) do
local frame_start = love.timer.getTime()
-- Handle events
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
running = false
end
love.handlers[name](a,b,c,d,e,f)
end
end
-- Update game state
if love.update then love.update(FRAME_DT) end
-- Draw game state
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.origin()
if love.draw then love.draw() end
-- Wait until its time to process the next frame
local frame_time = love.timer.getTime() - frame_start
if (frame_time < FRAME_DT) then
love.timer.sleep(FRAME_DT - frame_time)
end
-- Flip backbuffer
love.graphics.present()
end
end