Is this a good OO design?

I am creating 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 through the skeleton addition method, providing the parent bone.

Now I want to add animation and frames.

What I was thinking about is a class that can load and interpolate animations. So this will be the object that will load the animation. Then in each frame he would have to have a skeleton and change the Skeleton accordingly.

Is this a good design? Should an animation take a skeleton or should a skeleton take an animation and apply it to itself?

+4
source share
3 answers

It’s best to create an animation that uses a skeleton instead of the opposite. This is because, in terms of logic, a Skeleton object does not require animation for life, but an animation strongly requires a skeleton. Thus, you can link these elements in the animation itself. Do not put too many logics in objects and try to place them where necessary.

+7
source

Presumably, each bone has a 2d position / angle, and the animation is a set of frames, where each frame is a set of bone identifiers and position / angles?

Then you can 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)); } } } 
+1
source

I would create an Animation class containing the std::vector<Skeleton> data element, which can be used to control individual Skeleton objects on each frame or interpolated through several Skeleton objects in the key frame vector. Then, when you β€œplay” the animation, you just need to iterate over the vector, calling each Skeleton object and passing it to some other function or class that displays the results on the screen (or does something else Skeleton may be useful, for example, mesh deformation, etc.)

The presence of an animation object will greatly facilitate the manipulation of animation frames, allowing you to delete / replace frames, etc. Otherwise, if you try to collect all this functionality into a Skeleton object, then you are going to find a lot of luggage there when you try to manipulate certain aspects of the Skeleton separately from the animation sequence (i.e., suppose you need to change the Skeleton hierarchy for the frame segment and etc.? ... it would be very simple if there is a skeleton on each frame, but not if you have a monolithic Skelton object).

0
source

All Articles