
- Parts I, II (single player with authoritarian server)
- Part III (The appearance of the enemy)
- Part IV (Headshot!)
How to hang the perfect headshots if you have a ping for 2 seconds? You will learn in this article.
The current algorithm of the multiplayer
- The server receives commands from clients and their departure times.
- The server updates the state of the world
- The server with some frequency sends its status to all clients
- The client sends commands and reproduces their result locally.
- The client receives updated world states and:
- Apply state from server
- Reapply all their commands that the server did not have time to apply.
- Interpolates other players' previous states.
- From the player’s point of view, there are two serious consequences:
- The player sees himself in the present.
- The player sees others in the past .
This usually works fine, but it becomes a big problem for events that need high space-time accuracy. For example, if
you want to smash the enemy head off !
Lag compensation
Here you have aimed your sniper rifle straight at your opponent's head. You exhale quietly pulled the trigger. You are confident in your abilities, you can not miss.
But you missed!
')
Why did this happen? How did this happen?
The fact is that in the architecture that we built, you aim at the place where the head of the enemy was 100 ms before the shot, not at the time when you shot!

It's like being a sniper in a world where the speed of light is insane, just negligible. You are aiming for an opponent, but by the time the light reached you, it was long gone in the old position.
Thank God, there is a fairly simple solution for this problem, which is also acceptable for other players most of the time (with one exception, which will be discussed later).
Here's how it works:
- When you shoot, the client sends a server shot event containing the server time of the state that was displayed at the time of the shot. And the exact direction of your weapon.
- This is a KEY moment. Since the server still sends its status every frame to clients, it can recover any moment from the past. In particular, he can restore the world exactly as the client saw him at the moment of the shot.
- Thus the server leaves the arrow in the position in which it is located; and the rest of the world changes to the state that the client displayed at the moment of the shot, i.e. returns back in time .
- The server processes the shot and gets its result.
- The server returns the whole world to the current state of time.
- The server applies the result of the shot and sends updates to the clients as usual.
And everyone is happy!

The server is happy because it is a server. He is always happy, because he is always right.
The shooter is happy because he aimed at the enemy’s head and blew it with one accurate shot.
Killed - the only one who may be unhappy. If he stood on the spot at the moment of the shot, he himself is to blame. If he ran, then you are a good shooter.
But what if he ran across the danger zone, and a split second later he was shot dead when he thought he was safe? He will be distressed.

Here it can happen. This is a compromise we have to make. Since the shooter shot him in the past, a player may be shot after he has already hid behind a corner.
It sounds dishonest, but we will remember that from the point of view of the shooter this player ran across the square for as much time as he did from the point of view of the player. The only difference is the delay.
It would be much worse if the exact shot did not work!
We bring to the point of absurdity
It is important to note that this method works even with a giant ping.
Attention, the trick is done by professionals! Do not perform this trick on a production server! Teams with a two second prescription do not need to be executed in real life.
Conclusion
Such a thing as multiplayer is difficult to do right, but if you are armed with an understanding of the concepts described, there is nothing crazy in implementation.
Materials for further reading (English):
Gaffer On GamesValve's latency compensation methodsFrom translator
This concludes the series of articles from Gabriel Gambetta .
But I'm not saying goodbye to you. I'm still going to write an article about how to build a serialization of the game world, and in general you never know where research of such an interesting topic as multiplayer will lead you!UPD
krimtsev asked a very reasonable question.
FPS and PING understand how to influence this, but what do you think about the TICKRATE account?
I answer. With
bogotoff, we allowed ourselves to deviate from the described algorithm, since it seemed inconvenient for us to play at a server refresh rate 10 times per second.
Since we use interpolation on the client to display the positions of enemies, it would not be entirely correct to apply a specific server state that the client allegedly painted, because in fact the player was shooting at an enemy that was between two different states.
Since we use the lag compensation mechanism, it can be used to find two states of the world between which a shot occurred and interpolate them.
Thus, you can adequately shoot even with one server update per second:

Although the game at this server frequency, of course, looks awful. Do not do this. In general, keep the FPS as high as possible. The higher the sampling rate of the game world, the better the gaming experience the player will have. Although my personal IMHO that above 60 - no longer makes sense.