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

Author Topic: Does setScale() affect setPostion() and setOrigin()?  (Read 1684 times)

0 Members and 1 Guest are viewing this topic.

svento92

  • Newbie
  • *
  • Posts: 6
    • View Profile
Does setScale() affect setPostion() and setOrigin()?
« 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.

Quote
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);




svento92

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Does setScale() affect setPostion() and setOrigin()?
« Reply #1 on: December 16, 2019, 11:54:39 am »
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

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Does setScale() affect setPostion() and setOrigin()?
« Reply #2 on: December 18, 2019, 08:52:44 pm »
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)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*