09/12/2023
Lag Compensation!
Why?
Let's imagine a scenario when networking latency exists and player A shooting Player B a hit can't be missed
But he missed it , how ?
While the player A shooting player B in his view player B was in the past position compared to his position in the server which player A hasn't received it yet due to the networking latency he has.
Player A shoots player B while player B is in his past position and when the shoot reaches the server for hit Detection player B wasn't facing the shoot direction because he is already moved in the server.
To deal with this problem I implemented a networking mechanism called Lag compansation.
How lag compensation works ?
When you shoot, client sends this event to the server with full information: the exact timestamp of your shot, and the exact aim of the weapon.
Here’s the crucial step. Since the server gets all the input with timestamps, it can authoritatively reconstruct the world at any instant in the past. In particular, it can reconstruct the world exactly as it looked like to any client at any point in time.
This means the server can know exactly what was on your weapon’s sights the instant you shot. It was the past position of your enemy’s head, but the server knows it was the position of their head in your present.
The server processes the shot at that point in time, and updates the clients.
And everyone is happy!