In a networked game, you typically need to keep a single concept of logical
time, at which game events happen. The physical clocks of the client and
server machines are likely set to different values, so you need to keep an
adjustment value to get to logical time from physical time.
I'm assuming that client and server attempt to keep one synchronized time (call
this logical time). This logical time is a measurement of how far the
simulation has progressed since start (or some assumed start, sometime back in
time).
For a simple implementation of this, the client can add its physical time (the
actual clock) to each outgoing packet, and the server, when receiving the
packet, can remember that value, as well as the server logical clock at time of
receipt. It puts both those values, as well as the server logical clock at
send, in the next packet to the client. The client can then use these values to
calculate both RTT (ping) and offset from its physical time to get logical
time.
|
Client Logical Time = Client Physical Time + Client Offset to Logical Time
Client->Server:
CP1 = clientPhysicalClock()
send( CP1 )
recv( CP1 )
SL1 = serverLogicalClock()
...processing...
Server->Client
SL2 = serverLogicalClock()
send( CP1, SL1, SL2 )
recv( CP1, SL1, SL2 )
CP2 = clientPhysicalClock()
ServerProcessingTime = SL2-SL1
RTT = CP2 - CP1
OneWayPing = (RTT - ServerProcessingTime)/2
ClientOffsetToLogicalTime = ((SL2+OneWayPing-CP2)+(SL1-OneWayPing-CP1))/2
You might want to smooth your estimation of the actual offset, to avoid too
abrupt changes, and too much jitter because of a single delayed packet.
There are fancier way to calculate client/server time synchronization; for
example, look at the way that NTP calculates
time.
|
| |
|