SFML community forums

Help => Graphics => Topic started by: oOhttpOo on April 22, 2015, 05:06:14 pm

Title: Bounding Box Help :/
Post by: oOhttpOo on April 22, 2015, 05:06:14 pm
Hello, I'm trying to make a simple game that needs to detect when one sprite touches another's bounding box, but I don't quite understand the code for this. Sorry if this has already been posted, but I could not find any help. The bit I don't understand is
// check collision with another box (like the bounding box of another entity)
sf::FloatRect otherBox = ...; //What goes here??
if (boundingBox.intersects(otherBox))
{
//collision
}
 
Title: Re: Bounding Box Help :/
Post by: MrMuffins on April 22, 2015, 07:41:12 pm
The sprite has an getGlobalBounds() function which returns a FloatRect.
http://www.sfml-dev.org/documentation/2.2/classsf_1_1Sprite.php#a203d2d8087bfdca2ebc3c0485cdb7409

Keep in mind it's the full bounding box of the sprite object, even if the entire sides of the sprite are empty or transparent.
Title: Re: Bounding Box Help :/
Post by: oOhttpOo on April 22, 2015, 08:27:34 pm
The sprite has an getGlobalBounds() function which returns a FloatRect.
http://www.sfml-dev.org/documentation/2.2/classsf_1_1Sprite.php#a203d2d8087bfdca2ebc3c0485cdb7409

Keep in mind it's the full bounding box of the sprite object, even if the entire sides of the sprite are empty or transparent.
k thanks
Title: Re: Bounding Box Help :/
Post by: oOhttpOo on April 25, 2015, 05:29:41 pm
I did that, but it seems to intersect all the time when it is no where near the sprite. I have this code:
sf::FloatRect helib = heli.getGlobalBounds();
sf::FloatRect obs1b = obs1.getGlobalBounds();
...
if (helb.intersects(obs1b))
                {
                        window.close();
                }




 
Title: Re: Bounding Box Help :/
Post by: Jesper Juhl on April 25, 2015, 08:16:46 pm
Use your debugger. Set a breakpoint and inspect the values of the rectangles you are comparing for intersection.
Title: Re: Bounding Box Help :/
Post by: oOhttpOo on April 25, 2015, 09:19:17 pm
hmm... This doesn't make any sense as far as I can see. Even when the two sprites are no where near each other, it still thinks the bounding boxes collide  :-\ And also they collide even when one isn't even drawn.
Title: Re: Bounding Box Help :/
Post by: MrMuffins on April 25, 2015, 10:32:56 pm
hmm... This doesn't make any sense as far as I can see. Even when the two sprites are no where near each other, it still thinks the bounding boxes collide  :-\ And also they collide even when one isn't even drawn.

Removing the draw call for a sprite isn't gonna stop its logic from being ran. It still exists, even if you can't visually see it.
Title: Re: Bounding Box Help :/
Post by: oOhttpOo on April 25, 2015, 11:41:48 pm
Nevermind I fixed it. In case anyone else had this issue, I forgot to put the .getGlobalBounds in the same loop.  :-[