Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How would you do player animations?  (Read 4526 times)

0 Members and 1 Guest are viewing this topic.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
How would you do player animations?
« on: January 06, 2014, 04:54:49 am »
I make games in C++ and SFML with OpenGL from GLEW. I have SFML for input and windows, and all of my rendering is done from normal OpenGL. Currently, when I use animations, I have an Animation class that holds all of the frames for each animation, and how long each frame should be displayed (two int std::vectors, one for ticks and one for frames), but the problem is when I have to add new animations to my entities or dealing with multiple animations at once.

During initialization, I create animations for my characters, (which I want to change to data-driven, but not a huge priority right now) and I store the animation to my current character and any enemies that start out. The problem is, I have like 12 different animations that have to be stored(in each direction: walk, idle, run, hurt, attack), and if I want to spawn new entities, I have to either store a copy of each animation for each entity type (which I want to eventually be randomly different), or I have to load them each time. It just seems.. difficult to keep track of so many. All the animation tutorials I find online are the very basics about changing your texture coordinates for the next frame, but nothing too in-depth.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How would you do player animations?
« Reply #1 on: January 06, 2014, 06:11:50 am »
It depends on exactly what you need, but there's probably nothing wrong with storing all the animation data in the character instance. E.g.,

Code: [Select]
class Character {
public:
  Character();

  // build character animations
  void addAnim(string name, Animation anim){..}

  // anim control
  void playAnimation(string name, bool loop = false){}

protected:
  vector2 position;
  string currentAnim;
  int currentFrame;
  float animationTimer;
  map<string, Animation> animations;
};

There's different optimisations you can apply here depending on your use case.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Re: How would you do player animations?
« Reply #2 on: January 06, 2014, 06:21:03 am »
Well, right now my entities all have their own "animations" stored, but they're all individual objects, so I have like 16 Animation objects because I need one for each direction/action. The code is messy, and I'm just wondering if anyone has completed a game or has come close that could show me how they did it.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How would you do player animations?
« Reply #3 on: January 06, 2014, 06:38:46 am »
Well in my game, Moonman, I basically have
- sprites (lists of frames with pivot points etc)
- spritemaps (a map<fourcc,id>, which maps short labels like "jump" to a sprite id)
- both of these are stored in lookup tables, referenced by id
- in-game entities (a campfire, an arrow, a dog, etc) store an ID to their relevant spritemap, as well as the current frame, spritemap mode, timing information, etc..
- some in-game entities are built of multiple sprites, connected together with pivots and anchors. so e.g., a person has a legs sprite, a head sprite, a slot for a weapon sprite, etc.

The renderer then basically does this:
Code: [Select]
for(Entity e: entities){
  SpriteMap& spriteMap = resourceManager.lookup(e.spriteId);
  SpriteFrame frame = spriteMap[e.mode][e.frame];
  sf::Sprite sprite = buildSFMLSpriteFromFrameData();
  sprite.setPosition(e.position());
  target->draw(sprite);
}

This is the basic idea, but I also cache things, by storing copies of the sprites in the entities to avoid the resourceManager lookup every frame, but you get the idea.

Also be sure to check out a popular engine like Flixel to see a simple method for handling animation.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Re: How would you do player animations?
« Reply #4 on: January 06, 2014, 06:57:49 am »
I looked at the way Unity does things, and it looks like they have an encapsulation of Animation, and then you can add different animations based on the "state" you're in for walking forwards and backwards and any other actions. Thanks for helping!

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: How would you do player animations?
« Reply #5 on: January 06, 2014, 07:04:11 am »
No probs. The state machine is a popular method for handling animations and AI state, but it might be overkill unless you're building a flexible game engine. For example, you could just have a isWalking flag in your character that tracks that state, and then apply the appropriate transitions when your character moves. In any case, good luck!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: How would you do player animations?
« Reply #6 on: January 06, 2014, 08:49:13 pm »
I have developed the Thor.Animation module with points such as genericity and flexibility in mind. It can still be extended, but the interface already allows you to completely customize animations.

If you're interested, you could have a look at the tutorial and the API documentation :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: