This is a note after reading Fast-Paced Multiplayer by Gabriel Gambetta. Thanks to the author, it also has a Demo.
Summary
- Use client prediction to simulate animation before server responds.
- Reconciliate un-acked client messages by server responding last acked message ID.
- Entity interpolation to trick clients events happening between server’s time steps.
Game server’s state is authoritative to prevent cheats. RTT (round-trip time) between client and game server is unavoidalbe so we need mechanisms to make clients appear as if RTT doesn’t exist.
Game server also has a tick rate to update its state and broadcast the snapshots because the compute and bandwidth is limited.
Prediction
When a user presses forward button, message is sent to server, server applies the movement to the game state and broadcasts to players. RTT is noticeable to user so it is not acceptable if the player does not move until it receives a response for a live-action game.
Client mitigates this by predicting the movement so the player moves as soon as the key is pressed. Assuming player speed is constant, client can predict the movement if it runs the same simulation as server’s.
Reconciliation
This works fine until client’s prediction does not match with server’s snapshot because of high RTT. If we assume 250ms RTT and client’s movement animation of 100ms, then player moves two units after 200ms. However, with 250ms, the first movement is applied on the game server and echo back to the client at 250ms and second at 350ms. This is a problem because at 200ms, client predicted player moved two units but at 250ms, it received authoritative response from server it only moved one unit.
This is where client and game server needs to reconiliate. Game server needs to respond the last message it processed so the client knows which messages are not applied. In the previous example, if server at 250ms responds with lastMsg:1, then client knows that the second message is not yet processed and does not adjust the player position.
Without reconciliation, player will move at a slower rate because of dropped messages and appear glichy as player is kept moved backwards as client “corrects” the position with the server’s.
Interpolation & Lag Compensation
Due to the limited CPU and bandwidth, the server updates at an interval. This interval is called time step. In some games, movements needs to capture all activies that happen in between the time step. For example, a player jumps, or moves left and right quickly.
If the client renders the latest snapshot, it would make the game appear jittery or glitchy.
Trick here is to show current player the past snapshots, and interpolate from the last two snapshots.
Immediate question I had when reading this was “wouldn’t current player have advantage?”. The answer is clearly, no. The server has the previous snapshots so it can use that to simulate the the state of the entities. Valve’s Source Multiplayer Networking contains more in-depth explanation.