SFML community forums

Help => General => Topic started by: shayan-sa on August 05, 2016, 09:26:40 pm

Title: SFML game development book,Projectile::getBoundingRec()
Post by: shayan-sa on August 05, 2016, 09:26:40 pm
I am not sure, if this is not a silly question, but i can't understand.
in SFML book, we reach this code:

sf::FloatRect Projectile::getBoundingRect()const
{
        return getWorldTransform().transformRect(mSprite.getLocalBounds);
}
 

my question is why we should not using mSprite.getGlobalBounds() . I am not still able to understand SFML well, but as document says, it will use transforms too. is there any difference?
and as another question, for example, when we rotate a shape, by PI/2 radians, it means the bounding rectangle is also rotated by PI/2 radians?
Title: Re: SFML game development book,Projectile::getBoundingRec()
Post by: Hapax on August 06, 2016, 12:48:33 pm
Without looking at the code, I would guess that this gives a "local" bounding rectangle within the "world" co-ordinate system.

Bounding rectangles are always axis-aligned so the rectangle is not rotated; the rectangle is instead expanded to contain what would have been the rotated rectangle.
Title: Re: SFML game development book,Projectile::getBoundingRec()
Post by: shayan-sa on August 06, 2016, 02:40:13 pm
So what is globalBounds??? I am pretty confused now! when we use getWorldTransform, we want to get to the real transform not relative to anything? isn't this what globalBounds return?
and another question, that won't be nice, if for example i have a bullet, that is a little large, and has 45 degrees rotation, then the rectangle would be big, and it is possible that bullet collides with an enemy while it is only close to it! how can i rotate bounding rectangle?
Without looking at the code, I would guess that this gives a "local" bounding rectangle within the "world" co-ordinate system.

Bounding rectangles are always axis-aligned so the rectangle is not rotated; the rectangle is instead expanded to contain what would have been the rotated rectangle.

Title: Re: SFML game development book,Projectile::getBoundingRec()
Post by: Hapax on August 06, 2016, 03:45:45 pm
Global bounds is the local bounds rectangle transformed by the object's transformations (position, scale, rotation etc.). It looks like "world transform" here is a custom sf::Transform that is applied to multiple objects.

Many examples of how to detect collisions with rotated rectangles can be found by googling.
I implemented a way to do this in my Plinth (https://github.com/Hapaxia/Plinth/wiki) library. For example, using these functions: https://github.com/Hapaxia/Plinth/blob/3185999f57b5e74bbef084ebb301580fe8a4204d/Plinth/Sfml/Generic.hpp#L78-L80