# Object Animation

Aim: To animate an object
Skill level: Intermediate
Files Needed: animation.bb / gargoyle.md2

So far we have just used static objects, its Time to liven up our 3d world and think about animation. Blitz will load in animated meshes from a variety of sources, including X. format (DirectX), MD2 (used by the Quake 2 engine) and lastly 3DS (3D Studio).

How do you draw and animate an object ?, although B3D has a few commands for building shapes such as the cube and sphere instructions - really we need to use a 3d modeller program to create our object.

Most 3d packages will export/import just about every format you are going to ever need, personally I like to work with the Directx format (X). What modeller you choose to use really depends on your preference, Truespace (www.caligari.com), Rhino 3D (www.rhino3d.com) and Canvas 3d (www.amegia.com) - all support X format without any problems.

Recently I've been using the AC3D modeller for my modelling needs, since its simple but powerful. You can download a fully working trial version from official website - (www.comp.lancs.ac.uk/computing/users/andy/ac3d.html), but of course designing an object that has realistic movement really is an art form all in itself. Most of us including me - are going to find it a struggle, luckily places such as 3dcafe (www.3dcafe.com) contain hundreds of ready made free models for us to use.

Time for the source:

| Graphics3D 800,600 SetBuffer BackBuffer() camera=CreateCamera() CameraViewport camera,0,0,800,600 light=CreateLight() man=LoadMD2( "gargoyle.md2" ) PositionEntity man,0,-35,600 RotateEntity man,0,

180,0
AnimateMD2 man,1,.1,32,46
While Not KeyHit(1)
If dist<970 MoveEntity man,.5,0,0
If dist=970 AnimateMD2 man,1,.05,0,31
dist=dist+1
UpdateWorld
RenderWorld
Text 320,500,"An Animated MD2 Demo"
Flip
Wend
End

So whats going on ?

man=LoadMD2( "gargoyle.md2" ) PositionEntity man,0,-35,600 RotateEntity man,0,-90,0

Here, I've used the LoadMD2 command to load an MD2 animated object into the pointer handle 'man'. MD2 models can be found just about anywhere on the net, they are the staple diet of Quake 2 - So as you'd expect there's thousands of ready made objects with animation out there just waiting to be included in your epic. (Although be careful to read any copyright messages that may be attached to them).
Unless you plan on using MD2 models all the time, I expect you will be using the LOADMESH command which does exactly the same thing as the LOADMD2 instruction. (but allows loading of X and 3DS animations)
We wont be going into any of the details that involve creating animations from a modeller, but if your interested I'm sure whatever product you use will contain some sort of tutorial for creating animations. (You should be able to find some tutorials for 3d modelling from my Gamecoding website very soon)

The frames of animation (keyframes) are loaded in with the model, all we have to do is tell B3D where to start - and where to end !. Before we look at this in action, you will notice that I've positioned and rotated the object to a new starting point. Originally the character was designed facing Right, so we need to rotate it by 90 degrees - so it faces us. (I did this by using the RotateEntity command)

AnimateMD2 man,1,.1,32,46

This instruction informs B3D that we want the object 'man' to animate in a loop (so the walking frames can restart), at a speed of .1 of a unit, starting at frame 32 - and ending in frame 46

When we have set the command up, every update (depending on the animation speed you have selected) the animation will run by itself without us having to do any more to it.

If we were using a regular animated object (X or 3DS format) - we would use the ANIMATE instruction, which works exactly like the ANIMATEMD2 command I've used here in this example.

As you can see by looking through the source, I've setup a loop that continues to move the shape forwards until the counter is equal to 970. (using the MOVEENTITY command)

After that the animation is reset to display the standing stance animation.. (frames 0-31). Why not alter the program to include rotation, so that the character can move about freely.

Although I've setup the animation to loop, we can of course just have it run through the animation loop once if we had wanted to it. To do this you just change the MODE flag in the Animate instruction. Here's the instruction in full:

Animate [entity name], [mode 0,1,2 or 3], [speed], [frame to begin at], [frame to end at]

in the case of MD2's you'd use the instruction:

Animate

MD2 [entity name], [mode 0, 1,2 or 3], [speed], [frame to begin at], [frame to end at]

Just incase your interested, here's a rundown of all the mode switches we can use: (Remember we used the loop anim mode)

0: Stop Anim 1: Loop Anim 2: Ping-Pong Anim 3: One-Shot Anim

NOTE: To play an anim backwards use a negative speed value

B3D thankfully makes animation very easy, it really is just a case of setting up whatever we need the anim to do first using the various flags in the ANIMATION instruction before setting it running. However to make the animation look convincing you need to get the timing right. How many times have you looked at a game only to see the main hero character float-walking over the floor rather than connecting with it ?.
But that's half the fun of it !, as you'll see a little effort can really make a big difference.

B3D contains a large array of animation commands that will suit every purpose you will ever need, but tucked away in the depths you'll find a couple of incredible commands that can morph animation frames from one animation to another. For instance, an object of a man running can switch smoothly to an anim of him standing still. If you run this example a few times and watch the point that the object comes to a halt you'll notice the jump. But, by using these commands we could smooth out the change so it would be unnoticeable. Although I wont be using these commands in this tutorial, I may add a new tutorial on the subject very soon.

So what are you waiting for !, go and design the next Quake !!

Tutorial 4 - Texturing