Search code examples
network-programmingsynchronizationgame-physicsmultiplayer

Sync physics with a game server


I wanted to write a multiplayer game using a physics engine. The client uses the physics engine for calculations and displays the results just in time, but of course, the server needs to simulate the whole thing, too.

Afaik, clients usually send velocity and position to the server. My idea was to send the key presses to the server instead.

Sending positions/velocity:

  • Server needs to interpolate the movement.
  • I have no idea how to feed a physics engine with this data.
  • On interpolation, the results might not be exact.

Sending key presses

  • I can feed the server's physics exactly like on the client (by key presses). This means less code!
  • No interpolation needed.

So, what could be bad with my idea? Why isn't it (afaik) used in games?


Solution

  • It's not used for two main reasons - one outlined by user1640050, the other one is far more substantial:

    Latency calculation - you would still need to calculate it both client-side and server side, since on internet, there is going to be a 20ms-500ms lag between server and the clients. What usually happens is that both sides calculate the physics, and the actions (not really keypresses) are being sent across the network, timestamped with the client's time. Then, the server does the calculation again, compute any interactions between clients and sends corrections to trajectories back to the client.

    This way the client can calculate realtime evolution of the game without any server feedback, and it will be corrected if the server informs the client about other players' activities, usually in a combination of actions and results of an action (more common).

    This is done to discourage cheating of various sorts - the less the client knows about say engine thrust of the opponent, the harder it becomes to add unauthorized mods and cheats - this way the client knows about it's own engine thrust, and the positions/velocities/accelerations of the opponents, but doesn't know how it came to life.