nanoMMO

Smooth movement for other players in a C++ MMORPG

Our C++ MMORPG draws the other players and the monsters a tenth of a second in the past, between two worlds it already has, so they walk smoothly on a network that loses packets, and carry on for a moment when worlds go missing.

Watch on YouTube

Entity interpolation for our MMORPG, in C++. On a network that loses packets, the other players and the monsters stopped and lurched whenever a world from the server came late or not at all, as the client eased each of them towards the newest place it had. The client now keeps the last second of worlds, works out how far its clock is from the server's from the world that came quickest, and draws everyone else a tenth of a second in the past, between the two worlds on either side of that moment, while your own player stays in the present with last episode's prediction. When the next worlds are lost, the last two carry everyone on the way they were going, for a tenth of a second at most. This is episode 69 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.

Topics: Networking

The code

Each step on screen is a tag in the game's repository. The code at the end of the episode is ep069/end.

  1. 3:08ep069/step-1A moment in the pastBrowseChanges
  2. 14:39ep069/step-2A lost worldBrowseChanges

Transcript

Intro

0:00 Hi, and welcome to episode sixty-nine, where we're going to make the other players and the monsters walk smoothly, on a network that loses packets, with what's called interpolation. The client is going to draw them a tenth of a second in the past, between two worlds it already has. To me, interpolation is the other half of last episode's prediction, which looked after our own player and nobody else. First, a quick look at what we'll have by the end.

0:27 Each pane runs on a network that holds each packet back fifty milliseconds and loses one in ten, with other players walking around arthur. On the left is the game as we left it, where the others move unevenly, and stop for a moment now and then as worlds from the server go missing. On the right, they're drawn a moment in the past, between two worlds, and they walk on at an even pace.

0:55 If you'd like to follow along, the code is open source, and the series' website links to it on GitHub, next to a page for each episode with the code at each of its steps.

Last time

1:05 A quick reminder of where we left off, first. Our inputs go out numbered, one for each of the server's ticks, and each world says which one the server applied last, so the client can show the round trip under the minimap. The server's walking moved into a library that both sides link, and the client walks us as soon as a key goes down, then lets each world from the server correct it. While the server moves us on its own, such as up to a monster, the client draws us the way the server says.

Today

1:35 The game as it stands goes on netem again, where each packet waits fifty milliseconds, and one in ten is lost along the way. Our own player has been drawn by the prediction since last time, so keep an eye on everyone else instead. The client eases each of them towards the place the newest world gave them, so a world that comes late slows them down for a moment. Over UDP, a lost world doesn't come again, so they stop where the last one had them, and then lurch forward when the next one arrives, a tick or two further on. And as a world carries everyone near us, a lost one holds back everyone in it at once, about twice a second on this network.

2:22 So, the server sends a world twenty times a second, evenly, and each one carries the time it was sent. On the way, one can be held up, and over UDP, one can go missing altogether. Drawing everyone where the newest world has them turns each of those into a stop and a lurch. Most other games draw the other players a little in the past instead, and I'd pay that delay any day, as someone a tenth of a second behind is hard to notice, while a stutter jumps out. So the client keeps the last second of worlds, works out how far its clock is from the server's, and draws everyone else as they were a hundred milliseconds ago, between the two worlds on either side of that moment. After that, we'll deal with the case where no world after that moment has come yet. Right, let's start with the interpolation, in a header file of its own.

A moment in the past

3:09 The top CMake file moves the project on to version zero point sixty-nine, for this episode's code. The interpolation gets a new header file, interpolation, with its guard and our namespace as usual. We bring in c standard int, for the ids, deque, as worlds come in at one end and the oldest go from the other, map, for each unit's place by its id, and our protocol, for the world. An Interpolation draws everyone but you a little in the past, between the two worlds from the server on either side of that moment. So they move smoothly whenever the worlds come, as long as each next one has come by then. The delay is how far behind the server's clock we draw them, a tenth of a second. That's two of its ticks, so a world can come up to a tick late and still be there in time. Valve's Source engine draws other players a tenth of a second back by default too. A longer delay copes with later worlds, and shows everyone further in the past, which you'd start to feel when you fight them. Add takes a world as it came, with the time on our clock, in seconds. Over UDP, a world can come in several datagrams, and not always in one frame, so the later parts of a world we already have fill it in, and it keeps the time its first part came. And Shown hands back the newest world, with the players other than you, and the monsters, where they were that delay ago on the server's clock. Inside, a Place is where a unit stood, along x and z. The place is all we draw from the past, as the animator already turns and blends the facing and the motion of the newest world smoothly. A Snapshot is one world we keep, and sent is when the server sent it, on the server's clock, in seconds. Ahead is how far our clock was ahead of the server's when it came, which takes in however long the world took to arrive. Then come the players' and the monsters' places, by their ids. Then gives the time on the server's clock to draw at, now, and the snapshots go in a deque, oldest first. New worlds join it at the back, and past a second's worth, the oldest leaves from the front.

5:15 In interpolation, we bring in algorithm, for std min and for finding the first snapshot after a time, cstddef, for size t, iterator, for std prev, and limits, for an infinity to start a minimum from. What comes before the class's functions sits in an unnamed namespace, so it stays private to this file. We keep twenty snapshots at most, which at twenty worlds a second is a second of them. That's plenty to draw from, and to learn the clocks from. Mix goes from a to b, by the share of the way from one to the other, so a share of a half lands halfway. It's the simplest blend there is, a straight line from one place to the next, and over a twentieth of a second, that's close enough to how anyone walks. Next comes a function template, as players and monsters are two message types that both have an id, an x and a z, and the compiler writes it out once for each. It puts each unit in the newest world but you where it was at then, by the share of the way from one snapshot to the other. It's called Between, and it takes the units, your id, the places in each of the two snapshots, and the share. For each unit, it looks its id up in both. We skip you, as the prediction draws you, and anyone missing from either snapshot, such as a player who's just come into view. They stay where the newest world has them. Add starts from the world's time, which the server stamps in microseconds, so sent is that over a million, in seconds. A world older than the newest snapshot is a part of an older world that came late, and it's no use now, so we leave it. A newer world gets a snapshot of its own, with its time, and ahead, our clock now, less sent, so the clock comes from the frame its first part arrived in. It goes on the back, and past twenty, the oldest goes from the front, so the deque holds a second of worlds. Then, new or not, the newest snapshot takes each player's place, by their id, and each monster's, in maps, so that finding a unit in an older snapshot is a quick lookup by its id. A part that comes a frame later writes its players and monsters into that snapshot too, so the snapshot holds the whole world once its last part is in. Then is where the two clocks come in. Neither side knows how long a world takes to arrive, and the clocks needn't agree either, so each snapshot's ahead is the difference between the clocks plus that trip. The world that took least to come shows best how far apart the clocks are, as the others were held up on the way by that much more. So we take the smallest ahead in the last second, and the moment to draw is now, less that, less the delay. I like that this needs no separate clock syncing between the client and the server, as the time stamps on the worlds are enough. Shown starts from a copy of the newest world, and with fewer than two snapshots, it hands that back as it is, or else it works out then. Find if, from the ranges library, finds the first snapshot sent after then, and the one before it is the last at or before then. If then comes before all of them, or after all of them, we draw the newest world as it is. Otherwise, the share is how far then is past the earlier snapshot, over the gap between the two. Between puts the players there, skipping you, and the monsters, with zero for you, as no monster has that id. So a unit that's in both snapshots walks along the straight line from one to the other, at the pace the two worlds give it. And the client's CMake file builds interpolation into the client's core, and its test into the client's tests.

Drawing them behind

8:47 Until now, the animator smoothed the others out on its own, by easing each figure a share of the way towards where the newest world had it, each frame. That softened the steps a little, and drew everyone else behind by however long the easing took. So in the animator's header file, the follow rate is for a monster's lift now, its height off the ground, which still eases: the share of the gap left after a second is e to the minus the rate. In the animator, the line that eased the position goes, and the figure goes where the world puts it, which the interpolation has made smooth. An easing on top would draw them further behind for nothing. In the client's main, we bring in interpolation. Next to the prediction, the client keeps an Interpolation, for everyone else, a moment in the past, between the worlds on either side. Each frame, the world as far as it's come goes into the interpolation, with raylib's clock, before the prediction corrects us. A world's parts fill in its snapshot, whichever frame each comes in, which is why this runs each frame, and not once for each new world. Then the animator gets the world from Shown, with everyone else where they were a tenth of a second ago, and the prediction patches us in on top, as before, so we stay in the present. Further down, we're still drawn where the prediction has us, as the interpolation draws the others a moment ago, which is what we write over that loop now. When a world comes up to a tick late now, the others are still walking between two earlier ones, and the late one is there by the time they need it.

10:20 Now the tests, starting with the animator's, where the test that it catches up with the server over a few frames becomes one that it draws each figure where the world puts it. One frame after a world moves a player to one along x, the figure is there, as the animator takes each place as it comes. Then the interpolation gets a test file of its own, which brings in its header and gtest. At the top, in an unnamed namespace, a helper called At makes a world sent at a time, in seconds on the server's clock. It turns those seconds into microseconds, the way the server stamps its worlds, and says that your id is two, as the player the prediction draws. Then a loop goes over the ids one and two, and for each of them, it adds a player to the world, with that id, at the x it's given. With two players, each test can check the others and you at once. The first test draws the others between the worlds of a moment ago. The server's clock is a thousand seconds behind ours, and each world takes thirty milliseconds to come. Three worlds arrive, fifty milliseconds apart, at x zero, one and two, each thirty milliseconds after it was sent, and we keep the last as the newest, to draw from. At a thousand and five point one five five on our clock, a tenth of a second ago on the server's is five point zero two five, halfway from the first world to the second. So player one is drawn at a half, and you're where the newest world has you, at two, as you're the prediction's. The second test starts like the first, and then holds the middle world up forty milliseconds more on the way, so it comes at a thousand and five point one two, while the other two take thirty milliseconds as before. At the time from the first test, Shown still draws player one at a half, as the clocks come from the quickest world. An average of the three would have put the server's clock further back, and drawn him nearer zero, which is why we take the smallest. The third test fills in a world as its parts come. The first world comes whole, at x zero, as in the others. The next world's first part has nobody in it, and its second, a frame later, has them, at x one. Halfway between the two worlds, player one is drawn at a half. That needs the second part to have filled in the snapshot, as with nobody in it, he'd stay where the newest world has him, at one.

12:38 We build the client's tests at this step, and run the interpolation's three, along with all of the animator's, which need no databases, so dash dash no deps leaves them out again. All three of the interpolation's pass, the clocks from the quickest world among them, and so do the animator's seven, with the easing gone.

12:57 This step goes on the network from before, with fifty milliseconds on each packet and one in ten lost. The other players and the monsters walk at an even pace now. When a world goes missing, the client still has the one before it, and a tick later, the one after it, so it draws them along the line between those two. They're drawn a tenth of a second further back than the newest world, and arthur, drawn by the prediction, stays in the present.

A lost world

13:31 Now a much worse network, which loses three packets in ten. At this step, when two or more worlds in a row go missing, the moment we draw at passes the newest world the client has. With nothing after it, the client draws everyone where that world has them, so they stop, and once the next world comes, they jump to where they should be by then. The stops are far rarer than before the interpolation, as one lost world on its own no longer causes one, and most last a tenth of a second or less, so watch closely.

14:08 Here's the idea for the next step, before we write its code. Say the client has the worlds sent at five seconds and at five point zero five, and the two after them are lost. When the moment we draw at passes the newest world, we carry everyone on the way they went between the last two, as someone who's walking is most likely still walking a moment later. We do that for a tenth of a second at most. The longer we guess, the further off we'd draw a player who stopped or turned in the worlds we lost, so after that, they wait for the next world, which puts them right.

14:40 In interpolation's header file, the most ahead is a tenth of a second. That's how far past the newest world we carry them on, along the way they were going, when the next one is late or lost, before they wait for it. In interpolation, the search for the two snapshots stays, and we write over it what happens with none after then. The next world is late or lost, and the last two carry them on the way they were going, for a tenth of a second at most. A then before the first snapshot still hands back the newest world as it is. Past the last one, we take the newest snapshot as the after, so the one before it is the from, and the line between them runs on past the newest. The gap is the time between those two. The share can go past one now, which carries them beyond the newest world, and std min holds it at one plus the most ahead over the gap, a tenth of a second past the newest. Mix needs no change, as a share of one and a half goes half a gap past b, along the line from a. I prefer a short guess like this to a freeze, as most of the time the guess is close, and when it's wrong, the next world puts it right a moment later.

15:47 And in the interpolation's test file, a fourth test, that it carries them on a while when one goes missing, which is the case we've just written. It starts like the first, with three worlds arriving fifty milliseconds apart, at x zero, one and two, and the last of them is the newest we have. The world sent at five point one five is lost, though. At a thousand and five point two eight on our clock, then is five point one five, so player one goes on the way he was going, to three. At a thousand and five point four three, then is five point three, though he stops a tenth of a second past the newest world, at four, to wait for the next.

16:25 After the build at this step, the filter picks each test whose name starts with Interpolation, all four of them, with dash dash no deps again, as they need no servers. A lost world carries them on, for a tenth of a second and no further, and the three from before pass as they did.

16:43 The worse network again, which loses three packets in ten, now with the code that carries everyone on. When a few worlds go missing in a row now, the others walk on the way they were going, for up to a tenth of a second past the newest world, and the next world takes over from there. Someone who stopped in the worlds we lost is drawn a little past where they stopped, and then pulled back once the next world comes, which is easy to miss in a crowd.

Wrap-up

17:15 The client now keeps the last second of worlds, and learns how far its clock is from the server's, from the world that came quickest. Everyone but us is drawn a tenth of a second in the past, between the two worlds on either side of then, and the animator takes those places as they come. When the worlds after then are late or lost, the last two carry everyone on for a tenth of a second, and then they wait.

Playing it

18:32 Let me know in the comments what you'd like to see next, and if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.