Multiplayer tutorial 1: concepts

Favourite 220 favourites
Tutorial written by AshleyOriginally published on 3rd, March 2014 - 4 revisions

Lag compensation

Consider the following problem: player A is standing still and aiming at player B, who is running to the right. Player A hovers the mouse exactly over player B and shoots. (Assume the shot is instant-hit, like a laser or infinite speed projectile.) The message to the host indicating they are shooting takes 100ms to get there. During that time, player B keeps moving to the right. The host receives the message 100ms late and tests for a collision where player A is aiming - but they miss, because player B has moved on! Player A saw themselves give a direct hit, but they never caused any damage.

In general, the host sees each player do everything a bit late because of their latency, and players end up missing moving targets. This kind of issue will infuriate players, who eventually learn that they actually need to aim for the empty space ahead of where the player is moving. This is not very realistic or fair. Lag compensation is a technique to avoid this result.

Rewinding time

The host knows how far behind each player is seeing the game, because it is measuring everyone's latency. So when the host receives the message indicating player A is shooting, it knows they actually did that 100ms ago.

The host is remembering the past few seconds of each object's history. It can then look back and see where player B was 100ms ago. Then it can test if player A scored a hit on player B at the time they originally fired. This is how the latency of player A is compensated for by the host, and it means player A likely scores a hit on the host when they see themselves score a hit on their own screen.

Lag compensation in a multiplayer game
This creates another problem though. Player B is on a delay as well. If they have a 100ms latency, then the host is moving them about 100ms behind where they see themselves (remember their local input prediction moves them straight away). On top of that, the host is moving them further behind when testing for a collision when player A fires.

This means player B sees player A aiming behind them where they would miss, firing, and then they get hit anyway! In extreme cases player B may have in fact reached shelter from player A so they can't be directly hit, and then they suddenly take damage. This is annoying for player B, but is usually not as bad as having no compensation - at least players actually have to aim directly at each other to hit. A common solution is to not clearly indicate where players are aiming, so player B can't easily see player A appears to be aiming behind them, making it look more acceptable when they are hit.

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.


stefanos 3,482 rep

Yeah that's nice :)

Monday, March 03, 2014 at 8:36:42 PM
Vallhalen 3,067 rep


Monday, March 03, 2014 at 8:43:51 PM
Leandrus 6,001 rep


Monday, March 03, 2014 at 8:55:27 PM
SgtConti 4,941 rep

Very interesting, hopefully the beta release comes tomorrow ;)

Monday, March 03, 2014 at 9:02:03 PM
DUTOIT 12.8k rep

Fantastic :)

Monday, March 03, 2014 at 9:05:34 PM
emoaeden 10.3k rep

I can't wait to try this !

Monday, March 03, 2014 at 9:07:53 PM
NeoBiel 4,639 rep

Meu sonho se realizando...

Monday, March 03, 2014 at 9:08:29 PM
albrektv 8,606 rep

Thank you for the lesson that you are waiting for him

Monday, March 03, 2014 at 9:11:31 PM
valdarko 6,357 rep

how it will handle data save, like itens and stuff?

Monday, March 03, 2014 at 9:33:35 PM
Joskin 6,129 rep

Yes ! Thanks a lot !

Monday, March 03, 2014 at 10:23:48 PM
jobel 17.2k rep

I'm very excited to go through this! thanks!

Monday, March 03, 2014 at 10:35:05 PM
DatapawWolf 30.9k rep

I notice this does not address the subject of host cheating. Preventing the clients is nice and all, but, for example, say the host hacks their client, they could now send whatever they want to the peers.

Now you could say that anti-cheating measures and checks could be implemented, but then you could apply that same logic to each client as well.

Thoughts, anyone?

Monday, March 03, 2014 at 11:17:34 PM
popyui 2,499 rep

Me gusta mucho esto y me da una mejor idea de como funcionan mis juegos de mis consolas

Monday, March 03, 2014 at 11:19:48 PM
DatapawWolf 30.9k rep

Also, not to spam, but addressing the "Lag Compensation" techniques. I'm sorry but if a player has a bad connection in a shooter game, too bad for them. Newer games in the Call of Duty series have notoriously infuriating lag comp which is responsible for all sorts of nonsense.

In my opinion it's a technique that is often overused and should not be looked as much as interpolation or other ways of synchronizing between peers.

Monday, March 03, 2014 at 11:29:12 PM
DatapawWolf 30.9k rep

"By default updates are sent 30 times a second"
We will be able to modify this, correct? Or send out updates only when needed? It'd be wasteful to constantly spew out that much data for something like a grid/turn based game, where movement may be as low as 1 grid square per 100ms, or not at all when a player is AFK.

Monday, March 03, 2014 at 11:32:42 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.