1
\$\begingroup\$

I'm working on my Java game with Libgdx and having trouble with the network code. I can create a server, make multiple clients connect to it, but there is a huge delay between the player's input and the movement.

This is basically how it should work:

  1. SERVER: (Launch and listen to connection.)
  2. CLIENT: I want to connect
  3. SERVER: OK, here is the map and all other data
  4. CLIENT: I pressed "D"
  5. SERVER: OK, I move this player, here is your position
  6. CLIENT: I received my position

On the 2 last steps, when I pressed an input, the server gets it correctly and updates the position and immediately sends the position to the player, but here is my problem:

Server log (the numbers are X- and Y-coordinates):

Sat Sep 20 18:49:16 CEST 2014 : Send 2356.0 1376.0
Sat Sep 20 18:49:16 CEST 2014 : Send 2358.0 1376.0
Sat Sep 20 18:49:16 CEST 2014 : Send 2360.0 1376.0
Sat Sep 20 18:49:16 CEST 2014 : Send 2362.0 1376.0
Sat Sep 20 18:49:16 CEST 2014 : Send 2364.0 1376.0
Sat Sep 20 18:49:16 CEST 2014 : Send 2366.0 1376.0
Sat Sep 20 18:49:17 CEST 2014 : Send 2368.0 1376.0
Sat Sep 20 18:49:17 CEST 2014 : Send 2370.0 1376.0
Sat Sep 20 18:49:17 CEST 2014 : Send 2372.0 1376.0
Sat Sep 20 18:49:17 CEST 2014 : Send 2374.0 1376.0
Sat Sep 20 18:49:17 CEST 2014 : Send 2376.0 1376.0
Sat Sep 20 18:49:17 CEST 2014 : Send 2378.0 1376.0
Sat Sep 20 18:49:17 CEST 2014 : Send 2380.0 1376.0

Client log:

Sat Sep 20 18:49:16 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:16 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:16 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:16 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:16 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:16 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:16 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:17 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:17 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:17 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:17 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:17 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:17 CEST 2014 : Receive : 2336.0 1376.0 

As you can see, the player receives the data at the wrong moment. Here is when the client start to receive

Sat Sep 20 18:49:18 CEST 2014 : Receive : 2336.0 1376.0 
Sat Sep 20 18:49:18 CEST 2014 : Receive : 2338.0 1376.0 
Sat Sep 20 18:49:18 CEST 2014 : Receive : 2340.0 1376.0 
Sat Sep 20 18:49:18 CEST 2014 : Receive : 2342.0 1376.0 
Sat Sep 20 18:49:18 CEST 2014 : Receive : 2344.0 1376.0 

As you can see, there is almost a 2 seconds of delay (something like 1.6 / 1.7s).

This is how I send/receive data:

Sending:

synchronized(this.gestionPlayers) {
   for(GestionPlayer gp : this.gestionPlayers) {
        if(this.serializablePlayer.size() > 0)
            gp.sendPlayer(this.serializablePlayer);
    }
}

sendPlayer function:

try {
    this.objectOut.writeObject(sp);
    this.objectOut.reset();
} catch (IOException e) {
    this.actif = false;
    e.printStackTrace();
}

Receiving:

Vector<SerializablePlayer> p =
    (Vector<SerializablePlayer>) this.objIn.readObject();
this.map.addNewPlayers(p);
this.map.multiplayerRender();

SerializablePlayer contains the player's ID and coordinates.

If you need more information, just ask. This is the last step in my multiplayer game and I'd be happy if you could help me! :)

\$\endgroup\$
2
\$\begingroup\$

Your server is more authoritative than it needs to be. The client needs to start moving as soon as the button is pressed, so there is no perceived lag. The server can then essentially replicate packets for other players, (maybe run a physics-sanity-check against a stored world model to prevent cheating, like warping through walls and such). Time stamping the packets and implementing motion prediction helps smooth out player motion on other players' screens.

This GDC presentation (1.5 hours) goes into fine detail on how client/server latency impacts perceived gameplay. It's worth a watch just to be aware of the potential problems of making portions of the gameplay client-authoritative vs server-authoritative, and how to modify game mechanics to hide or eliminate perceived lag.

\$\endgroup\$
  • \$\begingroup\$ While I agree that clientside prediction is generally necessary, I think there's something else going on; a roundtrip time of two seconds is highly irregular, unless there's some serious network congestion. \$\endgroup\$ – Chaosed0 Sep 24 '14 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.