SFML community forums
Help => Graphics => Topic started by: svento92 on December 16, 2019, 09:07:47 am
-
I'm trying to write a generic function for bringing an object on the center of the screen. Works great for sf::RectangleShape , sf::Text. However when i try to do it on a sf::Sprite the sprite seems to position itself closer to the bottom of the screen rather than the middle. The only differenace is that i downscale the sf::Sprite with setScale(sf::Vector2f(0.20,0.20)). So i'm wondering does this affect the setPoistion() and setOrigin of a sf::Sprite? Or is there some other reason the sf::Sprite seems to be going towards the bottom of the screen rather than center.
The code to put a sf::sprite at the center of screen is as seen below.
sf::Vector2u windowCoords = window.getSize();
float width = windowCoords.x / 2.0f;
float height = windowCoords.y / 2.0f;
sprite.setPosition(width,height);
sf::FloatRect shapeRectangle = sprite.getLocalBounds();
sprite.setOrigin(shapeRectangle.left + shapeRectangle.width/2.0f,
shapeRectangle.top + shapeRectangle.height/2.0f);
-
The issue seems to have been that the top of the sf::Sprite was set to the center of the screen. So if i subtracted the sprites height / 2 from the height that i passed into setPosition() and setOrigin() then the actual center of sf::Sprite was in the center of the screen rather than the top of the sf::Sprite
-
First, an answer to your title: no. ;D
The origin is the part of the sprite in local co-ordinates to be considered the position of the sprite.
The position is location in the window (view) that the origin is placed.
Any transformations are applied around the origin so rotations rotate around the origin (and that part - the position/origin - doesn't move) and scales are scaled from the origin so it expands from or contracts to the origin (so the position/origin again doesn't move).
Does the code that you posted not work or is it different code that doesn't work?
(I tested your code and even added a scaling line of code and it still seems to work okay)