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

Author Topic: Help with rotating a sprite  (Read 1470 times)

0 Members and 1 Guest are viewing this topic.

rcplusplus

  • Newbie
  • *
  • Posts: 15
    • View Profile
Help with rotating a sprite
« on: May 10, 2012, 11:20:47 pm »
I have a character sprite that I need to rotate to face up, left, right, and down. The way I'm currently rotating it is like this:

(SpriteX is a wrapper class I made around an sf::Sprite, and these are some rotation methods)

void SpriteX::rotate(float degrees)
{
   sprite.SetCenter(get_width()/2, get_height()/2);
   sprite.Rotate(degrees);
   sprite.SetCenter(0, 0);
}

void SpriteX::set_rotation(float degrees)
{
   sprite.SetCenter(get_width()/2, get_height()/2);
   sprite.SetRotation(degrees);
   sprite.SetCenter(0, 0);
}
 

The sprite still rotates around its top left corner, how do I make it rotate around it's center just for the scope of the rotation method? I still want to position the sprite based on its top left corner.

Will I just have to make 4 separate image files?

Any help is appreciated!
« Last Edit: May 11, 2012, 08:00:33 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help with rotating a sprite
« Reply #1 on: May 11, 2012, 08:02:41 am »
You can't do what you want (with SFML 1). Transformations are not applied immediately and combined with the current state, the SetPosition/SetRotation/... functions just set internal attributes that are combined when drawing the sprite, always in the same order.
Laurent Gomila - SFML developer

rcplusplus

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Help with rotating a sprite
« Reply #2 on: May 11, 2012, 08:32:18 pm »
Can it be done with SFML 2? And if so, how?

And the way I'm currently getting around the situation is by just center-positioning all the sprites internally, and using the wrapper class to make it behave like I'm positioning using the top left corner. Would you say this is bad design?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help with rotating a sprite
« Reply #3 on: May 11, 2012, 08:42:32 pm »
Quote
Can it be done with SFML 2? And if so, how?
Yes. In SFML 2, the behaviour of the setRotation/setPosition/... functions is the same, but you can ignore them and provide your own custom transform at draw time. There's a sf::Transform class that can combine translations/rotations/... in any order.

Quote
And the way I'm currently getting around the situation is by just center-positioning all the sprites internally, and using the wrapper class to make it behave like I'm positioning using the top left corner. Would you say this is bad design?
According to the API of SFML1, I think this is a pretty good design :)
Laurent Gomila - SFML developer

 

anything