Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- 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
- update: function(){
- player.body.velocity.x = player.speed;
- tick++;
- if (tick == 6){
- tick = 0;
- sendPositionToServer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement