Character animation is another area of 3D programming that's critical to game development. Nothing makes a game more exciting—to the point that it draws you in and makes you feel like you're there—than being able to interact with animated, lifelike characters. Even better, you can use the Microsoft DirectPlay API to add network capabilities to your game (as you'll see in Chapter 15) so that users can play an immersive, head-to-head game with friends. Animated characters can represent real people that are playing on remote computers, animals in your 3D world, and so on.
You can create animated characters to add to your 3D world in several ways. Here are two of the most popular methods:
On the CD accompanying this book are two characters—an animated horse model and an animated human model—provided by Credo Interactive, the makers of the LifeForms series of character animation software. These characters have been animated using LifeForms and then exported as animated 3D studio (.3ds) file format objects. I have also converted them to DirectX file format. These are in the \models\credo directory on the CD.
A segmented character is a 3D model with separate components for all the articulated body parts (such as the head, upper arms, lower arms, hands, and torso). Segmented characters work wonderfully with Microsoft Direct3D because you can directly manipulate their body parts using the transformations (rotation, translation, and so on) that Direct3D supplies. For example, you can move a character's arm up and down by rotating the shoulder joint the desired number of degrees. All the attached body parts (the lower arm, the hand, and so on) will move with the upper arm as expected because of the hierarchical nature of the transformations in Direct3D.
The ability to apply motion capture data to 3D characters is particularly thrilling. I often use LifeForms, a piece of character animation software by Credo Interactive. This package is not only powerful, it's easy to use. The folks at Credo have supplied a demo copy of LifeForms for this book's companion CD that allows you to experience all its power and convenience. Credo has also supplied two 3D character models whose body parts will accept the motion capture data you create with the LifeForms software. In addition, Credo has supplied several motions that you can apply to your own 3D characters.
Single-mesh characters are very popular in 3D games and simulations. To use this technique, you create a set of single-mesh models that, when played in a 3D sequence, produce an animation. Each step of the animation is stored as a mesh representation of the shape of the body. Therefore, to replicate the motion of walking, you store a set of 3D models that represent each movement made in a complete walking sequence. You have models that represent the body starting to take a step, models in the various positions that the body assumes as it's progressing through the step, and finally models representing the body after the step is complete.
Packages such as the HyperMatter and Character Studio plug-ins for 3D Studio Max enable you to animate single-mesh characters. In Character Studio, you can animate a character by attaching a skeleton to the character's single-mesh skin in a way that allows the skin's vertices to move convincingly with the bones. Using this technique, you can attach motion capture (or hand-generated) data to the skeleton components, causing the mesh to move accordingly.
The ability to animate the mesh itself using tools such as HyperMatter is even more exciting because the mesh represents the character's flesh or clothing. With HyperMatter, you can make clothing look like it's made of fabric and flex realistically as the character moves.
One key feature that Immediate Mode neglects to provide developers is the ability to load either the segmented or the single-mesh characters or objects you might want to use in your games. Although you can always create 3D objects on the fly using the vertices (such as vertex buffers) with DrawPrimitive, it's often much more logical to create and animate an object in advance. The main reason Immediate Mode doesn't support this capability is that developers have a variety of methods they use to create their applications' objects and characters. Because these methods often require different file formats and animation techniques, attempting to provide a standard format for all developers using Immediate Mode wasn't practical. In addition, the complexity of the file formats, such as 3D Studio (.3ds), makes it difficult to import the files correctly.
The RoadRage application on the companion CD allows you to load static 3D models (furniture, buildings, and so on) stored in the 3D Studio format (.3ds). The code that implements this capability is in the import3ds.cpp file. The CD also contains a program called RoadRage Map Editor (rr_editor.exe) that allows you to generate your own 3D environment for RoadRage. Using RoadRage Map Editor, you can create your own 3D world and place objects such as roads, buildings, lights, and signs in it, from a top-down view. See the readme.txt file on the companion CD for more details on RoadRage Map Editor.
Although discovering how to write code to load and animate 3D characters can be a great learning experience, it's one of the most difficult tasks game developers face. The difficulty lies in the fact that understanding Direct3D in detail is only the first step—you must also understand character animation and 3D modeling and the multitude of file formats the various rendering tools use. This depth of knowledge isn't something I'd expect from every developer interested in using Direct3D. Many of you are no doubt familiar with the game Quake II, which uses models constructed with single-mesh characters. Each movement the models in this game make consists of a sequence of frames that produces an animation. The following table shows the models' movements and the frames represented by the various positions of each mesh.
Motion | Frames |
---|---|
Stand | 0 through 39 |
Run | 40 through 45 |
Attack | 46 through 53 |
Pain1 | 54 through 57 |
Pain2 | 58 through 61 |
Pain3 | 62 through 65 |
Jump | 66 through 71 |
Flip | 72 through 83 |
Salute | 84 through 94 |
Taunt | 95 through 111 |
Wave | 112 through 122 |
Point | 123 through 134 |
The RoadRage code on the companion CD lets you load an animated 3D model (in Quake II MD2 format) and "play" the various motions you want to use in a complete, real-time 3D game. In addition, you can play the various motion sequences of the model through keyboard or code control.
The companion CD also includes a number of excellent shareware applications (written by a variety of authors) that allow you to create your own models for use in Direct3D (or in Quake if you want). These tools also enable you to "paint" a character so that you can apply details such as clothing and facial features. Once you're equipped with these tools, you'll be well on your way to creating incredible animated characters for your games and simulations. For more information on these programs, see the readme.txt file on the companion CD.