Page 1 of 2

Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 3:42 pm
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

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 3:57 pm
by raidho36
That's because you're doing it wrong. You should re-write love.run function rather than tamper with it's workflow.

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 4:00 pm
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?

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 4:03 pm
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.

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 4:08 pm
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

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 4:46 pm
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?

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 4:55 pm
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?"

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 5:05 pm
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.

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 5:09 pm
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.

Re: Fixed Timestep with Interpolation

Posted: Sat Sep 07, 2013 5:24 pm
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