Jump to content
• Advertisement

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

This topic is 2241 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:
 // 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(); } }; 

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

##### Share on other sites
Advertisement
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 transparency).
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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
14
5. 5
• Advertisement

• 12
• 9
• 16
• 11
• 9
• ### Forum Statistics

• Total Topics
632594
• Total Posts
3007309

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!