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.

Pingback: 3D Game Programming » Getting started with XNA - First Person Camera - XNA News and Tutorials - DirectX News and Tutorials

I think there is a tiny typo at the end

_model.Root.Transform = Matrix.CreateRotationY(time * 0.5f);;

I did not have a problem with it, but for all the blind copy/pasters out there

No bad, the last ; is just an empty statement. But I’ve updated it just in case

this tutorial is fantastic. It definitely helped me get my head around 3d modelling. I bought a book on using xna by o’reilly and it doesnt cover any of this, only how to load the model and rotate the whole thing, nothing on animating individual parts of it.

This tutorial definitely seems to be helpful, but there’s one thing I don’t understand.

About the 3.14 angle… This angle is between exactly what and what? I assume it’s between the fork and the mast, but what parts of the fork and mast, along which axis?

Hi,

I’m deeply intrested in examining full source code of this tutorial you have provided, but asai see it’s gone from the server. Is it anyhow possible to get it? It’s probably something I’ve been searching for for quite a long time.

Best Regards

Hy,

The source cod download link dont work. Pleace email source cod!!!

Hi,

Gr8 post. But source code is missing. can you please email the source code to me also?

Thanks.