Varcade Dev Blog

Unlocking the framerate in XNA with Mango

by on Jul.31, 2011, under XNA

With Mango, developers have the option to run their XNA apps at the full refresh rate of the devices.  Previously, this was limited to every other frame.   Here is the code, in the Game class constructor:

graphics = new GraphicsDeviceManager(this);
graphics.PreparingDeviceSettings += (s, e) => { e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One; };

The default is PresentInterval.Two

Leave a Comment more...

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)

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

XNA Drawing 2D Lines, Shapes, and Laser Beams

by on Feb.26, 2011, under Coding, Games, XNA

I’ve been using this great XNA lib for drawing 2D lines, circles, and shapes for awhile. I’m using an older version that I cleaned up to minimize garbage collection ( to none! ), but the code is still being updated by CentauriBoy on the AppHub forums, and recently got anti-aliasing and filled shapes added.

Home thread on XNA forums:


Docs:  HOW_TO.txt

A simple example I use to draw a laser beam

beam = new BasicPrimitives(Game.GraphicsDevice);

beam.AddVector( this.position ); // Vector2
beam.AddVector( target.position ); // Vector2
beam.Colour = new Color(255, 255, 255, 180);
beam.Thickness = 1;

Notice the color in this example is slightly transparent. For my laser beams, I’m stacking 3 to 5 of these lines with slight offsets to get a glow effect. Also note that using ‘new Color’ in a draw loop is bad practice. You never want to be creating anything in your loops because it will later need to be removed by the garbage collector. To eliminate garbage collection, you would define your colors once in a constructor, and re-use them.

1 Comment more...

Timing Problems and Solutions

by on Mar.07, 2010, under Games

I added a 2D Camera to the game. This allows zooming, panning, and rotation of everything in the game word.  I’m not actually using it for anything yet, but I imagine it will be nice to have when I have to start worrying about supporting widescreen vs standard def resolutions.  Also it can be used for special effects like camera shake on big explosions.

Now I noticed my Pickers movement was not as smooth as it should be, even though my frame rate was a easy solid 60 fps.  Others have noticed this as well and there has been a good bit of discussion on the subject.  The summary is, by default, the XNA framework runs in a fixed timing mode, fixed at 16.6 ms per update, or 60 fps.  But if you do some testing of this, people have found that the end results are actually more around 58 fps, somewhere frames ARE being dropped.  Depending on the kind of game and the kind of movements being done, ( or you ability to perceive such things ) it isn’t always noticeable.  It was definitely bothering me, so I changed the timing mode to variable time step, and things smoothed right out.

 this.IsFixedTimeStep = false;

There really is no downside to not using fixed timing, except it require you to do you own time management in the update loop. It’s simple to do, and I was already doing it anyway, a habit from previous physics related projects. Anything time related just needs to be applied against the Time Delta.  So instead of moving an object by X amount because you assumed a fixed step, you move it scaled by the actual time passed:

// calculate dt, the change in time since the last frame.
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
gameObject.position += gameObject.velocity * dt;

Now with things running silky smooth, I noticed another timing issue. The enemies where changing speed as they followed their path to the destination. Typically slowing down as they approached the next waypoint. It turns out that I’m not crazy, but this is normal behavior using the CatmullRom interpolation in general. Basically, if you are moving between points A and B, and you are taking steps of 1% at a time, you would expect to be 75% of the way to point B after 75 steps. This is just not the case though, you may closer or further depending on how the algorythm defines the spline. The solution to this is to find what point on the spline represents the actualy distance we want. Easier said then done. It’s really not THAT hard, this page describes the idea of measuring the arc lengths of each splice segment, and using them in a lookup table. The only problem is the implementation is left to the reader ( no cut/paste!  ).  So I tackled it, it took quite a bit of time and debugging, but I am happy with the results. The Enemies now move at a constant velocity in world units, no matter the structure, complexity, or irregular placement of the path waypoints they are following.

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