1
\$\begingroup\$

I'm trying to handle all the physics on the client and correct data on the server.

e.g.

Client_1 wants to move right
Server broadcasts that
Clients simulating Client_1's movement
Client_1 sends it's coordinates to the server (every 100ms)
Server validates coordinates based on client's last position and last update (which is stored on the server)
Server corrects Client_1's coordinates on every client

But when FPS<60 players are moving faster than they should, which causes server to teleport them back.

My question is basically how to check x,y without teleporting players with high latency (or low fps).

Server:

var now = Date.now();
var clientX = ...x position sent by client...
var clientY = ...y position sent by client...
var timeDiff = Math.ceil((now - player.lastPositionUpdate)/5)*5;
var timeDiff = timeDiff > 1000 ? 1000 : timeDiff;
var pixelPer100 = player.speed/10;
var maximumPixelDiff = (pixelPer100/100) * timeDiff;
player.lastPositionUpdate = now;

if (clientX > player.position.x){
    var diff = clientX - player.position.x;
    newX = player.position.x + (diff > maximumPixelDiff ? pixelPer100 : diff);
}
..and more

Client:

update: function(){
    player.body.velocity.x = player.speed;
    tick++;
    if (tick == 6){
        tick = 0;
        sendPositionToServer();
    }
}

I'm using Phaser with Arcade Physics.

\$\endgroup\$

Your Answer

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

Browse other questions tagged or ask your own question.