I'm writing a simple online pong game for two players playing over the network. It's a client-server application, with a game logic on the server side. I have some problems with game synchronization on the client side, and the result is pretty unsatisfying. This is how it currently works:
This method doesn't work very well as it moves the game objects back and forth on the client side. Any ideas how to improve it?
Admit you have a latency of 30ms between client and server. The client send "I wanna move my racket down" and its racket is at y=100px with a velocity of 0.1px/ms
30ms seconds later:
20ms seconds later:
30ms seconds later:
At this point, the client racket "jumps" backward from 108 to 102px...
How to cope with the network latency? Two complementary ways:
The first aproach is used when the effect on the client is hardly linked with the result and cannot be "rollback". Example: when a client shots a missile, its not addmitable that this missile is being suppressed by the server on the next update because the client had in fact no more missile stock. So in this case, client application will launch the missile only afer the server sent an acknowlegment.
The second is always used to avoid these "jumps" when server sync. You have to monitor network latency to predict movment of your game elements. Here two ways: compute the ping or synchronize server and client time at startup (the easiest and more robust way). The general idea is:
30ms later:
Perfect! They are synchronized.
20ms later:
30ms later:
Again, client and server are synchronized!
Desync can happens on the other client when the first client stop moving. In this case the player racket will "jump" a little bit. When this is not critical, it's possible to smooth this transition.
Hope it will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With