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

Author Topic: Help with sprite animation  (Read 4067 times)

0 Members and 1 Guest are viewing this topic.

zyphur

  • Newbie
  • *
  • Posts: 2
    • View Profile
Help with sprite animation
« on: February 26, 2017, 09:44:26 pm »
So heres my issue im having. Say i have 60 images of a swordsman that im animating. But the size of the sprite shifts around. I want it so that his feet are planted at an origin but currently the guy moves around like crazy in the wrong way. So i end up having to frame by frame adjust the position so his feet always match. Because the sprite is drawn from the top left. I need a way to make it so the sprite is drawn from the bottom right so that his feet are always constantly planted on the ground. If thats not possible than does somebody have a suggestion for avoiding micropositioning over and over because its making the process take 10 x longer than it should.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Help with sprite animation
« Reply #1 on: February 26, 2017, 10:15:01 pm »
It's not exactly clear from your post, but assuming the frames are all the same size you can change the origin with setOrigin()

zyphur

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Help with sprite animation
« Reply #2 on: February 26, 2017, 10:18:53 pm »
They are not the same size.

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Help with sprite animation
« Reply #3 on: February 28, 2017, 11:47:24 am »
Something like this should do it.
   // After you set the animation on your sprite:
   sf::IntRect rectAnimFrame = sprite.getTextureRect();

   // to draw sprite from bottom right:
   sprite.setOrigin(sf::Vector2f(rectAnimFrame.width, rectAnimFrame.height));
   // to draw sprite from bottom center:
   sprite.setOrigin(sf::Vector2f(rectAnimFrame.width / 2.f, rectAnimFrame.height));
 
« Last Edit: February 28, 2017, 12:16:33 pm by Jabberwocky »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Help with sprite animation
« Reply #4 on: February 28, 2017, 11:54:51 am »
If you don't want to adjust the sprite sheet to get a consistent size, then another option is to write in a separate file, all the pixel positions of for example the feet and the respective frame's size. The you can read that file and adjust the sprites texture rect and origin accordingly.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with sprite animation
« Reply #5 on: March 01, 2017, 09:04:11 pm »
You may want to consider using Selba Ward's Gallery Sprite. It was created for easing this very problem.

It stores "exhibits", which are just texture rects with an anchor (separate origin for that texture rect), and they can be switched by number/index.
All you would need to do is add each texture rect and its corresponding anchor (the point that should be the same for each "frame") and then you can cycle through them.

You should also be able to do the stuff you would normally do with SFML's standard sprite too!

All of the frame animation (indeed all of the sprites at all) in this example video use Gallery Sprite:
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything