Network delay compensation

Hi, guys.

I beg your pardon, my English is far from perfect, still I need your help.

Currently I’m trying to develop a networked clone of space invaders and i’ve got some troubles with synchronization issues.

Each time player presses or releases a button I send a message that holds a data about ship position and movement direction. For example typical message may contain info like this: [x=28 y=50 direction=northwest].

After receiving such message client assumes that ship is moving from specified position in a specified direction. The client will move a ship until another message arrive.

When another message arrives local ship position is not the same as actual position according to message data. Local ship moved a bit further because of network delay. When I synchronize ship state, I simply move it to position I got from message.

So, when one of the players changes the movement direction, other player sees an ugly “jump” of first player’s ship.

What are my options to make my game look smoother?
Thanks in advance.

The reciever could buffer the message and wait before interpolating the position. If it keeps track of when the position was sampled on the sender and when the message was recieved, it can compensate to create a smooth animation.

It is hard to explain, but I’ll try with an example. Lets assume the sends the following messages
sender time 3000, position 0 0
sender time 3100, position 1 0
sender time 3200, position 2 0
sender time 3300, position 3 0

This is what happens on the reciever if playing back with 200ms delay (btw, its clock is not synchronized with the sender)
recieving time 1000, gets sender time 3000, position 0 0
recieving time 1150, gets sender time 3100, position 1 0
time 1200, add interpolation between (0, 0) and (1, 0) during time 1200 and 1300 (1200 is local start time, 1300 is local start time + (3100 - 3000) = 1300)
recieving time 1210, gets sender time 3200, position 2 0
time 1210, add interpolation between (1, 0) and (2, 0) during time 1300 and 1400 (1300 and 1400 is calculated using local start time and difference in send time)

time 1400 animation ends
recieving time 1500, gets sender time 3300, position 3 0
time 1700, add interpolation between (2, 0) and (3, 0) during time 1700 and 1900
tim 1900 animation ends

This will produce a smooth animation without teleportation.

Thanks for your advice.

I’ll try to do it as you supposed. :slight_smile:

Sorry, tried to get your idea with pencil and paper, but failed to.

Could you explain it some other way? If you don’t have time for that maybe you could point some tutorials or sources that describes your idea. Today I realized, that making a protocol based on current position plus direction is not cheat-safe. Client can pretend to have any position. This seems not to be right.

Maybe I am at the wrong path and idea of sending direction is incorrect? How would you implement a network protocol for a game like pong or space invaders?

Ok, now back to interpolation.

So let’s say we have client A that performs movement and client B that listens to movement.

B receives a series of messages:

  1. [position=1, moving forward] time
  2. [position=3, stop]
  3. [position=3, moving backward]
  4. [position=-2, stop

How should client B display the position of A to avoid “teleportation effect”

PS
It seems to be the very basic issue of real-time online game development! I searched Google for some examples, tutorials or sources on using NIO for such kind of games. All I’ve found was the games like chess or othello where real time issues are not critical. Once again I’ll be grateful for any suggestions on the subject.

huh? depends on the game… but client could have any position, that’s the point. If client turns north, then he turns north. For this to be used as a cheat there should be a cheat that turns client, kind of like aimbot in fps games. There is no way you can prevent this on the server.

About making networking smooth…
So client send a message and server gets it. Server can’t just directly process the data from the message because it knows message arrived later then data it contains happened. You need to know how much later. This can be done with synchronizing clocks (like tom proposed) or directly with calculating latency (which is basicly same thing) which I’ll try to explain now. So let’s say you know that latency (ping) is 50 ms. This is time needed for message to get to server and come back if it’s sended back instantly (I think that is it). So your message is late ~25ms. This is where you use dead reckoning (interpolation). You take the data from the message and try to calculate where/what will data be 25ms in future. If it’s movement, you take direction and speed and calculate where would player be 25ms if he continued with that speed and direction. Calculation isn’t something extra you need to code, since your game already moves players in game engine, you just need to modify that so it works for networking also. For example, your game has 100 updates per second, if message is late 25ms that means you need to do extra 100 / (1000 / 25) = 2.5 updates on received data and then apply the data.
I’m no expert, only wrote one game with networking, I hope this is clear.