Sign in to follow this  

What's the best way to manage multiple animations for objects?

This topic is 1958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm interested in this topic from a more high level design perspective rather than a low level implementation perspective. For what it's worth, I'm working in C++ with animated 2D sprites (and I can animate sprites just fine), but the concepts I'm interested in aren't specific to C++ or 2D. Here's the situation:

For simplicity's sake, let's say you have 3 objects: Player, Enemy, and Cannon. Player and Enemy have some common animation sequences (that is, the animations are different but they represent the same states), like "walk," "die," "attack," "jump," and "idle." However, Player has a couple additional animation sequences, like "cast_spell," "idle_1" (which is him just sitting there breathing), "idle_2" (which is him looking around), "idle_3" (which is him picking his nose) ("idle_1" is the common animation for the "idle" state that gets played the most, but occasionally "idle_2" and "idle_3" get played simply to give the game a little more depth). The Cannon object is quite different though: it has "idle" (which is it just sitting there), "attack," and "reload," but doesn't have anything else.

What have you found as a nice way to organize, structure, and refer to these various animations for these various objects?

I've thought about this, but I haven't come up with a solution I particularly like. It's something like this:
[code]
// Regex rule for referring to a sprite's animation files for resource compiling/loading:
"spritename_animation_(+d)_(+d).png" // here, $1 is the animation number, and $2 is the frame number

// Note: I've also considered a rule like "spritename_animation_(.+?)_(+d).png" where $1 is the animation
// name and $2 is the frame number (don't stress too much if the regex isn't quite correct... it's the ideas
// I'm focusing on now; the regex is probably wrong)

[...]

// Here's some pseudo code of how it might work:
struct Player
{
AnimationSequence sequence(AnimationLoader::load("player")); // loads corresponding animation compiled with rule "player_animation_(+d)_(+d)"

enum State
{
WALKING,
IDLING,
DYING,
ATTACKING,
JUMPING,
CASTING_SPELL;
} state;

void update() // updates player, also advances animation frame
{
// The selectAnimation() code would be messy, and is where I have issues with this kind of design.
// Each class type would have to have it's own selectAnimation() function so that the appropriate
// animation could be selected. However, I can't really think of a way that this can be avoided.
// The mapping of states to animations isn't 1:1 either.
sequence.selectAnimation(state);

sequence.advanceFrame();
}
};
[/code]

I've also thought of having "animation groups" and grouping animations into these "groups." That way, multiple "idle" animations could be in the "idle" group, and multiple "cast_spell" animations (for various types of spells) could be grouped together. Then an object's state would directly map to a single animation group, and selecting a particular animation in the group would depend on additional parameters/state of the object.

I just don't know how much I like this design, and I'm interested in seeing how other people deal with architecturing their code and assets when animating their objects.

Share this post


Link to post
Share on other sites
Animations in my game are structures the following way:

1. I have several channels for each entity (movement,action,reaction), a channel can contain up to two animations (while blending animation from a to b), the final animation is the weighted sum of all channels (think of photoshop layers with alpha [i]transparency[/i]).
2. I have a fix number of unique animation ids (idling,dying,attacking...) much like your enum.
3. Each animated entity has a mapping from animation id to animation, more then one animation id can point to the same animation or to none at all (not supported).

Share this post


Link to post
Share on other sites

This topic is 1958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this