1
\$\begingroup\$

I'm developing an FPS in Unity using Photon for networking. The Photon-provided interpolation is very basic so I decided to roll out my own using the Hermite spline.

It works good and is significantly better than the provided interpolation. However, I was wondering what's the correct way to choose the t value. In a perfect world, it should be incremented by deltaTime*timeBetweenPositionUpdates. However, that doesn't work since even if the tickRate is set to 10 for example, sometimes the client sends it at 9.5, and then at 10.2 and so on and so forth. This means that we can't have a constant increment rate for the t value since it'll either lag behind or move too fast and we'll run out of buffered frames. What I decided to do is to send the time between frame updates as well and interpolate using that. But, this also isn't perfect by any stretch of the imagination and the receiving client will soon lag very much behind.

What I'm doing now is using a multiplier on the t value such that if there is more frames in the buffer than we want to have then it'll speed the interpolation up, and if there's less than how many we want in the buffer then it'll slow the interpolation down. This works pretty well, but it's a hack I believe.

Is there a better way, or a proper way to do this?

\$\endgroup\$
1
\$\begingroup\$

It depends on what you want. It's always a trade-off between having to wait occasionally or having a longer lag. You can trade one against the other (by specifying your desired optimal buffer size).

In order to adapt to variable incoming frame rates you can either output variable frame rates or adapt the interpolation interval. You do the latter and if this is indeed what you want, I would not regard it as a hack but rather as a feature.

Also it makes sense to slow the interpolation down, when the buffer gets empty and speed it up when the buffer is full. You might define a correction that somehow scales with the difference of the current buffer size and the desired buffer size. This difference is what you want to keep minimal.

This is also known as a control-feedback-loop. In order to work efficiently, a feedback loop should react fast enough to changes in the control variable but not too fast or you'll get wildly oscillating buffer sizes. The general idea is to increase the reaction speed until overshooting appears, then reduce the sensitivity again. An additional integral part (sum of control variable values for a certain time interval) might even help more. See also: PID controller

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.