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

Author Topic: Rotating sprite around its center AND resizing it  (Read 7702 times)

0 Members and 1 Guest are viewing this topic.

lowps

  • Newbie
  • *
  • Posts: 2
    • View Profile
Rotating sprite around its center AND resizing it
« on: April 07, 2012, 09:42:45 am »
I am using SFML 1.6 and am trying to rotate a resized sprite about its center. Rotating about a sprite's center seems to work fine if the sprite is not resized, but when resized, SetCenter() does not seem to place the sprite's center at the proper place, and the sprite ends up rotating about some other point.
For example, the following code is in my main game loop, as a test:
Code: [Select]
sf::Sprite test( ImageCache::GetImage("simple_bit.png") );
  float rotation = 0;
  test.SetPosition( 300, 300 );
  //test.Resize( 50, 50 );                                                                                                                                                                                                         
  test.SetCenter( test.GetSize() / 2.f );
  test.SetRotation( rotation++ );
  screen.Draw( test );

This will work fine, and the sprite will appear to rotate about its center. Yet if the call to Resize() is uncommented, the sprite will seem to rotate around a point that is slightly off its center. The greater the difference between the resized sprite and the original image, the greater the distance between the desired axis and the axis I get.

The only reason I could think this would happen is if SetCenter() sets the center of the sprite in relation to the size of the Image, not the sprite. Some tests I have done seem to indicate this. If this is actually the issue, then this needs to be made more clear in the forums and tutorials (unless I'm just blind).

Am I missing anything?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rotating sprite around its center AND resizing it
« Reply #1 on: April 07, 2012, 10:28:04 am »
The center is in local coordinates, ie. without any transformation applied. So you must not take the scale in account when you set it.

I thought it was explained in the doc; maybe it's just in the 2.0 doc.
Laurent Gomila - SFML developer

lowps

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Rotating sprite around its center AND resizing it
« Reply #2 on: April 07, 2012, 09:46:46 pm »
Thanks -- I realize that makes more sense now. I've looked around the forum and some other places and most people seem to use (or imply they use) sprite.SetCenter( sprite.GetSize() / 2.f ), which would work only if the sprite has not been resized already. Maybe it's better to use:
Code: [Select]
if( sprite.GetImage() )
   sprite.SetCenter( sprite.GetImage()->GetWidth / 2, sprite.GetImage()->GetHeight / 2 )
because it will work regardless of when the image was resized?

Thanks again!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rotating sprite around its center AND resizing it
« Reply #3 on: April 08, 2012, 08:38:14 am »
Yep, you're right.
Laurent Gomila - SFML developer

 

anything