Fixed Timestep with Interpolation

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
bromailley
Prole
Posts: 6
Joined: Sat Sep 07, 2013 3:32 pm

Fixed Timestep with Interpolation

Post by bromailley »

I'm trying to fix my time step and use interpolation to correct the visual stuttering, and although this is leaps better than my first attempts, it's still not perfect. I'm updating the game at 30 ticks per second. I know, for example, that GameMaker can run games at 30 ticks per second and doesn't suffer from significant stuttering... so why is my Love program stuttering? :/ Obviously this stuttering is significantly reduced when I double the tick rate, but I'm still wondering why it is occurring at 30.

Code: Select all

function love.load()
    --load game assets (runs only once)
	x = 100
	y = 100
	prevX = x
	prevY = y
	speed = 7
	accum = 0
	tickrate = 1/30
end

function love.update(dt)
	if (dt > 0.25) then
	    dt = 0.25
	end
	accum = accum + dt
	while (accum >= tickrate) do
	    accum = accum - tickrate  
		prevX = x
		prevY = y
		if love.keyboard.isDown("right") then
			x = x + speed
		end
		if love.keyboard.isDown("left") then
			x = x - speed
		end
		if love.keyboard.isDown("up") then
			y = y - speed
		end
		if love.keyboard.isDown("down") then
			y = y + speed
		end
	end
	love.draw()
end

function love.draw()
    love.graphics.print("FPS: "..tostring(love.timer.getFPS()), 10, 10)
	love.graphics.print("X: "..tostring(x), 10, 25)
	love.graphics.print("Y: "..tostring(y), 10, 40)
	love.graphics.setColor(255, 255, 255, 255 * (accum / tickrate))
	love.graphics.circle("fill", prevX, prevY, 50, 100)
	love.graphics.setColor(255, 255, 255, 255)
    love.graphics.circle("fill", x, y, 50, 100)
end

function love.quit()
    --quit the game
end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Fixed Timestep with Interpolation

Post by raidho36 »

That's because you're doing it wrong. You should re-write love.run function rather than tamper with it's workflow.
bromailley
Prole
Posts: 6
Joined: Sat Sep 07, 2013 3:32 pm

Re: Fixed Timestep with Interpolation

Post by bromailley »

raidho36 wrote:That's because you're doing it wrong. You should re-write love.run function rather than tamper with it's workflow.
Well, duh. Obviously my implementation is incorrect. How should I go about fixing it?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Fixed Timestep with Interpolation

Post by raidho36 »

As I said, rewrite the love.run function.

What I really suggest is to avoid using "fixed" timestep approach. I enquote it because no matter how hard you try, you can't get it really fixed, due to many many factors. Fixed timestep anyway is a simplification of dynamic timestep, a tradeoff - slightly simplier to program motion in expense of lost precision and logic framerate fluctuations.
bromailley
Prole
Posts: 6
Joined: Sat Sep 07, 2013 3:32 pm

Re: Fixed Timestep with Interpolation

Post by bromailley »

raidho36 wrote:As I said, rewrite the love.run function.

What I really suggest is to avoid using "fixed" timestep approach. I enquote it because no matter how hard you try, you can't get it really fixed, due to many many factors. Fixed timestep anyway is a simplification of dynamic timestep, a tradeoff - slightly simplier to program motion in expense of lost precision and logic framerate fluctuations.
Yes, but having tinkered with the love.run function, I haven't gotten anywhere. Thanks for the help, I guess I'll just keep chipping away at it.

I understand the tradeoffs, but I'm using a fixed timestep.

I have tried modifying love.run; granted, I don't understand everything that's going on in here and obviously I am missing something still.

Code: Select all

function love.run()

    math.randomseed(os.time())
    math.random()
	math.random()

    if love.load then love.load(arg) end

    local dt = 0

    -- Main loop time.
    while true do
        -- Process events.
        if love.event then
            love.event.pump()
            for e,a,b,c,d in love.event.poll() do
                if e == "quit" then
                    if not love.quit or not love.quit() then
                        if love.audio then
                            love.audio.stop()
                        end
                        return
                    end
                end
                love.handlers[e](a,b,c,d)
            end
        end

        if love.timer then
            love.timer.step()
            dt = love.timer.getDelta()
			if (dt > 0.25) then
			    dt = 0.25
			end
			accum = accum + dt
        end

        -- Call update and draw
        if love.update then
		    while (accum >= tickrate) do
			    accum = accum - tickrate
			    love.update()
		    end
		end
		
        if love.graphics then
            love.graphics.clear()
            if love.draw then love.draw() end
        end

        if love.timer then love.timer.sleep(0.001) end
        if love.graphics then love.graphics.present() end

    end

end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Fixed Timestep with Interpolation

Post by Robin »

Minor detail: don't call love.draw() in love.update(). They're both callbacks.

Also, why don't you simply multiply speed by dt instead of mucking about with semi-fixed timesteps?
Help us help you: attach a .love.
bromailley
Prole
Posts: 6
Joined: Sat Sep 07, 2013 3:32 pm

Re: Fixed Timestep with Interpolation

Post by bromailley »

Robin wrote:Minor detail: don't call love.draw() in love.update(). They're both callbacks.

Also, why don't you simply multiply speed by dt instead of mucking about with semi-fixed timesteps?
Thanks. I'll fix that.

Because I want to experiment. Because other software I am familiar with uses fixed timesteps. Because some people suggest I should be using fixed timesteps. Because fixed timesteps lead to more regular physics interactions. Because fixed timesteps lead to easier networking. But all of that is an aside, because I am new to Love and simply want to try different things out... which is why the thread isn't called, "Should I use variable or fixed timesteps?"
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Fixed Timestep with Interpolation

Post by raidho36 »

That's right - you shouldn't. So that's completely out of question. All it gives you is slightly simplier programming, and that's it. It's a false claim that it gets you better physics, too. It's good for newbs who can't get into deeper details, but overall, it's a mess and in reality it doesn't works as great as advertised.
bromailley
Prole
Posts: 6
Joined: Sat Sep 07, 2013 3:32 pm

Re: Fixed Timestep with Interpolation

Post by bromailley »

raidho36 wrote:That's right - you shouldn't. So that's completely out of question. All it gives you is slightly simplier programming, and that's it. It's a false claim that it gets you better physics, too. It's good for newbs who can't get into deeper details, but overall, it's a mess and in reality it doesn't works as great as advertised.
I'm really not sure what you're saying here. Anyway, I don't want to turn this into a topic about what I should or shouldn't be doing in terms of fixed vs variable timesteps. I just want to know how I would go about implementing a fixed one.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Fixed Timestep with Interpolation

Post by Nixola »

raidho36 wrote:It's a false claim that it gets you better physics, too.
Actually, fixed timesteps help solving some issues with 'random' timestep, one of which is that equal actions always have the same outcome
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 9 guests