Simple online pong sync online

I am writing a simple online pong for two online players. This is a client-server application with server-side game logic. I have some problems with client-side synchronization of the game, and the result is rather unsatisfied. Here's how it works right now:

  • On the server side, I have a game object in which the positions of the players and the ball are stored, each object has its own position x, y and speed x, y. Based on this position, objects are updated in a loop. On the client side, there is the same local object with the same data, and it is also updated in a loop.
  • When a player presses / releases up or down, the client sends a network packet with a single integer, so that the player’s object starts / stops to move in the game object on the server.
  • The server sends a synchronization packet every 50 milliseconds with the position and speed of all three objects. When the client receives this package, he accordingly changes the position of the game objects.

This method does not work very well because it moves game objects back and forth on the client side. Any ideas how to improve it?

+5
source share
1 answer

Assume that the client has a latency of 30 ms between the client and the server. The client sends "I want to move the racket down," and its racket is y = 100px at a speed of 0.1px / ms

30ms seconds later:

  • the client racket is at y = 100 + 30 * 0.1 = 103px
  • ( y = 100px )

20 . :

  • y = 103 + 20 * 0,1 = 105px
  • y = 100 + 20 * 0,1 = 102px
  • (102px)

30ms :

  • rarcket y = 105 + 30 * 0,1 = 108px
  • positon racket: 102px

"" 108 102 ...

? :

  • .
  • ( )

aproach , "". : , , , . , .

, "" . , . : ( ). :

  • " = 1265871" .

30 :

  • y = 100 + 30 * 0,1 = 103px
  • 30 ( ) y = 100 + * 0,1 = 100 + 30 * 0,1 = 103

Perfect! .

20 :

  • y = 103 + 20 * 0,1 = 105px

30 :

  • y = 105 + 30 * 0,1 = 108px
  • (105px ), 30 y = 105 + * 0,1 = 105 * 30 * 0,1 = 108

!

Desync , . " ". , .

, .

+17