Advertisement Jump to content

Handling complex animation states (Thoughts?)

This topic is 569 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

Hi Guys,

Wondering if I could get your thoughts on handling complex animation states? As my code is starting to get a bit spaghetti.

My player uses acceleration and deceleration to get a nice feel. But, things are starting to get complex as I add more states.

For example, if my player runs to the right, it accelerates nicely and decelerates if right is no longer pressed. If I push left (while the character is running right) deceleration is greater and I then play the turning animation and then eventually start accelerating in the other direction, back into a run sequence.

It starts getting more complex when I decide that I change my mind and want to run right again (mid turn), reversing the turn animation.

If have this all coded and working ok, but I have so many 'if' statements already. 

There is a bunch of other things I want to do also, jump, climb, etc. Which will all have there own transistion animations also (like the turn animation does).

I'm just wondering how I can structure this better and get on top of the code logic before the actual code gets unmanageable.

Normally I would have clear states setup, but the added complexity of acceleration, transition frames, and being able to opt out of the action (in some cases - like turning) are making it difficult.

I'd love to know what the best approach would be in this scenario.

Thanks in advance :)

Share this post

Link to post
Share on other sites

If those articles aren't too dissimilar to what you're doing now, then you're probably in pretty good shape! :-D

The idea behind those patterns is just to cleanly organize the logic.  E.g., a command pattern implementation, or state machine implementation (a graph/node-based approach, not a hard-coded if/then/else approach) is very flexible, and scalable.  Logically, they're not different than if/then/else, they're just easier to maintain.

Share this post

Link to post
Share on other sites

Professional games usually handle animations and transitions between them as a state machine. This is a data structure that includes all the possible animations, the ways one can transition into another, and any conditions for such transitions. You probably need something similar.


Share this post

Link to post
Share on other sites

Kylotan - Yeah. I suspect so.

I have a state machine for the animation states themselves now and it is extremely clean and nice. But, I am still struggling with the structure of the conditional code.

For example, this is the basic concept of what I need for a run / turn condition.

  • Player starts IDLE when there is no input (no problems)
  • Pressing right goes into the RUN cycle state, frame number and speed are acceleration based (no problems)
  • Player then holds left and the TURN sequence initiates. Player decelerates.
  • Player can then hold right and opt out of the turn. Requires a reversal of the turn sequence and apply acceleration again.
  • Or the player can continue the turn and complete the turn sequence.
  • Or the player can release the key, decelerate speed back to zero, and move back into the IDLE state.

There is probably more conditions (and there will be once jumping, running and then crouching, etc.. get applied).

Given the small example above, how would I stop this from exploding into a massive 'if' 'else' mess?

Currently the acceleration and deceleration components of my code are working great. It's the matching up of the correct animation sequence that is causing me the grief.

Thanks again for your help guys :)

Share this post

Link to post
Share on other sites

The state machine needs to contain the conditional transitions as part of each state in which they're relevant. For the current state - whatever it is - there are probably only 2 or 3 possible transitions out of it. Either the animation finishes normally, or a button is pressed and it transitions to one of the other states.

A brief analysis of your situation suggests you have these states, each corresponding to 1 animation:

  • Idle (loops, can transition to Run Right or Run Left)
  • Run Right (loops, can transition to Idle or Turn Right to Left)
  • Run Left (loops, can transition to Idle or Turn Left to Right)
  • Turn Right To Left (one shot, can transition to Run Left or Turn Left to Right (maybe starting partway through))
  • Turn Left To Right (one shot, can transition to Run Right or Turn Right to Left (maybe starting partway through))

Each state only has 2 transitions, so the conditions aren't that complex. The only awkwardness is being able to bail out of a turn half-way through, but if your turn anims are closely matched, you can ask to transition to a specific frame of the next animation in order to perform the change smoothly.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!