Varcade Dev Blog

MD2 Charater Animation for XNA

by on Jun.26, 2011, under Graphics, XNA

Skeletal animation has been the standard method for doing character animation in games for awhile.  XNA provides a GPU accelerated skinned model pipeline by default.  On WP7 phones, where using the CPU for the skinning can actually be faster,  a CPU Skinning pipeline is provided in the online samples library.

There are times when you don’t need the complexity of skeletal animation, when smoothly blending between multiple animations isn’t required.  Such as the case I had for doing a flock of 3D birds, where they have only a single ‘flap’ animation cycle, but it turned out useful for other objects or characters that have simple motion cycles.

So the best technique I found to save processing time was going back to the old school style of Quake animations, sometimes known as vertex blending, or morph targets.

The Tools:

MD2 Model Class:

The MD2 class above can handle the loading and animating of MD2 files.  It does not use the Content Pipeline.  You will want to put your MD2 files in the Content section of your project and set them to Build Action: None, and Copy to Output: Copy Always/Newer
I had a few issues with the class, and made some improvements

  1. It expected the frame names in the MD2 to be a specific format (name+number), to separate them into diferrent animation sequences.  Depending on how you are creating your MD2 files, you may need to play with frame names. I commented out that code and just had all frames of the walk cycle named WALK, next set of frames named RUN, etc. ( see other tools below for ways to rename your frames or combine animations if your exporter doesn’t )
  2. It loaded the raw normals information from the MD2, but did not use them.  If you want to do lighting in XNA, you will need normals.  Normals in the standard MD2 are stored as a byte index, which coresponds to a vector lookup table.  Being only 162 values in this lookup table, the normals are only an approximation of the true normal and are pretty rough.  I implemented the code needed to recreate the normals in XNA.  They give you what the format supports, but you may want to calculate proper normals yourself.
  3. It supported animation framerate ( playback rate ), but only increasing it. Now things can be slowed down as well. This has been moved to a Update(GameTime) function.
  4. There is no interpolation between frames. This works fine for many cases. The phone is only running at 30fps, and my animation in the MD2 is 30 frames per sec, so things look smooth. If you are targeting a 60fps platform, or want to be able to increase/decrease the animation speed dramatically, you might want to implement vertex interpolation. I would probably just store my animations in the MD2 at 60fps if that were the case, skipping the interpolation and trading memory for CPU time.

The original code was released under a Creative Commons License, so here is my version.


Q2 Modeller 0.91: (dropbox download)

An oldie but a goodie.  It can load/save md2 and several other formats, rename each frame, and combine multiple files into one ( walk cycle, death cycle, etc ).  I used it mostly to test the MD2’s I was creating, while I worked though problems in the MD2 XNA class.


Content Creation:

For content creation I was able to use the exact same skin and bones models I had already created for .FBX or .X formats, and just export to MD2 with no additional work required.  I use 3D Studio Max, and found a couple of ways to export to MD2. The first is a plugin called QTip. It’s $25 and the animation exported worked first try in XNA.  I also found some Maxscripts (export, export, import) that do about the same job.  These are 2 version of the same exporter here, 1.01 by the Adam Barton,  and 1.6  with some additions by JCA, plus some changes done by myself to work with the XNA class ( frame names ), and made so both scripts can be installed at the same time. ( put them in Scripts/Startup )


Mummy Walk

Mummy with Motion Captured Walk Cycle


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!


A few highly recommended websites...