Issues with synchronisation of player movement on a multiplayer server

before people mention that such questions have been asked I would like to tell that yes they had been but I am trying to find an answer which suits my situation much better.

I am working on a networked game in which player moves 32 pixels every time they press a direction key (That is they move 1 tile at 1 press of key). My client does collision check etc and then send a packet to server with the direction player pressed key. The client moves the player 2 pixel a frame and hence it takes 16 frames for client to move a tile and while the player is moving, it cannot send any movement update to server.

The server on the other hand as soon as receives a packet, queues it up for the player. I have movement threads running which run every 2ms and check for every player queue and then if player has movement, it updates the other client as well as checks whether player is on correct tile (making sure he is not cheating). The server also checks if player is on a warp tile (there are tiles on which if you step you will be warped to the other map) and other such tiles which the server checks. The server does this by adding (+32, -32) in x or y co-ordinates depending on direction and then checking the new tile type etc. If the player is on an incorrect tile, he was synced back to original tile.

Everything was working fine until I tried it on a real network. Now i receive lag for example when there is a warp to a new map, sometimes the player and server goes out of sync.

Basically there are many synchronization issues I am getting and I am wondering should I rather go to an authorative-server based approach where server will tell client when to move and when not to move. I can post code if needed.

I am using TCP protocol because the game doesn’t need to be Real-time. What’s important is reliability of messages.

I have the one question at the moment :
Why do you use multi-threading in your app? (Movement threads)

Yes because i know how to use multi-threading.! Multi-threading is not an issue here at all. I am good at these java concepts :slight_smile: Its just MMORPG where I am new in and finding weird issues.

There is no way to eliminate these issues. If you’re playing over the internet, you
will occasionally encounter round trip delays that are unbelievably long. I mean seconds long. I recommend a few things.

First, disentangle your game engine “move to this square” from the
real time interface “user hit a key”.

Second, if the actions are supposed to be simultaneous, you’ll need a
system to reassemble the normalized time sequence after the fact. To
present a reasonable interface to the user, time has to move on at 1:1,
but when new actions arrive from the network, which took place “before”
the current time, you must unwind and replay in the correct time sequence.
This will occasionally result in your local user thinking he has succeeded in
some action, only to find that he hasn’t.

You shouldn’t be using timed threads to move the player around, as Stranger said. Just use math.

Here is an example of a function you could use to animate the player between two tiles :

player.x = startTileX + (targetTileX - startTileX) * Math.min(1, (System.currentTimeMillis() - animationStartTime)/animationDuration);

Where :
player.x = your player’s x coordinate
startTileX = the x coordinate of tile that the player is moving from
targetTileX = the x coordinate of the tile the player is moving to
animationStartTime = the time when you started the animation of the player moving from one tile to the next
animationDuration = how long it should take for the player to move from one tile to the next

As to your performance problems, your client should be animating the player, not just waiting for the server.
You should have your client interpolating frames between packets (essentially guessing where the player should be), so that even if the network is slow, your game remains smooth.