Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
possible duplicate of How to make a character jump? –  bummzack Dec 15 '12 at 11:55
2  
Your question is about jumping and you posted your source-code which leads to the conclusion that you want to improve on that. You're talking about implementation, not concept... only the concept tag implies otherwise. Also your statment "So how is animation done today?" implies that the way you do it is how it has been done in the past. I suggest you improve your question so that you actually get the answers you're hoping for. As of now, the answers in the question I linked to perfectly answer the question as it is now. –  bummzack Dec 15 '12 at 13:33

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
1  
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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.