## Interpolation: how to do it right?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

### Interpolation: how to do it right?

Hi, I read that it is good to separate the simulation from the render cycles, which allows for a fixed dt in the simulation. This makes the simulation frames and render frames independent, so in order to achieve smooth animation you need to interpolate between two simulation frames.

Now, this is the sort of pseudo code that ive been using:

Code: Select all

sim_frame = 1 / 100 -- i want 100 simulation cycles per second
render_frame = 1 / 60 -- i'm assuming this
current_sim = getTime() -- what is the current state of the simulation
last_render = getTime() -- when did the last flip occur

function update()
while current_sim < last_render + render_frame do -- simulate until you catch up with next render time
simulate(sim_frame) -- simulate with constant dt
current_sim = current_sim + sim_frame
end
interpolation = ((last_render + render_frame) - (current_sim - sim_frame)) / sim_frame -- takes values between 0 and 1
draw(interpolation) -- draw shapes interpolating between x and ox
last_render = getTime()
end

What my problem is, is the 2nd line, when I define that the render frame lasts 1/60 sec, which might not be true. Then I take a note when the last flip occured and predict that the next flip will occur at last_render + render_frame.

The question is: how can I universally predict when the next flip will happen, in order to apply the correct interpolation?
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

### Re: Interpolation: how to do it right?

The only case when you would need the simulation frequency to be higher than the rendering frequency is when it would necessary for simulation stability. Is this your case? Keep it simple.
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

### Re: Interpolation: how to do it right?

Yes, it is the case. But it doesn't even matter, simulation frequency could be 60 Hz as well. But still, I have no guarantee that the rendering frequency will be 60 Hz, especially in case of disabled VSYNC.

The question is: If I simulate with a fixed framerate, how do I interpolate the render frames without knowing the refresh rate?
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

### Re: Interpolation: how to do it right?

Two options come to my mind:
• If you know, that the simulation is faster than real time, then you can add some lines of code that make the loop wait until 1/60 of a second is passed. That way you can try to enforce the frame rate. This is roughly like this:
• measure time
• draw everything
• perform simulations until desired point in time (determined by render_frame) (the simulation data is now a bit ahead of the real time)
• wait until the time passed since the time measurement above is equal to render_frame
• repeat
• The second option is to constantly measure the time you need for one full loop and use that. That way, the time estimate is one cycle behind, but I remember that I read somewhere that this is not noticable.

### Who is online

Users browsing this forum: Google [Bot] and 42 guests