4

I'm building an API for myself to do 2D skeletal animation.

I have a Bone class and a Skeleton class.

The Skeleton creates a root bone and then subsequent bones are added via the Skeleton's add method by providing the parent bone.

What I now want to do is add animation and frames.

What I was thinking of, is a class that can load and interpolate animations. So it would be an object that would load an animation. It would then, at each frame, take in a Skeleton and modify the Skeleton accordingly.

Is this a good design? Should an animation take in a Skeleton, or should a Skeleton take in an animation and apply it onto itself?

Kromster
  • 7,181
  • 7
  • 63
  • 111
jmasterx
  • 52,639
  • 96
  • 311
  • 557

3 Answers3

7

It's best to create an Animation that makes use of a Skeleton instead of the opposite. This because, logically speaking, the object Skeleton does not require an Animation to live, but an Animation strongly requires a Skeleton. So you can couple those elements in the Animation itself. Do not put too much logics in the objects, and try to put it just where necessary.

marzapower
  • 5,531
  • 7
  • 38
  • 76
  • I see, so I should base it on dependencies. Thanks! – jmasterx May 23 '11 at 15:49
  • 2
    Well, the best implementation has the less possible coupling between objects, I think. So you should try to link them just where necessary and not everywhere. – marzapower May 24 '11 at 07:25
1

Presumably every bone has a 2d position/angle and an animation is a collection of frames where each frame is a collection of bone identifiers and position/angles?

Then you might consider something like

public class Skeleton
{
    public List<Bone> Bones {get;set;}
    public void Animate(Animation animation)
    {
        foreach(Bone bone in Bones)
        {
           bone.Displace(animation.Displacements.FirstOrDefault(o=>o.BoneId == bone.BoneID));
        }
    }
}
Kaido
  • 3,383
  • 24
  • 34
0

I would create an Animation class containing a std::vector<Skeleton> data member that you can use to manipulate individual Skeleton objects on each frame or interpolate across multiple Skeleton objects in the vector from keyframes. Then when you "play" the animation, you merely have to iterate over the vector, calling out each Skeleton object, and pass that to some other function or class that will display the results on-screen (or do whatever else the Skeleton can be useful for, such as warping a mesh, etc.)

Having an animation object will make it much easier to manipulate the frames of the animation, allowing you to remove/replace frames, etc. Otherwise if you try to pile all of this functionality into a Skeleton object, then you're going to find there's a lot of baggage when trying to manipulate individual aspects of the Skeleton separately from the animation sequence (i.e. suppose you need to change the hierarchy of the Skeleton for a segment of frames, etc.? ... that would be very easy if there is a Skeleton on each frame, but not if you have a monolithic Skelton object).

Jason
  • 31,834
  • 7
  • 59
  • 78