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

Author Topic: sf::Rect::Intersects() for two moving rectangles?  (Read 2492 times)

0 Members and 1 Guest are viewing this topic.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
sf::Rect::Intersects() for two moving rectangles?
« on: October 30, 2011, 05:32:23 am »
Is it possible to use sf::Rect::Intersects() for two moving rectangles?

I notice that Intersects() takes a const Rect, so I am assuming that one of the Rects has to be permanently stationary like a platform or something.

I am trying to make collision detection for a laser and a moving asteroid, and I was hoping to use this function, but if this isn't possible I may have to make my own. In that case I would love some guidance (doesn't have to be direct code, just basic advice, though code is fine too... =) ) on creating my own Bounding Box Collision.

Anyway, thank you!!!!
Brock G.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
sf::Rect::Intersects() for two moving rectangles?
« Reply #1 on: October 30, 2011, 07:26:31 am »
All the const means is that the Rect::Intersects() function promises not to modify the rect you pass to it.

You can pass normal modifiable rects to it.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
sf::Rect::Intersects() for two moving rectangles?
« Reply #2 on: October 30, 2011, 01:54:00 pm »
Any recommendation for how to create a Rect for each of my two sprites that will change appropriately as they move? I currently have it in an Update() function that gets called with every frame. But for some reason Intersects() is returning true for a rectangle like 10 times the size of my actual Rect.

The code I am using to set each Sprite's Rect member every time it updates is this:
Code: [Select]

Rect = sf::Rect<int>(this->GetPosition().x,
                     this->GetPosition().y,
                     this->GetPosition().x + this->GetSize().x,
                     this->GetPosition().y + this->GetSize().y);


And then I am calling:
Code: [Select]

if(sprite1.Rect.Intersects(sprite2.Rect))
        std::cout << "HIT" << std::endl;

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
sf::Rect::Intersects() for two moving rectangles?
« Reply #3 on: October 30, 2011, 02:10:20 pm »
Well, I realized that I was creating my Rect's wrong, and that it doesn't go by Right and Bottom any more but by Width and Height, so I should have been using this code:

Code: [Select]

    Rect = sf::Rect<int>(this->GetPosition().x,
                         this->GetPosition().y,
                         this->GetSize().x,
                         this->GetSize().y);

 

anything