What is lag compensation?

Specifically in regard to online games, what is lag compensation? Should it affect the way I play?


Solution 1:

Valve published an excellent article explaining the multiplayer network details (also called "netcode") of their multiplayer games. From this article:

Lag compensation

Let's say a player shoots at a target at client time 10.5. The firing information is packed into a user command and sent to the server. While the packet is on its way through the network, the server continues to simulate the world, and the target might have moved to a different position. The user command arrives at server time 10.6 and the server wouldn't detect the hit, even though the player has aimed exactly at the target. This error is corrected by the server-side lag compensation.

The lag compensation system keeps a history of all recent player positions for one second. If a user command is executed, the server estimates at what time the command was created as follows:

      Command      Current      Packet          Client
     Execution  =  Server   -  Round-Trip  -     View
       Time         Time         Time        Interpolation

Then the server moves all other players - only players - back to where they were at the command execution time. The user command is executed and the hit is detected correctly. After the user command has been processed, the players revert to their original positions.

This screenshot was taken on a listen server with 200 milliseconds of lag (using net_fakelag), right after the server confirmed the hit. The red hitbox shows the target position on the client where it was 100 milliseconds ago. Since then, the target continued to move to the left while the user command was travelling to the server. After the user command arrived, the server restored the target position (blue hitbox) based on the estimated command execution time. The server traces the shot and confirms the hit (the client sees blood effects).

There's more information in the article. If you're interested in this, I highly recommend reading the whole thing. It's not too long, and among other things it also explains entity interpolation (which is a part of the above equation) and input prediction. Keep in mind Valve is responsible for the netcode behind some of the most popular online games today - Counter Strike and Team Fortress 2 - so this is probably as professional as it gets.

The source code implementing this is publicly available as part of the Source SDK.

Solution 2:

Lag compensation is a feature of modern games that allows the engine to estimate where players would be, based on their ping.

For example, if a user is running forward at 10 feet per second, and their ping is 1000ms, you will see them continue to run forward for that whole second or until you receive another update of their position and direction.

This prevents users from looking like they are jittering around. However, in cases of extreme lag, "rubber banding" can occur.