Interpolation, Extrapolation - My life is still moving around...

I needed a good interpolation system for my cars in the mmo world. I tried a few in the internet but nothing served its purpose. The logic used server time and current network time to figure out how old the packets were to based on which it would interpolate or extrapolate or simply snap. For most part the logic worked fine and so I don't blame all the wonderful articles I found in the internet. The problem I had was lag, frequent snapping, and sometimes slow and quick acceleration of the car. There wasn't a smooth movement. It would move smooth as long as the packets arrived fine and once they delay it would begin doing all crazy things.


After dumping all logs it was evident that the problem was with the packets arriving very late. This is because over the wire through UDP there were lots of packets being dropped. This means on a high speed racing game where I collect and do a lot of things I definitely have a great deal of this loss and this magnifies where there are more users in the room. So if the difference in time between two packets is about a second or more what can you do? Answer : Nothing.

So I sat and wrote down my own system which basically pushes all data onto a list and keeps popping items from it once its done. There were few things I tried to overcome this problem.

Suppose there was a delay in the packet reaching a client then..

  • If the distance between the last received packet of the vehicle and the new packet is more than a threshold ((A-B).sqrMagnitude) then it means that all the packets between A and B were dropped. So you can introduce a time t (a high value depending on the difference in time) to ensure that the car keeps interpolating. The advantage in this approach is you would not see the quick interpolation  and your car would begin smoothly towards the new point B. The disadvantage is that this introduces lag. This means your mmo car is nowhere close to where its supposed to be considering even a average lag of 200 to 300 ms. If your main concern is getting the vehicle move smoothly then this should be it.
  • The next option is to begin extrapolating based on the last two received packets. If you have A and B as the last two packets in the buffer and you are waiting for packet C then you can use the information of A and B to decide how to extrapolate. (A-B) would give you the directional vector and you can simply do this math to extrapolate over time car.position = car.position + ( (A-B).normalized * speed * deltaTime ). Once you received the new packet you can add the current position of your car to the buffer (or else the car would snap back to B and begin interpolating towards C) so that your car continues from its current point towards C. Ideally it would be great to enabling physics in your car and add a force instead of positioning the car as above. This would ensure your car always stays above ground.
  • Just snap. If the difference is great most often the best approach is to snap it. It looks odd and ugly but there is no choice. At least your car is where its supposed to be.

Comments

Popular posts from this blog

Authoritative Server - MMO

Code dependencies