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 Comment more...

Glueit – Sprite Sheet Maker – Download

by on Mar.17, 2011, under Graphics

GlueIt’s host download site has been missing for awhile , and I’ve gotten some emails asking if I had a copy.  So here are a couple mirrors.. (local) (my Dropbox)

30 Comments more...

Generating Terrain with a Shadow Map

by on Mar.17, 2011, under Games, Graphics

A good bit of time was spent finding a technique to generate original levels / playfields.  I hope to be able to create a variety of different levels, some with a single enemy path, some with multiple paths, in a range of environments.  I ended up using Terrgen Classic to generate a terrain. The tricky part was figuring out how to ‘carve’ a custom paths into that terrain, but I think the results are pretty good.  By rendering a near overhead view, I was able to get the perspective to match that of my game units. Here is the results of the first test level.


Here is how the camera was setup in Terragen for an overhead shot:

The terrain itself is created using a gray scale height map. This can be generated in Terragen, or imported from a 3rd party app.  I started by first generating a random terrain in Terragen, exporting that to a paint program, and creating the paths ( roads ) by darkening  ( lowering ) them.

terragen landscape settings

Once you have a terrain created or imported, you will want to color it.  This is done with the surface map controls in the same dialog above.  The surface maps allow you to define color based on Altitude and Slope.


terragen surface layer controls


I was able to create a road by telling the surface below 280 meters and relatively flat to be gray.  A 2nd surface was created for the walls of my cannon roads, by coloring only terrain at a similar altitude, but with a much steeper slope .


Lastly, by rendering another version of the terrain without color, and high exposure, I was able to catch only the shadows and create a shadow map.

terragen shadowmap

This can be rendered using transparency in the XNA Sprite Batch after any ground units ( enemies ) so they appear to move into and out of shadows cast by the landscape.  Performance on the XBox doing a full screen overlay was fine.  It even worked well on a WP7 Phone.


terragen level, enemies, and shadowmap

Leave a Comment more...

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...