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

Author Topic: Sprite::getGlobalBounds() returning incorrect values  (Read 3562 times)

0 Members and 1 Guest are viewing this topic.

CasualKyle

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Sprite::getGlobalBounds() returning incorrect values
« on: August 09, 2016, 07:46:00 am »
sf::Sprite s(playerSpriteSheet, sf::IntRect(1, 1, 7, 6));
s.setScale(9.0f, 9.0f);
s.setOrigin(s.getLocalBounds().width / 2, s.getLocalBounds().height / 2);
s.setPosition(400.0f, 50.0f);
s.setRotation(180.0f);

I'm initializing a sprite with the above code. If I was to then find the value for the top of the global bounds I should get 23 and here's how: Since the scale of the sprite is 9 the height would be 6 x 9 or 54. The origin of the sprite is in the very center so the center of the sprite globally is 400, 50. To get the top of the global bounds you'd subtract half of the height from 50: 50 - 54 / 2 = 23. So when I call s.getGlobalBounds().top I should get 23 but I actually get 22.9999962 and when I call s.getGlobalBounds().height I should get 54 but instead I get 54.0000038.

I'm aware that since the sprite is rotated 180 degrees some floating point arithmetic is involved and thus gives me incorrect numbers because of the inherit problems with floating point arithmetic. But what if I was to tell you that the incorrect value are only present when the position of the sprite on the y axis is less than 92? When I set the y position of the sprite to 92 the correct values are returned, but when I set the y position of the sprite to 91, I then get values that are slightly off like mentioned above.

It's so strange to me that the values are only incorrect when the sprite's y position is less than 92. Why is this the case and is there anything I can do to ensure I'll always get the correct values regardless of the sprites position?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite::getGlobalBounds() returning incorrect values
« Reply #1 on: August 09, 2016, 07:58:37 am »
To me there's really nothing wrong. If you feel like this is too strange, feel free to have a look at the source code ;)
Laurent Gomila - SFML developer

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Sprite::getGlobalBounds() returning incorrect values
« Reply #2 on: August 09, 2016, 08:14:46 pm »
So when I call s.getGlobalBounds().top I should get 23 but I actually get 22.9999962 and when I call s.getGlobalBounds().height I should get 54 but instead I get 54.0000038.

Those value that you are asking for are very approximate.

23 -> 22.9999962 should have no visible difference unless you transform it to int type using C-Style cast or static_cast. If this is your problem try using std::round.

I would like a spanish/latin community...
Problems building for Android? Look here

CasualKyle

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Sprite::getGlobalBounds() returning incorrect values
« Reply #3 on: August 11, 2016, 07:01:58 am »
Thanks for your help, I guess nothing is really wrong so I'll round the values.
« Last Edit: August 11, 2016, 08:02:57 am by CasualKyle »