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

Author Topic: Bounding Box Help :/  (Read 6032 times)

0 Members and 1 Guest are viewing this topic.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Bounding Box Help :/
« 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
}
 

MrMuffins

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Bounding Box Help :/
« Reply #1 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.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Bounding Box Help :/
« Reply #2 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

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Bounding Box Help :/
« Reply #3 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();
                }




 
« Last Edit: April 25, 2015, 05:31:34 pm by oOhttpOo »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Bounding Box Help :/
« Reply #4 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.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Bounding Box Help :/
« Reply #5 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.

MrMuffins

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Bounding Box Help :/
« Reply #6 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.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Bounding Box Help :/
« Reply #7 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.  :-[
« Last Edit: April 25, 2015, 11:47:16 pm by oOhttpOo »