# How can I improve my Animation

The first approaches in animation for my game relied mostly on sine and cosine functions with the time as parameter.

Here is an example of a very basic jump I implemented.

if(jumping)
{
height = sin(time);
if(height < 0) jumping = false; // player landed
player.position.z = height;
}

if(keydown(SPACE) && !jumping)
{
jumping = true;
time = now(); // store the starting time
}


So my player jumped in a perfect sine function. That seems quite natural, because he slows down when he reached the top position, and in the fall he speeds up again. But patching every animation out of sine and cosine is stretched to its limits soon.

So can I improve my animation and provide a more abstract layer?

• possible duplicate of How to make a character jump? – bummzack Dec 15 '12 at 11:55

Movement in games is usually approached as constantly changing states, rather than functions. Programming requires a different approach to math than teached in school because it often requires iterative approaches.

Character::Update()
{
[...]
onGround = [... has the character ground under his feets? ...];
dt = [... time difference between now and the last time this function was called ...];
if(keydown(SPACE) && onGround)
{
yDir = 40; // Can be anything depending on how high you want the jump to be.
onGround = false;
}
if(!onGround)
{
yDir-=gravity*dt;
y+=yDir*dt;
if([... collision with the landscape occured...])
{
y=[... y-coordinate of the ground];
yDir=0;
}
}
[...]
}


Basically you have two components per coordinate, the position and the velocity. Now every frame the gravity is applied to the y-velocity and checked for collision, in which case the y-velocity is zeroed (= not moving in y direction). Moving left and right is done the same way, just that it isn't affected by gravity.

  if(onGround) // You can only walk with feet on the ground
{
xDir=0;
if(keydown(LEFT))
{
xDir = -40; // How far should he move?
}
else if(keydown(RIGHT))
{
xDir =  40;
}
}
x+=xDir*dt;
if([... collision with the landscape occured...])
{
x=[... x-coordinate of the wall];
xDir=0;
}


## General Animation Appendix

Animations are usually hand crafted by artists. In 3D usually bones are used for this, in 2D the animations are usually baked into a spritesheet, in which case it doesn't matter for the engine how they are created. Modern engines often include Inverse Kinematics (IK) for more dynamic motions. Sometimes 2D engines use bones too for the sake of IK.

Other than that there aren't really any "basic concepts" how animations are done, in the end whether a animation looks good or not depends on you fine tuning the parameters. There is no all-in-one solution, everything requires some artistic input.

• This whole answer is pretty good, but I especially liked Programming requires a different approach to math than teached in school because it often requires iterative approaches. – jhocking Dec 15 '12 at 15:05

It depends on your needs, but mostly it is a good idea to implement it as it implemented in real life: by physics! =) You can use 3rd party libraries or write your own stuff. AFAIK most popular lib now is Bullet.

If you don't need all of this, just implement mass, jumping force, gravity force and use Newton laws to calculate position (so your objects will jump with decceleration and fall with acceleration with parabolic trajectories). It will look accurately for any game entity, because its like a real.

• An animation system which relies just on muscle contractions and physics would be very natural and generic, but I believe that this is to performance hungry until now. – danijar Dec 15 '12 at 15:15