Skeletal Animation structure

Hi guys!

I’m working at a 2D game and I want to implement skeletal animation to it.
Can you give me a structure of that please?

My bone classes look like that:


class Bone {
 int x,y;
 double rotation;
}
class Node {
 Node[] children;
 Bone[] bones;
}
class Animation {
 Node node; // the root
 //[...]
 public void animate() {
    //[THAT'S WHERE I GOT STUCK]
 }
}

P.S.: Sorry for my english

I’d say look into Spine for a nice workflow and examine the various runtimes that are provided out of the box (LibGDX for instance) as all the code is already there for usage.

My suggestion:


class Bone{
    Bone parent;
    double x, y;
    double sin, cos; //of the rotation
}

When you want to animate a bone, you can then easily get the parent (if there is one), transform it by the parent and go on. You don’t generally need a list of children in each bone as long as you have a “global” list of all bones in a skeleton.

I’m personally in favour of a child list because it makes iterating the bones in the right order easier, if you just have a global list you need to make sure the list is ordered correctly. I would also duplicate your transformation variables so that as you have an original transformation and a final transformation. I have created a Transformation class below to hold what ever transformation details you need and to hold the multiply calculation that is applicable to that transformation.


class Bone{
    Bone parent;
    Bone[] childBones
    Transformation originalTransformation, finalTransformation
}

Your transformation would then be


public void initialise()
{
    bone.finalTransformation = bone.parent.finalTransformation.multiply(bone.originalTransformation);
    for (Child childBone : childBones)
   {
      childBone.initialise();
   }
}

And you would start from the root bone the call the same method on the children and then their children etc

To animate you could add a third Transformation (animationTransformation) and then use your animation logic to create animationTransformation from the originalTransformation and all you would then need to do is to is to change the formula above would be to replace the originalTransformation with animationTransformation. Some pseudo code is below to help you out. You don’t really need an Animate class (in my opinion) but you could have some sort of controller class to manage all the animate calls because you will probably animate more than one bone at a time and would want to optimise things better.


public void animate()
{
    animationTransformation = slerp(originalTransformation, walkTransformation, 0.67);
   initialise();
}


public void initialise()
{
    bone.finalTransformation = bone.parent.finalTransformation.multiply(bone.animationlTransformation);
    for (Child childBone : childBones)
   {
      childBone.initialise();
   }
}