SFML community forums

Help => General => Topic started by: lvenonl on March 02, 2015, 11:18:53 pm

Title: Usage of GetLocalBounds()
Post by: lvenonl 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   :-[
Title: Re: Usage of GetLocalBounds()
Post by: SeriousITGuy 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().
Title: Re: Usage of GetLocalBounds()
Post by: lvenonl 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.
Title: Re: Usage of GetLocalBounds()
Post by: Laurent 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).