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

Author Topic: SFML game development book,Projectile::getBoundingRec()  (Read 1377 times)

0 Members and 1 Guest are viewing this topic.

shayan-sa

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
SFML game development book,Projectile::getBoundingRec()
« 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?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML game development book,Projectile::getBoundingRec()
« Reply #1 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

shayan-sa

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: SFML game development book,Projectile::getBoundingRec()
« Reply #2 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.

« Last Edit: August 06, 2016, 02:42:59 pm by shayan-sa »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML game development book,Projectile::getBoundingRec()
« Reply #3 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 library. For example, using these functions: https://github.com/Hapaxia/Plinth/blob/3185999f57b5e74bbef084ebb301580fe8a4204d/Plinth/Sfml/Generic.hpp#L78-L80
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*