PDA

View Full Version : Animation: how do I do it?



cragwolf
04-03-2006, 12:56 AM
I know this sounds like a stupid question, but here goes anyway. How do you do animation?

For example, I might click on a button, and I want that button to do a one second animation. Or I might click on an army icon, and then that icon moves from the left end to the right end of the screen. Or I might click the "Execute" button and a bunch of animations take place on the screen.

I'm using OpenGL, but what I'm asking for is generic advice to incorporate animation into my game, independent of the graphics library (e.g. GDI+, SDL, OpenGL, DirectX) I use. Surely there are some library-independent aspects to animation?

Here's my game loop, by the way, pretty standard stuff:

while Game.Running do begin
Game.HandleEvents;
Game.Update;
Game.Render;
end;

I hope that makes sense.

AthenaOfDelphi
04-03-2006, 09:27 AM
Hi Cragwolf,

The simplest I guess is to have a sequence of images and to blit them onto screen in order to create the effect of movement. This is fine for 2D, but for 3D, I'm guessing you either need a sequence of models or some kind of model change data that adjusts the model as it moves.

Sorry I can't be of more help, but I'm pretty new to this graphical game business myself :-)

I get a lot of my info from the articles, library and tutorials sections on the forums. Check out the top menu for the links.

Traveler
04-03-2006, 11:31 AM
I have struggled with the same problem as my competition entry is entirly made in opengl without using a wrapper.
The advantage is a ton of experience since you have to figure everything out for yourself, but on the other side, it takes a great deal longer.

Anyway, animation: The idea is actually very easy. In fact you've used it countless times already. Take 24 pictures, put them next to eachother. Device a way to make only one visible at the time and then pull the strip at 1/24th of a second and voila, animation.

Using opengl: load your texture. (1 image composed of 24 smaller images placed next to eachother) Create a timer that's able to generate ticks at fixed intervals. At every tick, clip the texture to display the correct image. Thats bascially it.

cragwolf
04-03-2006, 12:26 PM
Maybe my terminology is wrong, but for me animation means anything that's not static. So if I draw a rectangle at a slightly different position at each frame, then that's animating. I like the idea of having different frames representing animation, particularly more complex animation, but you can understand it's not always necessary.

I would be looking to do frame-rate independent animation. It should take 1.4 seconds for that rectangle to move across the screen regardless of frame rate. So I guess I need some kind of a timer, some clock. I guess also that a unit (think war games or strategy games) could be in an animating state, and associated with that state is a goal of some sort (reach position A), and each frame (or less often?) it could check if it had reached its goal, and if it had reached its goal, then it would switch off the animating state and go back to its regular static state.

These are the kinds of thoughts I've had in my mind since starting this thread. I'm just hoping that someone with more experience will point me in the right direction. Even just to give me the right jargon to use, or some references to books and web sites.

Nitrogen
04-03-2006, 05:22 PM
Well, framerate independant movement is fairly easy..

Here's how I do it:



Procedure DoTimeKeeping;
begin
LastTim := Tim;
Tim := GetTickCount; //Replace GetTickCount with QueryPerformanceCounter if you need the extra resolution
T := (Tim-LastTim)/100; //replace the 100 with any magic number of your choice.
if T <= 0 then T &#58;= 0.01;

end;


Then when you have a button or object moving on the screen, you simply multiply it's velocity by T.



button.x &#58;= button.x + 0.5*T;


[/code]

Chebmaster
21-05-2006, 02:07 PM
Use states.
Define that object could be in one of the N states, and each render frame re-calculate the time elapsed since the moment it switched into that state.

Now the animation comes down to RenderIt(<state number>, <state phase>).

If your object, for example, is imp, and the state number corresponds him chasing the player, then this procedure takes an imp model, looks for the walking animation frames, then calculates, according to phase, the two specific frames and their blend factor, blends them, and finally renders the imp.

If you animate a simple 2d sprite, you need only to calculate the frame number out of the phase, and then draw it.

Thus you had the work split to two simple jobs: calculating the phase according the time passed and calculating the frame you need according that phase. Note, that animation frames not necessarily should be distributed even along the phase axis.

For cyclic animation just substract the top limit out of the phase if it goes over it.