# Client Side Prediction Keeping Movement In Sync With Server?

## 12 January 2016 - 1 answer

I am working on a multiplayer game at the moment. I am experiencing an issue with my client side prediction. After moving around for a while, the server position and client position are offset. Here is the game currently. The server position is shown in orange. Your local position is shown in white. What I am doing, is simply simulating the players movement locally and on the server. Here is the movement code:

``````var xMov = 0;
var yMov = 0;
if (player.keys.up) {
yMov -= 1;
}
if (player.keys.down) {
yMov += 1;
}
if (player.keys.right) {
xMov += 1;
}
if (player.keys.left) {
xMov -= 1;
}
xMov /= REFRESH_INTERVAL;
yMov /= REFRESH_INTERVAL;
var length = Math.sqrt(xMov * xMov + yMov * yMov);
if (length != 0) {
playerMoved = true;
xMov /= length;
yMov /= length;
xMov *= player.speed;
yMov *= player.speed;
player.deltaX += xMov;
player.deltaY += yMov;
} else {
playerMoved = false;
}
var oldX = player.x;
var oldY = player.y;
player.deltaX *= c.speedDecel * delta;
if (player.deltaX <= 0.1 && player.deltaX >= -0.1)
player.deltaX = 0;
player.deltaY *= c.speedDecel * delta;
if (player.deltaY <= 0.1 && player.deltaY >= -0.1)
player.deltaY = 0;
player.x += player.deltaX * delta;
player.y += player.deltaY * delta;
player.x = Math.round(player.x);
player.y = Math.round(player.y);
``````

The code on the client and server side is identical. When a user presses a key, the key information is sent to the server:

``````sendDataArray = key;
sendDataArray = val;
socket.emit('3', sendDataArray);
``````

Then on the server, the key value is set. The problem I think I am having is a timing issue on the client and server side. The movement method shown above is called from a setInterval on both the client and server side:

``````setInterval(move, 31);
``````

So when I press the key, on the client side, it could be 5ms until the next move() is called. Whereas on the server, when he receives the key info, it may be another 30ms. This might be causing an inconsistency.

How could this be resolved?