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

Author Topic: Simple method to rotate sprite about it's centre point  (Read 8628 times)

0 Members and 1 Guest are viewing this topic.

dk123

  • Newbie
  • *
  • Posts: 49
    • View Profile
Simple method to rotate sprite about it's centre point
« on: June 20, 2014, 06:43:10 am »
Hi, I was wondering if there was a simple way to rotate a sprite around it's centre point.

The rotate() function rotates about the sprite's top left corner,
which means I need to change the origin to the sprite's centre point.
However changing the origin then requires readjusting the position, and the scale.

Is there a simple way of going about this, or is this the only way?

I personally feel that rotation about the centre point is more instinctive and widely used than rotation about the top left corner (same with scaling). It would be great to have a function that easily gets this done if currently not implemented.

--update--

On second thought, perhaps a function on the lines of .setOriginAndReadjust() might be nice; where it not only changes the origin but also readjusts internal position, etc. to maintain the same visual appearance.

« Last Edit: June 20, 2014, 07:14:49 am by dk123 »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #1 on: June 20, 2014, 07:47:05 am »
On second thought, perhaps a function on the lines of .setOriginAndReadjust() might be nice; where it not only changes the origin but also readjusts internal position, etc. to maintain the same visual appearance.


I think this function does what you want:

void setOriginAndReadjust(sf::Transformable &object, const sf::Vector2f &newOrigin)
{
        auto offset = newOrigin - object.getOrigin();
        object.setOrigin(newOrigin);
        object.move(offset);
}
 



AlexAUT
« Last Edit: June 22, 2014, 10:27:17 am by AlexAUT »

dk123

  • Newbie
  • *
  • Posts: 49
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #2 on: June 20, 2014, 08:04:39 am »
Thanks AlexAUT!

Is there a reason for the offset.x * 2.f though? It seems to put my sprite slightly right than before.

object.move( { offset.x, offset.y } )

in comparison seems to work fine visually - although I'm not sure if this is technically correct.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #3 on: June 20, 2014, 08:17:16 am »
Is there a reason for the offset.x * 2.f though? It seems to put my sprite slightly right than before.

Yeah the 2.f isn't correct sry  ;)

btw you should write then:

object.move(offset)



AlexAUT

dk123

  • Newbie
  • *
  • Posts: 49
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #4 on: June 20, 2014, 08:34:25 am »
Thanks for all the help AlexAUT!

I really do feel though that this should be a default function in SFML  :)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #5 on: June 20, 2014, 08:38:43 am »
AlexAUT
I really do feel though that this should be a default function in SFML  :)

I don't think so, because basically when you create the sprite/rect/circle/whatever with the origin already centered you won't have this "problem" with the offset.



AlexAUT


dk123

  • Newbie
  • *
  • Posts: 49
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #6 on: June 20, 2014, 11:59:19 am »
AlexAUT
I really do feel though that this should be a default function in SFML  :)

I don't think so, because basically when you create the sprite/rect/circle/whatever with the origin already centered you won't have this "problem" with the offset.


Right, but I think in cases when you do change the origin after creation (for certain visual effects, etc.), you many a time probably don't want the visual appearance of what's already on screen to change.

The intention is more along the lines of 'change the origin to this, and have future .setPosition(), .setScale()' applied based on this', than 'change the origin to this, and have everything I've done up until now changed'.

I just think this is an extremely convenient function to have and adding it into SFML as a default function would definitely make this more accessible.

« Last Edit: June 20, 2014, 12:32:31 pm by dk123 »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Simple method to rotate sprite about it's centre point
« Reply #7 on: June 20, 2014, 07:33:14 pm »
I can't help thinking "what's wrong with changing the origin to what makes sense for a given transformation, do the transformation, draw the result, then change the origin back"?
No need to make a simple thing complicated; just change, transform, draw, change back  ;)

dk123

  • Newbie
  • *
  • Posts: 49
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #8 on: June 21, 2014, 03:33:45 am »
I can't help thinking "what's wrong with changing the origin to what makes sense for a given transformation, do the transformation, draw the result, then change the origin back"?
No need to make a simple thing complicated; just change, transform, draw, change back  ;)

Definitely, and a function along the lines of setOriginAndReadjust that maintains the visual appearance of the entity on screen despite the origin change would simply do just that without the manual readjustment fuss.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Simple method to rotate sprite about it's centre point
« Reply #9 on: June 21, 2014, 03:00:51 pm »
I just think this is an extremely convenient function to have and adding it into SFML as a default function would definitely make this more accessible.

No, if something like setSize(...) was removed in favor of directly using setScale(...) then this function does not belong at all.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

dk123

  • Newbie
  • *
  • Posts: 49
    • View Profile
Re: Simple method to rotate sprite about it's centre point
« Reply #10 on: June 22, 2014, 04:03:22 am »
I just think this is an extremely convenient function to have and adding it into SFML as a default function would definitely make this more accessible.

No, if something like setSize(...) was removed in favor of directly using setScale(...) then this function does not belong at all.

Could you elaborate? I don't quite understand what you mean.


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Simple method to rotate sprite about it's centre point
« Reply #11 on: June 22, 2014, 10:10:32 am »
What he meant is that there was once a sf::Sprite::setSize() method for convenience, but it was removed. The idea was to keep the API as minimal as possible and the different transform-related methods orthogonal, in order to make usage more intuitive. I agree that the method you proposed is a bit too specific. People who want to rotate around the center initially set the origin accordingly, there is no need to keep it in the left-upper corner (and for those who absolutely need it, it's two lines of code).

And instead of object.move( { offset.x, offset.y } ), you can use object.move(offset).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: