Page 1 of 1

Making fixed time steps not jerky

Posted: Sat Jul 29, 2017 2:26 pm
by Skeletonxf
My game has very significant bullet time style slow downs and uses love.physics for the physics engine. Initially I opted for a variable time step to let me have easy delta time changes but this caused far too many problems with the physics engine behaving differently so I opted for a fixed time step which I have now fully implemented into the update loop. I am currently testing with a fixed time step of 0.016 seconds. This is ridiculously small because the game can slow down by far more than a factor of 10. However, even with this very small step time the game is visibly jerky due to waiting noticeable lengths of time between two time steps. I think I need to render between time steps so the visuals do not jerk however I am not sure how to do this?

The jerkiness can be seen here:


Re: Making fixed time steps not jerky

Posted: Sun Jul 30, 2017 5:49 pm
by JoshGrams
The general approach is to track two states for all the objects (before and after the last physics step) and interpolate between them based on the "leftover" time. You can also use just the current state and extrapolate into the future based on the velocity and such, but that tends to have more problems. For instance, if an object is going to bounce off a wall next frame, then you will extrapolote into the wall (the wrong direction) and then it will "snap" back to the actual position after the next physics update. So usually it is better to live with the partial physics frame worth of lag.

So if you have the usual update loop where you accumulate time and feed it to the physcis engine in fixed steps:

Code: Select all

function love.update(dt)
    unsimulated = unsimulated + dt
    while unsimulated < fixedStep do
        unsimulated = unsimulated - fixedStep
        savePreviousState(allVisibleObjects)
        world:update(fixedStep)
    end
end
Then when you draw you do (very roughly):

Code: Select all

function love.draw()
    local t = unsimulated / fixedStep  -- between 0 and 1
    for <each object...> do
        x = object.prev.x * (1-t) + object.x * t
        y = object.prev.y * (1-t) + object.y * t
        -- ... etc. ...
    end
end
Does that help?

Also if you're going to slow down that much, you may want to use a shorter fixed timestep, maybe 100 or 120 FPS or even faster instead of only 60? I guess you'd have to test on your slowest target computer with your most complicated level to see how much you can crank it up...

Edit: You may also want to read Gaffer On Games' article Fix Your Timestep if you haven't seen that...

Re: Making fixed time steps not jerky

Posted: Mon Jul 31, 2017 2:57 pm
by Skeletonxf
I had indeed read that article and I was still stuck. As I'm using the physics engine I'm very cautious to decrease the time step as fps is already starting to be a concern to me.

Eventually I decided that I don't really need to slow down the game via the time step, I just need to slow down enemies and the player, and I've opted for faking the slow down via :setLinearDamping() instead.