1
\$\begingroup\$

So I'm working on a real time multiplayer game in NodeJs (Client and Server). Both loops handle the same "physics" (movement at a constant rate) and both are running at 40hz or 40 times per second. I am using setInterval on both client and server with a delay of 40ms. The issue is that my server average delta between each tick is ~41 and my client average delta between each tick is ~40. This leads to results like this:

enter image description here

See in the last result, there is a difference of 46 ticks meaning the client is rendering about 1.8 seconds ahead of server time. My first guess on solving this is to sync the client every so often but that would lead to it "teleporting" backwards quite often due to how much it is desyncing.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You've constrained the problem (specific to your ENV and your current attempt), but regardless this represents a massive, longstanding challenge in multiplayer networking that is difficult to solve in the abstract. The solution will probably depend more on tactics that address the specific needs of your game rather than on any "correct" answer. So it might not be a great question for SO's question format. That said, there are many questions here on the subject, a lot of research available elsewhere. For example, mod Andrew Russell has a whole video series on his solution to the problem. \$\endgroup\$ Dec 23, 2018 at 21:14

1 Answer 1

0
\$\begingroup\$

Rather than coupling the time component of your physics engine with this abstract notion of a game loop tick, could you use the system time? One second on machine X will always be the same as one second on machine Y.

let currentTime = Date.getTime();

// Then in your loop
let previousTime = currentTime;
currentTime = Date.getTime();
let delta = currentTime - previousTime;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .