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

Author Topic: Differently sized sf::Texture's causing sprite to move  (Read 1348 times)

0 Members and 1 Guest are viewing this topic.

programsfml

  • Newbie
  • *
  • Posts: 3
    • View Profile
Differently sized sf::Texture's causing sprite to move
« on: May 18, 2018, 12:04:02 am »
Hello. I have a Sprite that is made up of ~20 different .png's that my Sprite cycles through to create a 'running' animation. My issue is that a couple of the images have a bigger height then the rest of them. This causes the Sprite to move down vertically an inch or two. I'm guessing this is because a Sprite's origin is always relative to it's top left position, thus causing the sprite to move down because the top left remains in the same spot.

I have sorta fixed this by creating a variable called count that keeps track of what frame of animation the Sprite currently holds, and if it is on one of the bigger images, I manually bump the sprite back up to make it look like it never bounced down. I feel like this is really hackish and that there is a much more elegant way to fix this. Any thoughts? The only reason I don't want to keep this way of fixing it is because I have a bunch of other player sprites I want to use in my game and they all will have this problem and manually bumping all of the sprites up seems tedious and unnecessary.

I've attached a small clip showing what happens with my sprite.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Differently sized sf::Texture's causing sprite to move
« Reply #1 on: May 18, 2018, 12:50:29 am »
Adjust the size in the texture so they all have the same rectangle. Alternatively you will have to use a sort of additional file that describes how each frame needs to be adjusted.

I hope you're using one texture with all the images on it and use the texture rect to extract the wanted section.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

programsfml

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Differently sized sf::Texture's causing sprite to move
« Reply #2 on: May 18, 2018, 02:04:29 am »
Quote
I hope you're using one texture with all the images on it and use the texture rect to extract the wanted section.

I will admit I was originally loading in all .PNG's into a vector of sf::Texture, which I know now was not a good idea. I compiled all the pictures into 1 png in photoshop and then changed the dimensions like you said and it worked perfectly! I'm ashamed I didn't think of that earlier. Thank you though! This is a much nicer solution :)