Our MMORPG's people stand on a skeleton of our own now. The rig is data, in the game's content: twenty-four joints with an adult's proportions, two builds, a stance, and the numbers a walk and a run are made from. As the C++ client starts, it builds each person's bones from the rig, and works out their motions into raylib's clips: the legs bend at the knee with two-bone inverse kinematics to reach where the feet go, so a foot that's down stays where it was put down, and the ground holds the body up as it dies. Mixamo's files and the Docker stage that converted them go, so a fresh clone draws everyone as a person. This is episode 60 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.
Topics: Graphics
The code
Each step on screen is a tag in the game's repository. The code at the end of the episode is ep060/end.
- 2:37
ep060/step-1A rig of our ownBrowseChanges - 14:07
ep060/step-2A rig of our ownBrowseChanges - 19:41
ep060/step-3People on the rigBrowseChanges - 22:30
ep060/step-4People on the rigBrowseChanges - 32:53
ep060/step-5Legs that reachBrowseChanges - 39:58
ep060/step-6Walking and runningBrowseChanges - 47:11
ep060/step-7FallingBrowseChanges - 52:29
ep060/step-8Mixamo goesBrowseChanges
Transcript
Intro
0:00 Hi, and welcome to episode sixty, where our people get a skeleton of their own, in the game's content, and the client works out how they idle, walk, run and die from it. Before we build it, let's see where it's going.
0:13 By the end, each person stands on bones of our own, and walks and runs on legs that the client bends to reach where the feet go, so a foot that's down stays put.
0:27 The series has a website, which links to the game's code on GitHub, where it's open source, and has a page for each episode, with the code at each of its steps.
Last time
0:37 Last time, we gave the game a version. There's one for the client and the servers, which the login screen shows in its corner, the logs print, and the metrics carry. The login server turns away a client of another version, and says which one to get. A script lists the C++ libraries we fetch, pinned by their hashes, and moves them on, and each week, pull requests bring the newest versions. And a file of changes, the newest first, says what each version changed.
Today
1:05 This is the game as it stands, built from a fresh clone. The people's bodies are ours, but they hang on Mixamo's skeleton and move with its clips, which aren't in the repo, so everyone's a cube.
1:20 With Mixamo's files downloaded, and converted by a stage of the Dockerfile, they're people again, on Mixamo's skeleton, as they idle, walk and run with its clips.
1:33 And when a player's health runs out, the client blends into Mixamo's clip of dying backwards, which keeps its own motion, so the body falls back, as the world darkens and a window counts down to coming back.
1:55 Mixamo's terms let us use its files in a game, but not hand them out, so each person who builds the game has to download their own. So today, the skeleton goes into our content, as data, next to the figures: its joints, the builds of body it comes in, and how it stands, idles, walks, runs and dies. The client builds each person's bones from it as it starts, and works out the motions into the clips the animator already plays. The legs bend at the knee to reach where the feet have to be, so a walk and a run come from where the feet go, and a foot that's down doesn't slide. In the death, the ground holds the body up, and with that, Mixamo's files can go. Right, let's start with what a rig is.
A rig of our own
2:37 This is episode sixty's code, so first, the game's version in the top CMake file moves on to zero point sixty point zero, which the login server checks each client against. Next, a new proto file for the rig. Our people's parts hang on bones, and so far those bones came from Mixamo's bot. So we describe a skeleton of our own, in our content, next to the figures. It imports the figures' proto file, for its offsets, and the skills' one, for the keys and moves our skills are made of. A joint is where a bone starts, and it has a name, such as left arm, which the figures' parts and the items' parts already move with. It hangs from a parent, which is listed before it. The hips are the one joint without a parent, as they carry the rest. Where it is goes in metres, along the figure's own axes, as it stands with its arms out to the sides. That's x to its left, y up from the ground, and z forward. A joint on the left can be mirrored onto the right, named right for left, with x the other way across. That way, each arm and each leg is written once. A shift moves a joint, and the joints that hang from it, by an offset. A shift on the left moves its twin on the right too, the other way across. A build is a list of shifts: the rig with some of its joints moved, such as the shoulders out wider. Each player will have one, which their id picks, as it picks their skin and their hair, and an NPC's kind will pick one for it. A gait is a walk or a run, as numbers the client works the legs out from, instead of a clip of keys. There's the speed, in metres a second, which is the server's, and the cycle, a step with each foot, in milliseconds. The stance is the share of the cycle that a foot is down. It's over a half in a walk, so that both feet are down for a moment, and under a half in a run, so that for a moment neither is. Then how high a foot lifts as it swings forward, at its highest, and how far apart the feet are, middle to middle. The crouch is how far the hips sit below where the rig has them, and the bob is how far they rise as each foot passes under them. When the bob is below zero, the hips sink instead, as a runner lands on a bent knee. The sway is how far the hips go over to the side of the foot that's down, so that the body's weight is over it. Like the lift, the width and the crouch, it's in metres. The rest are angles, in degrees, starting with how far the body leans forward. A runner leans much further into each stride than a walker, so the run gets more of it. Then how far the hips turn with whichever leg is forward, and how far the chest turns back the other way, the way a person's shoulders go against their legs. There's no field for the head, as the client will turn it back from the chest to look ahead. Then how far each arm swings either way, and how far the elbows bend, more as an arm comes forward. The sword hand bends back as its elbow does, to keep the blade pointing ahead. Next, how far a foot tips onto its toes as it leaves the ground, and back onto its heel as it lands, and how far behind the body the arms are held as they swing. And from those numbers, the client makes each step of a walk or a run. The client works out where each foot is as the cycle goes round, and bends the legs to reach it. A foot that's down goes back under the body as fast as the server moves the body forward, so it stays where it was put down. Then it lifts and swings forward, to land for its next step. The rig has the joints, parent first, and at least one build, as the rig as it is counts as a build without shifts. The stance is a key, like one of a skill's moves. It turns the joints from the rig's pose, with the arms out, to how a figure stands, such as with the arms down by its sides. The idle and the death are moves over the stance, as a skill's move is over the clip under it. The idle loops, so its last key is its first again, and the legs are to bend to keep its feet where they are. The walk and the run are gaits, and the death holds its last key, where the figure lies. The rig is what the client builds each player's body on, from the figures' parts, and what it works out each motion's poses from.
6:52 The rig gets a header file of its own in the content, as each of the other files has, for its reader. Parse rig reads the rig from text, as the other content files are read. It returns the rig, or an error that says what's wrong with it. That's a field the proto file doesn't have, a joint without a name or with another's, or a joint whose parent isn't listed before it. A second joint without a parent is refused too, as are a build that moves a joint the rig doesn't have, and a walk or a run without a speed or a cycle. So are a walk or a run whose feet are down for none of it or all of it, and an idle or a death whose keys go back in time. Skeleton lists the joints of one of the rig's builds. Each mirrored joint is followed by its twin on the right, so each still comes after its parent, and the joints are where the build moves them. In rig, we use set, the standard library's ordered set, for the names we've read, and the text format's parsing, which we wrote for the other files. To the right gives a joint's twin's name. A joint on the left has Left in its name where its twin has Right, so it swaps the start of the name. Check gait refuses a walk or a run without a speed or a cycle, as the client works each step out from both. It refuses one whose feet are down for none of the cycle, or all of it, too. Either way, the feet would have no time on the ground, or none in the air, to make a step with. Check move goes through a move's keys, and refuses keys that go back in time, as the skills' checks do for their moves. A key may come at the very millisecond of the one before it, but not before that. Parse rig parses the text into a rig, and returns the error when the text doesn't fit the proto file. It keeps a set of the joints it has read so far, by name, with the twins on the right among them, and goes through the joints in the order they're listed. Each joint needs a name, or the rig is refused, and a parent among the joints before it, apart from the first one, which in our rig is the hips, and has none. A second joint without a parent is refused that way too, as the set isn't empty by then. With each joint after its parent, the client can place them all in one pass, parents first. A mirrored joint brings its twin's name along, as the twin is a joint too, and inserting a name that's in the set already fails, so two joints can't share a name. Each build's shifts can move nothing but joints the rig has. By now the set holds all the joints' names, the twins' too, so each shift's joint is looked up in it. Then the walk is checked, and then, as long as it passed, the run. Check move takes the idle and then the death, so their keys can't go back in time. The first error found is the one returned, and without one, parse rig returns the rig. Skeleton starts from an empty list of joints, and writes out each of the rig's joints in turn, and after a mirrored one, its twin. The twin is named to the right, hangs from its parent's twin, or from that parent when it's in the middle, and has its x the other way across. As each twin comes straight after its own on the left, it still comes after its parent. A build the rig doesn't have gives the rig as it is. Otherwise, each of the build's shifts moves its joint, and that joint's twin on the right, the other way across. The joints that hang from a joint come after it in the list, so a single pass that keeps the names it has moved finds them all. It does the left side first, then the right, and skips the right for a joint in the middle, such as the spine, as to the right gives back its own name. Each pass starts from the joint the shift names, or from its twin on the right. A joint moves when it's the one the shift names, or its parent has moved, and on the right, x goes the other way. The y and the z go the way the shift says, on both sides. Then skeleton returns the list, with each joint where the build has moved it. The rig's proto file and its source go into the content's library, in the CMake file, and the source generated from the proto file is built with its warnings turned off, as the other generated sources are. In the content's header file, the content gets a rig, next to the quests, and we'll fill it from the folder once there's a rig to read.
11:10 Now, a quick test that parse rig refuses a rig that doesn't add up, and takes one that does. The legs are the hips, a metre up, and a mirrored left leg, ten centimetres out. Next to them go a walk and a run, each with a speed, a cycle and a stance, which any rig needs to pass, written once so each rig in the test can use them. Those three together make a good rig, so each rig refused after it fails for the change it makes, and nothing else. A lambda parses a rig and gives the error's message, so each check can compare it with the message it expects. The first rig lists a left leg before the hips it hangs from, which is refused, as the hips aren't in the set yet when the leg is read. So is a tail that hangs from nothing, listed after the legs, as it would be a second joint without a parent. A right leg written out by hand, next to the mirrored left one, makes two right legs with one name between them, which is refused too. A build can't move an arm, as a rig of legs doesn't have one, though it can move the right leg, a centimetre out, as the mirroring made one. That build parses, so the arm's build was refused for its arm, and not for being a build. A walk whose feet are down for the whole cycle, with a stance of one, is refused. So is a run with a cycle and a stance but without a speed, as the client couldn't work out its steps. An idle whose keys go back in time, at five hundred milliseconds and then a hundred, is refused too. Next comes a test of skeleton, with a small rig of its own. That rig has the hips, a metre up, a left arm twenty centimetres to their left, which is mirrored, and a hand that hangs from the arm, half a metre further out. Then it has two builds, to compare the rig as it is with a broader one. The first build is empty, so it's the rig as it is, and the second moves the arm three centimetres out. Its walk and its run match the first test's, so the rig parses, as any rig needs both. The first build has five joints, as the arm and the hand each have a twin. The test reads the joints by their places in the list, as that order is what the client builds on. The right arm comes straight after the left one, and hangs from the hips, as the left does. The right hand hangs from the right arm, not the left, and its x is the left one's, the other way across. Then it builds the second, the broad build. Its shoulders are wider, and so are its hands, as they hang from the arms, and the hips stay where they are. Each arm goes three centimetres further out, to twenty-three from the middle, and each hand goes to seventy-three, with the right side's the other way across.
13:49 Let's build the content's tests at this step, in Docker, and run the ones for the rig. The image compiles the whole game first, so this takes a little while. A rig that doesn't add up is refused for its own reason, and each build's joints are where its shifts put them, so both tests pass.
14:08 Now the rig, in a text file of its own, like the rest of our content. The skeleton is about one point eight metres tall, and stands with its arms out to the sides. There are the hips, three joints up the spine, then the neck and the head. On each side, there are the collarbone, the shoulder, the elbow and the wrist, and the hip, the knee, the ankle, the ball of the foot and the tip of the toes. The lengths are an adult's, as shares of their height: the leg from the hip to the ankle is nearly a half, and the arm to the wrist under a third. The hips come first, a metre up, and carry the rest of the body. The spine goes up in three joints, a little further back each time, as a back curves, up to the neck, at one point four eight metres, and the head, ten centimetres above it. The collarbone starts at the top of the spine, five centimetres out. The shoulder is sixteen centimetres out from the middle, the elbow twenty-nine centimetres further out along the arm, and the wrist twenty-seven more, each mirrored onto the right. The hip joint is nine centimetres out from the middle, and just under the hips. The knee is fifty-two centimetres up, a centimetre ahead of the hip, and the ankle is eight centimetres up, three behind the hip. The ball of the foot is on the ground, eleven centimetres ahead of the ankle, and the tip of the toes is nine centimetres further. With the twins on the right, that makes twenty-four joints in all. Of the two builds, the first is the rig as it is, and the second moves the shoulders three centimetres wider on each side, with its hips a centimetre wider too. Then comes the stance, the key that takes the figure from its arms out to how it stands. In the stance, the arms come down by the sides, turned seventy-eight degrees about the way the figure faces, so they're a little out from the body. The forearms turn in twenty-two degrees, so the elbows bend a little, and the sword hand turns, so that the blade points ahead. The idle breathes in and out, and shifts the weight. It starts with the body two centimetres lower than the stance. At one point six seconds, it's three lower, with the chest two degrees back, and the hips rolled to put the weight over one foot. By three point two seconds it's back where it started, so the loop goes round again. The walk goes at one point eight metres a second, which is the server's walking speed. A step with each foot takes just under a second, and each foot is down for sixty percent of it. A foot lifts ten centimetres, and the feet are twenty apart. The hips sit seven lower, and bob and sway by two and a half. The body leans five degrees forward, the hips turn seven and the chest six, and the arms swing twenty-four. A foot rolls forty degrees onto its toes as it leaves the ground, and lands eighteen up on its heel. The run goes at three point one metres a second, the server's running speed. Each foot is down for under two fifths of a cycle that's three quarters of a second, so for a moment neither is. The feet lift thirty centimetres, and the hips sink five as each foot passes under them, on a bent knee. The body leans eighteen degrees, and the hips and the chest turn ten and nine. The elbows bend seventy-five degrees, and the arms are held fourteen back, as a runner holds them.
17:16 In content, we include set, for the names of the rig's joints. Reading the folder reads the rig too, from its file, with parse rig. Check content checks what no single file can, so it's where the figures meet the rig: each part has to move with a joint of the rig, and start and end at one. It collects the names of the rig's joints, with the twins on the right written out. Then a lambda goes through a list of parts, with the mirrored ones unfolded onto the right. It checks the joint each part moves with, the joint it starts at, and the one it ends at. An empty name is skipped, as a part that starts at its own bone's joint leaves its start blank, and a ball has no end. A part on a joint the rig doesn't have is refused, with whose part it is, and the name of the joint. It checks the body, then each way to wear the hair, for as long as nothing has been refused, so the first part refused is the error it returns. The items come from the items file, by id. Then it checks the parts of each item, such as the shirt and the shoes, with the item named in the error. And the client hash, the fingerprint of what the clients read, takes in the rig, as the client is about to build the people from it. So a client can tell whether a world has its content. In the content's header file, check content now refuses a part of a figure or an item on a joint the rig doesn't have. The figures' parts were named for Mixamo's bones without its prefix, which are the names of our joints. So in the figures' proto file, the bone a part moves with is a joint of the rig now, by its name. In the figures file, the parts are placed on our rig as it stands with its arms out to the sides, instead of on Mixamo's. In the content's tests, the test of the client hash reads the game's content again, into a copy of its own. It changes the walk's cycle to nine hundred milliseconds, which changes the client hash, as the clients are going to work out the people's motions from the rig. Further down, the test that refuses rigs that don't add up reads the game's content again, into a copy it can break, one part at a time. The game's own content passes check content, as each of its parts is on the rig. Shoes that end on left toes, a joint the rig doesn't have, as it calls the tip of the toes left toe end, are refused, and so is a part of the body that moves with a tail.
People on the rig
19:42 Now the client, which loads each character from a file. Soon, a person will be built from the rig instead, while the monsters still load their files. So what both need moves out of the constructor, in the characters' header file, into three functions of its own. Ready finds the character's hand, its feet and its joints, works out where its feet come down, and draws it with the shader. Dress puts the figures' body on the character, with its hull, and each way to wear the hair. Unload frees what the character's model, clips and garments hold. They're static, as each works on the character it's given, and none of them touches the class's own lists. In characters, once a model's clips are found, the constructor calls ready. Then it works out the scale, from the model's own mesh, so a figure is as tall as the model, and calls dress. Ready does what the constructor did, line for line. It sets the shader it's given on each of the model's materials, which is how each part of the model gets drawn with it. Then it starts with neither foot found, at an index of minus one, and goes through the bones by name, looking for the right hand, which holds the weapon, and for the left foot and the right foot. Each one it finds keeps its bone's index. For the walk and the run, it works out where in the clip each foot comes down, which is when the footsteps' sounds play. And it finds the joints the moves turn. Dress starts the hull as the model, and stops there when there's no body to put on, as for the monsters, which keep the meshes their files have. Otherwise, it unfolds the body's parts onto the right, and swaps the model's mesh for the parts, with the dress function the constructor called before. Then the hull is a copy of the dressed model, with a single mesh of its own. The hull is those parts with smooth normals, which the outline draws, and it shares the model's bones, so the clips pose it too. Then it skips each way to wear the hair that has no parts, and unfolds the parts of the others onto the right. Each way to wear the hair is a copy of the model, with the hair for its meshes, so it moves with the model's bones. A hair gets a model and a hull of its own, each a single mesh, made from the hair's parts, in memory raylib allocates. The hairs are kept by their places in the figures' list, and once they're done, the character counts as dressed. The destructor now calls unload for each character, before it frees the weapons. Unload goes through what the character wears first. For each garment, it unloads both meshes, the model's and the hull's, and frees the arrays raylib keeps them in, each one mesh long. Then it frees each hair's meshes that way too, and the hull's, when the character was dressed, as an undressed hull is the model's own. Last, it frees the clips and the model. The game draws everyone as it did before the change.
22:30 The people are about to stand on a rig of our own, so in the moves' header file, these thirteen names lose Mixamo's prefix. Ours were named after Mixamo's, so each name the skills' moves need is still there, from the hips and the spine to the sword hand and the knees, and the moves need no change. The list keeps the order the skills number them in, so the number in a key still finds its name here. A move's turns go into a new function, turn, which turns the bones of any pose it's handed, such as a frame of a clip being baked. The client is about to work out whole clips with it, a frame at a time, before anything's drawn. Apply still turns a model's bones, as a clip posed them, and works out the matrices the GPU skins it with. In both, a joint takes the bones below it round with it, as an elbow takes the hand. In moves, turn takes a skeleton, and a pose of its bones. It goes in order, each parent before the ones that hang from it, so each one's place comes from its parent's, turned by each joint above it, and its turn and theirs go on top of the rotation it had. Apply turns the model's own pose with turn, then works out the matrices, from where each bone was bound to where it is, as before. Turn leaves the matrices alone, as a frame being baked is kept as a pose, not drawn. Then a new header file in the client, for the rig. It includes raylib, for its models and clips, the standard library's vector, and the code generated from the rig's proto file. Rig model makes a model of the joints, without a mesh yet. It has a bone for each joint, parent first, resting where the joint is, in metres, unturned, and dress gives it its body. Bake works out the rig's motions for a model of it, as raylib's clips. They're the idle, the walk, the run and the death, in the protocol's order of motions, sixty frames a second, so the animator plays and blends them as it did Mixamo's. For now, each of them is the idle. In rig, we include raylib's maths, the standard library's algorithm, maths, string copying, utility and vector, and the moves' header file, for turn. The clips get sixty frames a second, and a pose is a transform for each bone, which is how raylib keeps a clip's frames. A poser holds what each motion's frames start from. It copies the model's rest pose, the joints as the rig has them, and finds the joints the moves turn, by their bones' names. The stance is a single key, so it goes into a move of that one key. Bent at zero milliseconds, that move turns the rest pose into standing, with the arms down. A metre is one unit here, as the rig is in metres. Bent turns the standing pose by a bend, as a skill's move turns the clip under it. It turns a copy, so the standing pose stays as it was for the next frame. It holds the model's skeleton too, as turn needs each bone's parent, along with the poses and the joints. Clip makes a raylib clip out of a list of poses, with its name, and the number of bones and of frames in it. It allocates an array with a pointer for each frame, then each frame's transforms, one for each bone, and copies the frames in. All of it is in memory raylib allocates, so raylib's own unload frees it, as it would a loaded clip. Frames says how many frames a stretch of milliseconds takes, and at least one. The idle lasts until its last key, or a second when it has none. That's split into frames, sixty a second, and the loop makes a pose for each frame, and one more at the end. Each frame bends the standing pose by the idle's move at its moment. The last frame's moment wraps round to zero, so the loop ends where it started. Rig model fills in a raylib model by hand, field by field, as raylib's loaders would. It starts with an identity transform and a single material, raylib's default, in an array that raylib allocates, and a bone for each joint. There's room for each bone's name and parent, its rest pose, its current pose and its matrix, each allocated with raylib's allocator, so unloading the model frees them. Then it goes through the joints in order, and makes each one's bone. Each bone is named as its joint, and the copy stops short of the end of the name's array. Its parent starts as none, at minus one, and is found among the joints before it, by name, and it rests at the joint, unturned, which keeps the sums simple when we pose it. The current pose starts as the rest pose, and each matrix as the identity, as nothing's posed yet. Bake makes a poser for the model it's given, which holds a copy of its rest pose and its standing pose. Until the legs can walk and the body can fall, each of the four motions is the idle. The four clips are made from the idle's frames, named idle, walk, run and death, and the count is set to four. The clips go into an array raylib allocates, as its loader would, so unload model animations frees them like any others.
27:35 In the characters' header file, the folder that Mixamo's characters came from goes, and a new constructor builds people from the rig, one for each of its builds, in the figures' body, each player in their own skin and hair. The other constructor loads the files in a folder, with their clips, as tall as a height, which is what the monsters do, so it doesn't take figures any more. In characters, we include the client's rig header file and the content's, and the bones are named as the rig's joints, without Mixamo's prefix: the right hand, and each foot. The sword is held at the middle of the fist, six centimetres along the fingers from the wrist, and one towards the palm. Then the function that gave the characters' folder goes, and the new constructor takes the rig, the shader and the figures. It goes through the builds, or the rig as it is when it has none, and lists each build's joints with skeleton. A rig without any joints stops the constructor there. Then it makes a model of each build's skeleton. It dresses each in the body first, then bakes its clips, four of them, for the idle, the walk, the run and the death. Without the four motions, it logs a warning, frees the character it was building, and stops there, so the players are drawn as they are without characters. A swing is a skill, with a move of its own, so the attack's motion stands as the idle does, and then the person is readied, as a loaded character is, and kept in the list of characters. In the constructor that loads files, nothing is dressed now, so a loaded character's hull is its model, as for the monsters. The way the sword sits in the hand changes too, for the rig's hand. The rig's right hand rests with its fingers out to the right, its palm down and its thumb ahead. So the sword turns ninety degrees about x, to point out past the thumb, and twenty about y, to lean towards the fingers. Then it moves to the middle of the fist. In figures, a bone is found by its joint's name, as it is, now the bones carry no prefix. The renderer builds the players' characters from the content's rig, and rig goes into the client's library, in the CMake file, with its test in the client's tests.
29:45 Now, a couple of tests for the client's rig, in a new file. It includes the client's rig header file, raylib's maths, and the content's. They read the game's own content, from the content folder the build points them at, and find a model's bones by their names, going through its bones until one has the name, or giving minus one. A baked struct holds a model of the rig, made from the first build's skeleton, and its clips, which bake works out for it. It frees both as it goes, with unload model animations and unload model, so no test leaks them, whatever it checks. It can't be copied, as two copies would free them twice. The model comes first among its fields, so it's made before bake reads it. The first test makes the model of the rig as it is, from the game's content, and finds three of its bones by name: the hips, the right arm and the right forearm. The hips are bone zero, without a parent, and the right forearm hangs from the right arm. The elbow rests over thirty centimetres to the right, so x is below zero, and over a metre thirty up, so the bones are in metres. The second test bakes the clips, from the game's own rig. There are four of them, with the idle first and the death last. Each of the three loops, the idle, the walk and the run, ends on the pose it starts with. Bone by bone, it compares where each bone is in the first frame and in the last, so playing it round doesn't jump. In the characters' tests, we include the standard library's sizes and the content's header file, and the tests read the game's own content, as the rig's tests do. The first test now builds a person for each of the rig's builds. It was skipped without Mixamo's files, as in CI, like two of the tests below it, and now they all run wherever the tests do. It reads the game's content once, and hands its rig and its figures to the constructor. With the game's two builds, that's two people. The next test, of a player standing in the middle, isn't skipped either. The player in the middle now comes from the game's content, and wears the linen shirt, which is dyed in each player's colour. So at the chest, a little lower than before, the picture is orange, player one's colour. The test of what a player wears over their figure doesn't look for Mixamo's files any more either. Content without a rig builds no people now, so the test gives it the game's rig. And in the figures' test, the includes change from the characters' header file to the rig's. The test doesn't need Mixamo's bot any more, so it isn't skipped. In place of the bot, it dresses a model of the game's rig in an arm, a head, and a tail the rig doesn't have. It finds the arm and the head by their joints' names, without the prefix.
Standing on it
32:30 Let's see a person drawn by the game's renderer at this step. The client builds each one from the rig as it starts, so this is our skeleton in the figures' body, standing as the stance says. The walk and the run are the idle for now, so the feet glide along the ground as the body moves on.
Legs that reach
32:53 Now the legs, each of them two bones of fixed lengths, the thigh and the shin, so where the ankle has to be says where the knee goes. Working back from where the end of a limb goes, to how its joints bend, is called inverse kinematics. Animators use it to plant feet and hands, and most other games use it for feet on uneven ground. Two bones is the case that has an answer in a few lines. In rig, we include string and string view, for the legs' bones by name. The heel is four centimetres behind the ankle. That's where a foot lands, and rocks forward from. Each foot, and so each knee, points six degrees out from straight ahead, as people's feet do. Past ninety-seven percent of a leg's length, it straightens more slowly than its foot goes. Bone named finds a bone in a skeleton by its name, or gives minus one, as the test's helper does for a model. Aim is the turn that takes a bone from the way it rests to the way it goes now. A direction alone would leave the bone free to spin about its length, so each comes with a front, square to it. Each pair makes a frame of three axes, with y along the bone and z to its front. The front loses whatever part of it runs along the bone, so it's square to it, and x is square to both. The three axes are the columns of a rotation matrix, one axis to a column, which raylib turns into a quaternion. The turn is the new frame times the inverse of the old one, normalised, so it stays a pure turn. A leg is its bones, from the hip, through the knee, the ankle and the ball of the foot, to the tip of the toes, each minus one until it's found. It has its side too, one for the left and minus one for the right. A step is where a foot is: its ankle, and how far it tips, toes down, in radians. Place makes the steps, and reach turns each step into a bent leg. The poser finds each leg's bones by their names, on the left and then on the right. Each name starts with the side, so the hip is up leg after it, the knee is leg, and the ankle is foot, with the toe base and the toe end after them. Has legs says whether each leg has its hip, its knee and its ankle, which a leg needs to reach. The ball and the tip of the toes are optional, as a foot can be drawn without them. Under is where the foot is when it stands, on the ground beneath its ankle in the rig. Place works out where the ankle is for a foot whose point under its ankle is on the ground here, tipped by a pitch it's given. A pitch above zero puts it up on its toes, and one below zero back on its heel. It starts from where the ankle rests, and the point under it. The foot pivots about whichever end stays on the ground. That's the tip of its toes as it tips forward, or its heel as it tips back. That end goes where it touches the ground, turned out with the foot, and the ankle is the rest of the foot from there, turned as it tips. The step it gives back has that ankle, and the pitch it was given. Reach bends a leg to put its ankle where the step says, with the knee over the foot. It takes where the ankle rests, the foot's turn out, that turn with the pitch, and the ankle from the step. Then it takes where the hip is in the pose so far, and where the hip and the knee rest. The thigh and the shin keep the lengths they rest with, measured from the joints in the rig, so a broad build's legs bend like any other's. The front is straight ahead, turned out with the foot. The knee bends forward, in the plane of the hip, the ankle and the way the foot points, as far as the two bones' lengths say. The gap is from the hip to where the ankle has to be. The leg's whole length is the thigh and the shin together. Past most of that length, the foot will fall a little short, so the knee straightens smoothly instead of snapping straight. The soft point, where that starts, is ninety-seven percent of the length. The reach is how long the gap is, but at least the difference of the two bones, which is as close as they can fold. Past the soft point, the reach shrinks on an exponential curve, more so the further it is past it, and stays short of the leg's whole length. So the ankle is placed a little short of where it's asked. The law of cosines gives the angle at the hip, from the three sides of the triangle that the thigh, the shin and the reach make. So the knee is the thigh's length along that angle, out towards the front, and the ankle is where the reach ends, straight out from the hip towards where it has to be. The hip turns from the way the thigh rests to the way it goes now. The knee moves to its place, and turns from the way the shin rests to the way it goes now, from the knee to the placed ankle. The ankle goes where it's placed, turned as the foot tips. The foot is one piece, toes and all. So when the leg has a ball, it goes where it rests from the ankle, turned as the foot is, and takes the foot's turn. When it has the tip of the toes as well, the tip goes where it rests from the ball, turned with the foot again, and takes the foot's turn too. Each also gets a scale of one, as a scale left out would be zero. Then, in the poser's private section, bone looks a bone up by name in the poser's skeleton, with bone named. Yaw is a foot's turn out from straight ahead, about the up axis, to its own side, and foot adds its tip, about the axis across it, after that turn. Place and reach both use these two, so a foot's turn is worked out in one place. The idle keeps each foot flat where it stands. Each frame, after the move bends the body, each leg reaches for the point under its ankle, so as the hips sink and roll, the knees give, and the feet don't go into the ground. Bake checks the poser has both legs before it makes any clips, and all four are still the idle. A rig without the legs to stand on gets no motions at all, an empty list of clips, so its characters aren't built, and the warning says why. In the rig's header file, bake gives each motion the idle for now, with the feet where they stand. The walk and the run come next, and after them, the death. So for now, a person who walks glides along, breathing as they stand. Then a test that the legs bend without stretching. A leg that stretches would draw its knee in the wrong place, or its shin longer than it is. It bakes the game's rig, and finds the right leg's hip, knee and ankle by name. From the model's rest pose, it takes the thigh's length, from the hip to the knee, and the shin's, from the knee to the ankle. Then it goes through all the clips the rig bakes, the idle, the walk, the run and the death, and each frame of each, taking the frame's pose. In each frame, the thigh keeps its length to a tenth of a millimetre, measured between its joints as they're posed, and so does the shin. Last, it adds the shin as it rests, turned by the knee's turn, to where the knee is. So the shin goes where the knee's turn takes it, as the GPU draws it, and its end has to be the ankle, to a millimetre. That catches a knee that's in the right place, but turned the wrong way. That's the mistake that's easy to make here, as each bone's place and its turn are worked out apart.
Walking and running
39:59 Now the walk and the run, worked out from where the feet go. In rig, we include numbers, the standard library's header for pi. The head takes back sixty percent of the body's lean, to keep looking ahead, and the sword hand bends back eight tenths as far as its elbow does, to keep the blade pointing ahead. For the first fifteen percent of a foot's time on the ground, it rolls down off its heel, and from sixty percent, it rolls up onto its toes. Smooth eases from zero to one, slowly at both ends. Swing gives the turns of a gait's upper body, a share of the way round its cycle, from when the left foot lands. It follows a wave, the cosine of the phase, which is one as the cycle starts and minus one halfway round. The turns go into a key, through a lambda that adds a joint's turn, with its pitch and its yaw. The hips turn with the leg that's forward, the left one at first, by as much as the gait says either way, and the chest turns the other way, with the arm that's forward. The lean is split between the hips and the spine, half each. The spine takes back the hips' turn, and it and the chest each turn half of the chest's. The head takes back most of the lean, and the whole of the chest's turn, so it looks ahead all the while. Each arm swings against its leg, and bends more as it comes forward. Each elbow bends at seven tenths of the gait's bend when its arm is back, and more, up to one point three times, as the arm comes forward. The arms are held back by the gait's arms back, and swing either way by its arm swing, the left one forward as the right leg is. And the sword hand bends back with its elbow, by that share. These turns go into a key, and a move of that key, bent at zero, makes them a bend, as for the stance. The hips' shift follows the middle of the left foot's time down, so it's in step with the feet. The hips sway over to the foot that's down. They're highest, or lowest for a run, as each foot passes under them, so they bob twice a cycle. Footing says where a foot is, a share of the way round its own cycle from when it lands. It takes the gait, the cycle's length in seconds, and the centre. While it's down, the body goes forward by the speed, times the time the foot is down. So the foot goes back that far under the body, which, seen from the ground, is standing still. On the ground, a share of the way through its time down, it starts up on its heel and rolls down flat, then rolls onto its toes. It goes from half that distance ahead of the centre to half of it behind, where it leaves the ground. Off the ground, the foot goes from where it left the ground to where it lands next. Its share of the way there is t, from zero to one, and the two ends are where the foot is on the ground at the end of its time down, and at the start of the next. At each end, it goes half as fast as the ground, backwards, which eases the foot off the ground and onto it. Between them, a Hermite curve carries it forward. A Hermite curve takes a start, an end and a speed at each, and passes through them smoothly. The foot rises on the way, along a sine that's bent to be highest a little before halfway. That bend is a power of t, chosen so that four tenths of the way goes to a half, where the sine peaks. Across the way, its x and its height go smoothly from one end to the other, and so does its tip. So while a foot is down, footing has it go back as fast as the body goes forward, over a stretch whose middle is the centre's distance ahead of where it stands. Centre says where the middle of a foot's time down is, ahead of where it stands. Too far ahead, and the leg reaches far forward to land but hardly back to leave the ground, which looks like leaning back. It finds where the left leg's hip is when the foot lands, and where it is when the foot leaves, both as swing puts it, and searches between half a metre behind and half a metre ahead. Thirty halvings of that search find the centre where the ankle is as far from the hip on landing as on leaving. Each try puts the left foot where it lands, ahead of the hip, and measures how far its ankle is from it. Then it puts the foot where it leaves the ground, behind, just before the end of its time down, and measures again. The centre moves towards the shorter reach, and the answer is the middle of the range that's left. Loop makes the frames of a gait, a cycle long, plus one more, which is the first again. A cycle is a whole number of frames, so the cycle the feet keep up with is the one the clip has. Then it works out the centre once, for the whole gait, and each frame's feet use it. Each frame's phase is its share of the cycle, and the frame bends the upper body by swing, puts each foot where footing says, the right half a cycle behind the left, and reaches for it. Each frame's pose goes into the list it gives back. In bake, the death is still the idle, for now, as its keys come in the next step. It's the walk and the run that change: each is a loop, baked from its own gait, at its own speed. In the header file, bake's description catches up, and the feet no longer stand still in the walk and the run. A loop's last frame is its first again, which now holds for the walk and the run, as it did for the idle. Then a test that a foot that's down stays where it was put down, which is the reason for working the gaits out this way at all. It's what a clip made by hand gets wrong, at a speed it wasn't made for. It bakes the game's rig, and finds the tip of the left foot's toes, and its ankle, by name. Then it goes through the walk and the run, each with its speed from the rig, and counts the frames the toes are down. Frame by frame, from the second on, it takes the pose before and the pose now, and finds the tip of the left foot's toes on the ground in two frames in a row, under a millimetre up. It works out how fast the tip went back from how far it moved in a sixtieth of a second, the time between two frames. That's the body's speed, to two centimetres a second, and the frame counts as down. The tip goes back as fast as the body goes forward, which on the ground is standing still. The ankle doesn't go under the ground, and in the walk and in the run, more than a fifth of the frames have the toes down, a good part of the cycle. In the characters' test of the people built from the rig, we include maths and optional, as there may be no gait to give. It asks the people for player one's gait as they walk. Each foot comes down once a cycle, half a cycle apart from the other, give or take five hundredths of a cycle, and that's when the footsteps' sounds play.
46:32 Let's build the client's tests at this step, in Docker, and run the rig's and the characters', which draw into a hidden window. The bones rest where the joints are, the loops end where they start, the legs bend without stretching, and a foot that's down stays put, so they all pass.
46:50 Now the stroll again, at this step, where the legs have their gaits. The client baked these frames from sixteen numbers as it started. As each foot goes down, it stays where it landed while the body goes on, and the legs bend at the knee to reach it.
Falling
47:11 Last, the death, which is keys over the stance, like a skill's move. There are a lot of them, so we paste them into the rig file. Struck, the figure jerks back in the first fifth of a second, doubles over by point six, and its knees give way. By a second, it sits down hard, sixty centimetres lower and thirty back. By one point four, it has rolled onto its back. By one point eight, it lies there, its arms out, its head to one side and its blade along the ground, and it settles by two point two seconds. The keys go lower than the body can, as the ground holds it up. In rig, a new function, sunk, takes a model, with its body, and a pose of its bones. It says how far below the ground the lowest corner of the body is, in that pose, and below zero when it's above it. It starts out without having found a lowest corner. It goes through each mesh, and skips one whose corners or bones aren't in memory. In the rest, it goes through each corner, and takes the bone the corner is bound to, and the corner's place, as the model rests. It turns by the bone's turn, about where the bone rests, and moves to where the bone is now. Each bone rests unturned, so a corner moves as its bone does. The lowest of those heights so far is kept. The lowest corner's height, turned round, is how far the body has sunk. Fall makes the death's frames, from the death's move, which lasts until its last key, or no time at all without keys. It counts the frames that takes, at sixty a second, one more than the last key's, which the clip stops on. Each is at its moment of the death's move, up to the last key, and bent by it. Wherever the keys put the body under the ground, sunk says how far, and when that's above zero, each bone in the pose comes up by that much, so it stops where it meets the ground, as a body does. That's why the body went on before the clips were baked. The death's clip is the fall now, in place of the idle. In characters, we note that reason where each person is dressed before the baking: the death keeps the body out of the ground. In the header file, the death's last two frames are where the figure lies, and a model with its body keeps the body out of the ground as it falls. Then a test that a person dies lying on the ground, not under it. It needs a window, as dressing a model uploads its meshes to the GPU, and it checks the window opened. It reads the game's content, and makes the model of the rig's first build. The rig's model is dressed in the game's body, a metre to a unit, and its clips are baked, four of them, as the test makes sure. The death is the fourth clip, and the body is the model's one mesh. In each frame of the death, it starts with the lowest a metre up. It goes through each corner of the body's mesh, takes the bone it's bound to, and finds where the corner is, posed as the GPU poses it. It works the corner out the way sunk does: where it rests, less where its bone rests, turned by the bone's turn in the frame, plus where the bone is in the frame. Each corner's height goes into a minimum with the lowest so far, so once it has been through all the corners, it holds the height of the one furthest down. That corner is above the ground, to a millimetre, so the ground has held the body up in each frame of the fall. The death's last frame is where the body lies. And at the end, the hips are down by the ground, under a quarter of a metre up. Then a test of the death as the client draws it, in the characters' tests, as the clip can be right and the picture still wrong. We include the standard library's algorithm header, for a minimum. This test draws a dead player as the client does, as they're struck and long after the fall. Like the rig's test, it opens a hidden window first, as the renderer draws with the GPU. Its camera is four metres to the side and under a metre up, aimed at sixty centimetres up, with a field of view of forty-five degrees. From the side and low down, a body lying on the ground can't be taken for one standing, as it can from high above. It works out the rows of the picture where a head would be, at one point six metres, and a knee, at half a metre. A lambda gives the highest row of the picture's middle column that the figure covers, for a player who died so long ago. It draws the player in the linen shirt through the game's renderer, into a picture of its own, sixty-four pixels square, on the blue background, through the side camera. The player went from the idle to the death that long ago, and blends into it over a quarter of a second, as the animator blends a player into their death. It goes down the picture's middle column, from the top, and gives the first row that isn't the blue background, which is the top of the figure. As it's struck, the figure reaches as high as a head, and once the fall's over, at three seconds, and long after, at ten, none of it is above the knees.
52:08 On one point of health, near the skeletons, a blow takes the last of it. The server sends the motion as dead, and the client blends into the death clip the rig baked.
Mixamo goes
52:30 Nothing reads Mixamo's characters any more, so what made them can go. In the Dockerfile, the characters stage goes, which ran a converter from FBX. The converter comes built for x86 Linux, so on an Arm Mac it was emulated, and the stage copied the characters out. The lines that kept Mixamo's files, the characters made from them and the tools' packages out of git go too, and the packages' line out of Docker's context. The client's build stops copying the characters next to it. The script that converted each bot goes, with the one that put each bot and its clips into a single file, their packages, and the notes on what to download from Mixamo. A fresh clone builds the game, and draws everyone as a person, without downloading anything for them.
In the game
53:14 So this is a fresh clone of the game at the end of the episode, with nothing downloaded for the people. The people stand on our own rig, idle, walk and run on legs that reach for their feet, and their feet stay where they land.
Wrap-up
53:32 So, today, the skeleton went into our content: twenty-four joints with an adult's proportions, in two builds. It has a stance, an idle and a death as keys, and a walk and a run as numbers, and the content's checks refuse a part on a joint the rig doesn't have. The client builds each person's bones from it as it starts, and bakes their motions into raylib's clips, which the animator plays as it played Mixamo's. Two-bone inverse kinematics bends the legs at the knee to reach where the feet go, so a foot that's down stays put while the body goes on. In the death, the ground holds the body up, and Mixamo's files, and the stage that converted them, are gone.
Playing it
55:03 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.