Getting started with XNA – Animating a 3D Model
In the previous tutorial, we’ve loaded and displayed a 3d model. But can you think of a game with static 3d models? No, it’s time we start animating our model. Most of the time, a 3d artist will handle the animations, and you’ll just load it and play it. But animations aren’t supported out of the box in XNA. There is a demo on creators club which allows you to load skinned animations, and there is a project on codeplex, called the XNA Animation Component Library, which allows you to load skinned and unskinned animations, with multiple meshes, and so on. But in this tutorial, we’ll animate our model from code. We’ll do a tutorial on the XNA Animation Component Library later.
Start by loading the end result of our previous tutorial. We’ll take that model, move the forks of the forklift up and down, and rotate our entire model. You’ll see that it’s relatively easy. Start by opening up our Forklift class, since that will be the only class we’ll be editing.
Add the following variable:
// Store the original transform matrix for each animating bone.
Matrix _forksTransform;
In this variable, we'll store the original transform matrix of our bone that we're going to animate. So in the LoadGraphicsContent method, initialize that variable.
// Store the original transform matrix for each animating bone.
_forksTransform = _model.Bones["vorken"].Transform;
Since we're doing an animation, we'll have to calculate the position of the forks and the position of the forklift every frame. For that, the XNA Framework provides us with a method just for that.
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
Before the base.Update(gameTime); call, we'll calculate the position of our forks. The calculation below might seem a bit excessive, but we want our forks to move up and down relative to our mast, so the forks don't go through them. The easy way to calculate this is using the sine and cosine of the angle. In this case, the angle is 3.18 degrees (you can see this in 3d Studio Max, when you design your model).
// Calculate the new position of the forks.
float time = (float)gameTime.TotalGameTime.TotalSeconds;
double angle = MathHelper.ToRadians(3.18f);
float sine = MathHelper.Clamp((float)(Math.Sin(angle) * Math.Sin(time)), 0, 1) * 100f;
float cosine = MathHelper.Clamp((float)(Math.Cos(angle) * Math.Sin(time)), 0, 1) * 100f;
// Apply the transform to the Bone
_model.Bones["vorken"].Transform = Matrix.CreateTranslation((float)(sine), 0, (float)(cosine)) * _forksTransform;
First off all, we calculate the new position of our forks, relative to the mast and to the time. We clamp the sine and cosine so they aren't negative. Then we apply the translation (movement) to our bone. If you press F5 now, you'll already see the forks move relative to the mast.
Now, that which we can do for a bone, we can do for an entire model. Let's rotate our model while our forks are moving. Add the following code just below the previous code.
// Set the world matrix as the root transform of the model.
_model.Root.Transform = Matrix.CreateRotationY(time * 0.5f);
That's it, we've animated our model. See how easy that was? Next up, we'll add a first person camera, so we can move through the scene.
As usual, you can download the source for this tutorial.

4 Comments
Jump to comment form | comments rss [?] | trackback uri [?]