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

Author Topic: Usage of GetLocalBounds()  (Read 5900 times)

0 Members and 1 Guest are viewing this topic.

lvenonl

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Usage of GetLocalBounds()
« on: March 02, 2015, 11:18:53 pm »
Hi there,

I'm new to SFML library thus I've been studying the Sprite class and I've got a doubt about the method GetLocalBounds(). What is the actual usage of this function?

I do understand that it returns a Rect with the dimensions of the sprite inside it's coordinate system, I just couldn't imagine any actual usage of this method. Why would I want to get the local bounds of the sprite? I know it's a silly question, I just couldn't sleep with it in my mind   :-[

SeriousITGuy

  • Full Member
  • ***
  • Posts: 123
  • Still learning...
    • View Profile
Re: Usage of GetLocalBounds()
« Reply #1 on: March 03, 2015, 11:48:22 am »
Typical use case for this is to set the origin of the sprite to its center point.
Something like this:
auto bounds = mySprite.getLocalBounds();
mySprite.setOrigin( bounds.width / 2.f, bounds.heigth / 2.f );
 

Other use cases is for collision detection between sprites, you need to know about the geometry of the sprite, and therefor can use getLocalBounds().

lvenonl

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Usage of GetLocalBounds()
« Reply #2 on: March 03, 2015, 04:42:47 pm »
But in both cases GetTextureRect also serves well:

auto rect = mySprite.GetTextureRect();
mySprite.SetOrigin(rect.width / 2.f, bounds.height / 2.f);

The only difference between these two functions is that GetTextureRect returns an x and y relative to the Texture's coordinates (useful for sprite animation). My curiosity regarding the usage of this method is specifically the returning values of the coordinates of the Sprite.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Usage of GetLocalBounds()
« Reply #3 on: March 03, 2015, 06:55:11 pm »
Quote
But in both cases GetTextureRect also serves well:
getLocalBounds() is more generic, it works with all SFML entities. And it makes exactly what its name says (you could imagine new features in sf::Sprite that would make the texture rect no longer match the sprite's geometry).
Laurent Gomila - SFML developer

 

anything