00:00:00
programming is cool they said it'll be
00:00:02
fun they said
00:00:03
look i don't know who they is in this
00:00:05
scenario but they forgot to mention that
00:00:07
you can spend two days trying to figure
00:00:08
out why your code isn't working
00:00:09
wondering if your brain is
00:00:10
malfunctioning and just before you go
00:00:12
completely insane you realize that you
00:00:14
didn't read the documentation properly
00:00:16
and all it takes to fix it is changing
00:00:18
one word
00:00:22
ah yes the joys of programming
00:00:26
so
00:00:26
yeah making multiplayer games kinda
00:00:28
sucks
00:00:29
over the last few weeks i've been
00:00:31
working on re-implementing client
00:00:32
prediction into my game and player
00:00:34
movement is once again nice and
00:00:35
responsive but why was the movement
00:00:37
unresponsive in the first place and what
00:00:39
the heck is klein prediction well let me
00:00:41
introduce you to the arch enemy of all
00:00:43
game developers that are working on
00:00:45
multiplayer games latency
00:00:47
you're able to watch this video right
00:00:49
now because youtube servers are sending
00:00:51
the video's data to your web browser
00:00:53
however this doesn't happen instantly as
00:00:55
it takes time for that data to
00:00:57
physically travel from youtube to your
00:00:58
computer
00:01:00
that travel time doesn't really make a
00:01:01
difference when you're watching a video
00:01:02
or stealing someone's code but if you're
00:01:04
building a fast-paced multiplayer game
00:01:06
where reaction speed is critical latency
00:01:08
will literally make your job go from
00:01:10
easy to pure pain and suffering
00:01:12
imagine light travel 299 million 792 458
00:01:17
times slower than it actually does or at
00:01:19
one meter per second now imagine you're
00:01:21
standing a meter away from someone if
00:01:23
that person took a step to the right you
00:01:25
would only see them start to move an
00:01:26
entire second after they actually
00:01:28
started moving this is essentially the
00:01:30
same problem that games face because by
00:01:32
the time your computer is told that
00:01:34
another player moved that information is
00:01:36
already outdated you can probably figure
00:01:38
out for yourself why that would cause
00:01:40
major problems in games like first
00:01:41
person shooters where even a fraction of
00:01:43
a second of delay in another player's
00:01:45
position could cause you to be aiming at
00:01:47
nothing seriously when they were
00:01:48
creating the internet who decided that
00:01:50
latency was a good feature to add
00:01:53
in this video though i just want to talk
00:01:54
about movement a little delay wouldn't
00:01:57
make movement unresponsive on your end
00:01:58
right you could just have every player's
00:02:00
computer calculate and update their own
00:02:02
position the same way single player
00:02:03
games do it players would still see each
00:02:06
other in the past but at least they
00:02:07
would see themselves move immediately
00:02:09
after pressing a key
00:02:11
unfortunately this is where the second
00:02:13
half of the problem comes in cheaters
00:02:16
you can't trust your players because a
00:02:17
certain amount of them will happily
00:02:18
exploit anything they can and often this
00:02:20
will ruin the experience for other
00:02:22
players to prevent things like speed or
00:02:24
fly hacks you need your game server to
00:02:26
be the one calculating the movement for
00:02:27
all players as it's the only thing you
00:02:29
can trust
00:02:31
unless of course you're an incompetent
00:02:32
programmer in which case you can trust
00:02:34
nothing
00:02:35
so since the server has to handle your
00:02:36
movement for you that means when you
00:02:38
press the w key to move forward you have
00:02:40
to wait for your computer to send that
00:02:42
instruction all the way to the server
00:02:44
then the server does the calculation
00:02:45
updates your position and you have to
00:02:47
keep waiting until that update finally
00:02:49
makes it back to your computer all of
00:02:51
that might happen in less than a tenth
00:02:53
of a second but when you're at the
00:02:54
controls even that seemingly tiny amount
00:02:56
of delay can make the movement feel
00:02:58
sluggish this is where client prediction
00:03:00
comes to save us the idea here is to
00:03:02
have the client which is your computer
00:03:04
and the server both calculate your
00:03:06
movement while you're waiting for the
00:03:07
server to receive your input calculate
00:03:09
the proper movement and send back the
00:03:11
result your computer will also calculate
00:03:14
the movement and update your position
00:03:15
accordingly which means that when you
00:03:17
press a key you will move immediately
00:03:19
then when the server eventually sends
00:03:21
back what it determined to be your new
00:03:22
position we have your client compare
00:03:24
that correct position to what it had
00:03:26
predicted for the same point in time if
00:03:28
they match which should be the case most
00:03:30
of the time for a non-cheating client
00:03:32
then all is well with the world if they
00:03:34
don't match the position needs to be
00:03:36
corrected but because nothing is allowed
00:03:38
to be simple when developing a
00:03:39
multiplayer game it's obviously not as
00:03:41
easy as just overwriting your current
00:03:43
position
00:03:44
since your current position is predicted
00:03:46
and therefore essentially from the
00:03:47
future and the position from the server
00:03:49
is technically in the past the two can't
00:03:51
just be compared because when you're
00:03:53
moving they actually should be different
00:03:56
we're basically dealing with multiple
00:03:57
time streams meaning that time travel
00:04:00
doesn't just happen in books and movies
00:04:02
your computer is constantly doing it
00:04:03
every time you play a fast-paced
00:04:05
multiplayer game i won't go into any
00:04:07
more detail though because this is the
00:04:08
point where things get even more
00:04:10
complicated and confusing and i'd rather
00:04:12
get back to the practical side of things
00:04:14
remember how at the beginning of the
00:04:16
video i said player movement is once
00:04:17
again nice and responsive well i lied
00:04:21
sort of the movement is smooth and
00:04:23
responsive on land but
00:04:25
this is what happens when you step on a
00:04:26
boat why remember our good friend
00:04:29
latency yeah me neither because latency
00:04:32
is the enemy not our friend since the
00:04:34
server is the one calculating the
00:04:35
movement for the ships by the time a
00:04:37
position update reaches your computer
00:04:39
it's already outdated that means that
00:04:41
when you try to predict your movement
00:04:42
from onboard a boat you're doomed to
00:04:44
fail because you're working with a
00:04:45
different starting position than the
00:04:47
server is it's kind of like taking two
00:04:49
calculators and punching in different
00:04:50
numbers then adding one
00:04:52
even though you added one to both the
00:04:54
result will never be the same because
00:04:56
the starting numbers were different
00:04:58
basically you end up constantly
00:04:59
mispredicting your position which then
00:05:01
triggers a correction and sometimes that
00:05:03
correction will put you inside the
00:05:05
ship's collider because the ship
00:05:06
position is outdated and then the
00:05:08
physics system loses its mind because
00:05:09
colliders aren't allowed to be inside
00:05:11
one another but then that puts you in
00:05:12
the wrong position again so another
00:05:14
correction is triggered immediately
00:05:15
afterwards and it causes a vicious cycle
00:05:17
of violent jittering
00:05:20
so through the power of editing you've
00:05:21
been spared several more weeks of me
00:05:23
questioning why i decided to build a
00:05:25
multiplayer game or get into programming
00:05:26
in the first place but i'm pleased to
00:05:28
announce that i have successfully made
00:05:30
predictions smooth
00:05:31
even on boats since my prediction works
00:05:34
on dry non-moving land i just made the
00:05:36
boat also not move i have this duplicate
00:05:39
of the ship way under the water where no
00:05:40
one will ever run into it and when you
00:05:42
step onto the actual real moving boat
00:05:45
both the client and the server start
00:05:47
using this proxy to calculate your
00:05:49
movement you can think of it like being
00:05:51
teleported onto the duplicate boat
00:05:53
except visually you still see yourself
00:05:54
moving on the real one since the proxy
00:05:56
doesn't move there's no incorrect
00:05:58
predictions to deal with except when
00:06:00
getting on and off of boats because
00:06:02
that's when the system has to change
00:06:03
where and how it's calculating movement
00:06:05
the way it is now the jitter is pretty
00:06:07
noticeable when you jump off a boat but
00:06:09
since it's only for a moment i think a
00:06:11
bit of smoothing should be able to hide
00:06:12
it quite well
00:06:14
anyways i'm super relieved that i
00:06:15
finally got this working but of course
00:06:17
i'm building a multiplayer game so i
00:06:19
need to make sure to test this with more
00:06:20
than one player connected at a time
00:06:24
[Music]
00:06:37
[Music]
00:06:45
you