I'm working on a 2D top-down-shooter and doing my best to copy concepts used in networked games like Quake 3.
- I have an authoritative server.
- The server sends snapshots to clients.
- Snapshots contain a timestamp and entity positions.
- Entities are interpolated between snapshot positions so movement looks smooth.
- By necessity, entity interpolation occurs slightly "in the past" so that we have multiple snapshots in which to interpolate between.
The problem I'm facing is "clock synchronization".
- For simplicity-sake, let's just pretend for a moment that there is zero latency when transferring packets to and from the server.
- If the server clock is 60 seconds ahead of the client clock, then a snapshot timestamp will be 60000ms ahead of the client local timestamp.
- Therefore, entity snapshots will collect and sit for about 60 seconds before the client sees any given entity make his moves, because it takes that long for the client clock to catch up.
I've managed to overcome this by calculating the difference between the server and client clock each time a snapshot is received.
// For simplicity, don't worry about latency for now... client_server_clock_delta = snapshot.server_timestamp - client_timestamp;
When determining how far along into the interpolation the entity is, I simply add the difference to the client's current time. The problem with this however, is that it will cause jerkiness because the difference between the two clocks will abruptly fluctuate due to snapshots arriving quicker/slower than others.
How can I synchronize the clocks closely enough that the only perceivable delay is that which is hard-coded for interpolation, and that which is caused by ordinary network latency?
In other words, how can I prevent interpolation from starting too late or too soon when clocks are significantly desynchronized, without introducing jerkiness?
Edit: According to Wikipedia, NTP can be used to synchronize clocks over the internet to within a matter of a few milliseconds. However, the protocol seems complicated, and perhaps overkill for use in games?