C++ sfml how to rotate a character from its center

1.3k views Asked by At

I want to use the function sprite.rotate() and sprite.setrotation() but sfml takes into account the top-right point of the sprite. How to set this point to sprite center?

1

There are 1 answers

0
Mems On

If you want to set the rotation center you have to use sprite.setOrigin() function which sets the center for position and rotation.

For example:

sf::Texture texture;
if (!texture.loadFromFile("texture.png"))
    exit(-1);

sf::Sprite sprite;
sprite.setOrigin((sf::Vector2f)texture.getSize() / 2.f);

Or if you are changing the texture rect of the sprite:

sprite.setOrigin(sf::Vector2f(sprite.getLocalBounds().width, sprite.getLocalBounds().height) / 2.f);