No pretty screenshots today. Just long walls of densely-packed text. Suck it up. We can’t work on bling-mapping every day.
I’ve mentioned that this was the big hurdle in my project. This is where I’ve been planning to run aground and give up. Two two or three times in the past I’ve tried to nail this down, only to get lost and frustrated, and eventually give up.
An animated character is a seriously complex beast. There are a lot of steps, all of them are hard, and a mistake in any of them will mangle the end product. The process goes something like this:
- Pick a 3D file format out of the dozens available. Collada files? 3DS Max files? Maya? .X? No file format is perfect. Some are harder to read than others. (And some are absolute devils to read correctly.) Some have acceptable documentation. Some are proprietary file formats that have been (partly) reverse-engineered by enterprising go-getters. Some have only partial or conflicting documentation for how to read them. There are a lot of different types of information that you will need, and a lot of information you won’t need. Surface normals, texture mapping coords, animation data, vertex weighting, surface materials & lighting properties. Some files include non-model stuff like the position of lights and the background you’re using in your 3D program. You need to find a format that is as easy to read as possible, has the data you need, and has as little extra stuff as possible.
- With your file format all picked out, you have to write an importer. Read that data into your program, convert it from its native coordinate system into the one used by your game, and turn it into polygons for rendering.
- Pick out an animation file format. This is a file that will describe the movements for your character. These poses will describe how to make your character walk, hold a gun, crouch behind chest-high walls, crouch over the face of fallen enemies, or whatever else your characters need to be able to do. (Although apparently those are the only things anyone cares about?) Choosing this file format is a lot like the process in step 1. (Some formats support both models and animations, but you still have to read in this other data. And using the same files for both comes with its own set of problems, which I’ll talk about later.)
- Write an importer for that animation data.
- Write a system to apply those animations to the model. You need to be able to smooth animation frames, blend between animations, play animations back at varying speeds, and a bunch of other stuff. The animations move the character’s skeleton. The skeleton moves the vertices, which move the polygons, which makes the magic happen.
- Now fire up your program. Doesn’t work, does it? The limbs are flailing around randomly. Is it a problem with your model importer? A problem with the animation importer? A problem translating from the foreign coordinate system to your own? A discrepancy preventing the two from properly relating to one another? Or perhaps your understanding of the animations, and both file formats is perfect, but you just have a plain old bug in your implementation? Congratulations. Have fun fishing around in those 2,000 lines of code, looking for a problem that you might not even be able to spot if I pointed to it. You are well and truly screwed.
Previously, this was how I tried to do it, which is why I failed. I’m taking a somewhat more cautious approach this time. This time, I’m going to work in the opposite direction, starting with the animation system and working my way backwards to importing models. Of course, I can’t very well develop an animation system without something to animate. So I make this guy:
A stickman. (Or, given the rather lopsided ratio of legs to torso, perhaps this is a female? Bah. YOU do better by trying coords in a text editor.) He’s actually more a collection of points than a proper “stick” man. The lines you see are just there to show the relationship between the points. Each point draws a line to its parent. I made this thing by hand, using code. Kind of annoying and a slight time sink, but it guaranteed that my skeleton would be exactly as I intended, without needing to write an importer first or wonder if it was working properly.
The next step is to get him moving. I don’t have an animation system yet, so I’m just having him rotate his knee on a sine wave.
Next, I stick a polygon to him. This is done by assigning the vertex points to a particular joint. (People think of skeletons as bones, but in graphics skeletons are really joints. You can see this when you look at models created by different artists. Some name their skeleton parts, upperarm, forearm, hand. Others would name those same parts shoulder, elbow, and wrist. Neither one is technically wrong, as long as everyone agrees on how things should be named.) Roughly, this process boils down to me saying, “This vertex belongs to the knee joint. When the knee bends, this vertex needs to pivot in space around the knee.”
It works. If I bend the knee, the polygon moves in space with it, making it look like the polygon is “attached” to the bone. If I move the hip, it rotates around the hip properly.
This should also explain the bug you may see in videogames once in a long while: You’ll see this brief glimpse of a character standing arms-out, feet slightly spread. That’s a character that hasn’t had any animation applied.
I add a few more polygons, placing them right where his bones would be. I apply a bunch of sine waves to all the joints, and he begins bending and twisting in space. All the polygons go where they should. None drift off or pivot around the wrong point.
It’s stupid, ugly, and primitive, but the important thing is that it is correct. Now I can make an importer. When things go wrong (and they will go wrong) I’ll know it’s a problem with the importer, secure in the knowledge that the underlying animation stuff is correct.
The Strange Evolution of OpenGL
Sometimes software is engineered. Sometimes it grows organically. And sometimes it's thrown together seemingly at random over two decades.
Marvel's Civil War
Team Cap or Team Iron Man? More importantly, what basis would you use for making that decision?
Games and the Fear of Death
Why killing you might be the least scary thing a game can do.
How to Forum
Dear people of the internet: Please stop doing these horrible idiotic things when you talk to each other.
The Death of Half-Life
Valve still hasn't admitted it, but the Half-Life franchise is dead. So what made these games so popular anyway?