SFML community forums
Help => Graphics => Topic started by: bglaze 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.
-
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.
-
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:
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:
if(sprite1.Rect.Intersects(sprite2.Rect))
std::cout << "HIT" << std::endl;
-
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:
Rect = sf::Rect<int>(this->GetPosition().x,
this->GetPosition().y,
this->GetSize().x,
this->GetSize().y);