Entity Position Interpolation Code
Source code for a position interpolation class, together with a small Windows test program.
Download: epic-latest.zip (2006-10-28)
screen shot
Interpolation and Extrapolation of Game Entities
The code is now available on GitHub, too. github.com/jwatte/EPIC
When writing a networked game, you quickly run into the problem that, on client B, when you get data from client A, that data will be old, because of the transmission latency inherent in any networking. On a LAN, you may get away with using the old data as-is, but when playing on the general Internet, you want to display a guess of where the player might be right now.
There are various ways of doing this. The simplest way is to just read the position and velocity of the entity update packet, and forward the position of the entity by the velocity in the packet to the current time. The main draw-back of this approach is that the entity on screen will jump around ("snap" or "lag") for each update packet you get, because there are discontinuities in the interpolated position.
A better method involves interpolating between the last position you actually rendered the entity at, and some position derived from the last update. Because we are drawing the entity forward extrapolated to our current time frame, the actual position we get out of the update will need to be extrapolated by the time until the next update, to give us a point to aim for. This method is implemented in the template class Extrapolator which is included in the EPIC download.
Source code and project files are included for Visual Studio 2003 and Visual Studio 2005; you can probably modify it to run with other compilers with little work. The extrapolator class should build on any modern compiler (including Linux/GCC) but the demo application is a Win32 application.
The code from EPIC is released under the MIT license, which means that it's Open Source, and also that you can use it in commercial products without any risk of "tainting" your code. All you have to do is agree to not sue me over this code (and agree that your customers also can't sue me), and include a small copyright notice in any credits or documentation page accompanying your code.
You can contact me as hplus0603 on the site GameDev.Net.