Best Method to Implementing Smooth Animation

What is the best method to programming more advanced 2D animations in Java, like skeletal movement and such?

For example, in a 2D game, a character walking animation with fluid leg movement as opposed to flipping between 3-4 animation sprites.

Is it best to just make a TON of sprites? Or is there a better way?

You could use http://www.java-gaming.org/topics/spine-2d-skeletal-animation/27914/view.html

I did notice that, and I might give it a try, but I’m more interested in first trying to just build something decent into the engine from scratch.

There is no “best” way to do this. This is done in 3D applications by creating a skeleton, which is a tree of bones. When you move one bone all of it’s children move. Once a skeleton is created you can create animations in frames, which is the state of each bone relative to its parent (the distance and angle for simple skeletons). To animate the model you attach each vertex (or a sprite in your case) to a bone (or multiple if you’re crazy).

Here’s one way of doing it:


public class Bone {
    public Bone parent;
    public Bone[] children = {};
    public Vector position = new Vector();
    public float angle;
    public float distance;
}

The update code


if ( parent != null ) {
  position.set( parent.position );
}
position.x += Math.cos(angle) * distance;
position.y += Math.sin(angle) * distance;
for (int i = 0; i < children.length; i++) {
   children[i].update();
}

So the root nodes should have the position of the actual sprite.

There are MANY ways of doing this… so there are several strengths (simple) and weaknesses (angles) to this approach.

Now to animation, I imagine you could create a skeleton like this:


public class Skeleton {
   public Bone[] bones;
   public Vector position;
   public void update() {
      for ( int i = 0; i < bones.length; i++ ) {
        if ( bones[i].parent == null ) {
          bones[i].position.set( position );
          bones[i].update();
        }
      }
   }
}

And a frame would have a snapshot of the bones angles and distances:


public class Frame {
   public float[] angles;
   public float[] distances;
   public float duration;
}

And an animation has a set of frames:


public class Animation {
    public int index;
    public float time;
    public Skeleton skeleton;
    public Frame[] frames;
    public void update(float dt) {
       Frame f = frames[currentFrame];
       time += dt;
       if ( time >= f.duration ) {
          currentFrame = (currentFrame + 1) % frames.length; // loop through frames
          time -= f.duration;
       }
       // update skeleton based on current animation
       Frame f0 = frames[currentFrame];
       Frame f1 = frames[(currentFrame + 1) % frames.length];
       float delta = time / f0.duration;
       for (int i = 0; i < skeleton.bones.length; i++) {
         skeleton.bones[i].angle = (f1.angles[i] - f0.angles[i]) * delta + f0.angles[i];
         skeleton.bones[i].distance = (f1.distances[i] - f0.distances[i]) * delta + f0.distances[i];
       }
    }
}

With all of this I’m sure you can figure out the rest, you have a Model which has a Skeleton and a list of Animations it can play. You could add animation queueing, transitioning, reversing, etc.

To attach sprites (i like to call them Limbs) you just need to know your bone, offset, and whether you rotate with the bone


public class Limb {
   public Bone bone;
   public Sprite sprite;
   public Vector offset;
   public boolean rotatesWith;
   public boolean sharesAngle;
   public float angleOffset;
   public void update() {
     sprite.position.set( bone.position );
     if ( rotatesWith ) {
         // rotate offset by bone.angle and add to sprite.position
     } else {
         sprite.position.add( offset );
     }
     sprite.angle = (sharesAngle ? bone.angle + angleOffset : angleOffset); 
   }
}

Something like that…

Hope it helps!

Very well written answer thank you! I’ll give this model a try.