I am working on a multiplayer game which uses UDP protocol in Java. I have a server and multiple clients. The clients connect to the server and they each have their player. When one client moves a player, the position of it is sent to all the other clients connected to the server. The positional data isn't sent from the client to server and then from server to all clients. It is sent directly from the client who moved the player to all other clients connected to the server. The player is then moved in all clients windows according to the positional data sent. I know packet loss exists and there is delay in receiving packets. But I dunno how other games manage to smooth out their multiplayer player movement. Can you explain how they do it and how I can go on implementing it my self.
The bottom code is happening in the client who hasn't moved. this is the client who has received the positional data of the client who HAS moved. The bottom code runs when ever the other client moves.
// here is some pseudo code of how I manage my player movement.
// This packet contains the x and y position of the player who has moved.
Packet receivedPacket = new Packet(recievedData);
// this is the player which represents the player who has moved.
player.setX(receivedPacket.getX());
player.setY(receivedPacket.getY());
The problem is there is very laggy player movement especially if the clients are on different computers. This is most probably due to packet loss and packet delay across the network. I have tried tweaking the player's position to the position that is received by the client. But still there is some lag. Please help me.