1
Graphics / Re: [SOLVED] SFML DIY RectangleShape Collision detection only partially working
« on: Today at 10:40:37 pm »
To check if two (non-rotated) rectangles are intersecting, you can use Rect's intersects function (SFML 2 only; for SFML 3, you'll need the findIntersection function).
For example:
It's a pretty simple function and you can do something similar yourself by comparing the edges: the right side of the leftmost rectangle must be past (more to the right than) the left side of the rightmost rectangle and/or the bottom side of the topmost rectangle must be past (lower than) the top side of the bottommost rectangle.
If you'd like to use rotated rectangles, there is a short simple function that can do that for you too:
https://github.com/SFML/SFML/wiki/Source%3A-Rectangular-Boundary-Collision
Obviously, it works on non-rotated rectangles too!
For example:
const sf::Vector2f rect1TopLeft{ 100.f, 100.f };
const sf::Vector2f rect1Size{ 200, 200.f };
const sf::Vector2f rect2TopLeft{ 200.f, 200.f };
const sf::Vector2f rect2Size{ 200, 200.f };
const sf::FloatRect rect1{ rect1TopLeft, rect1Size };
const sf::FloatRect rect2{ rect2TopLeft, rect2Size };
bool isRect1CollidingWithRect2{ rect1.intersects(rect2) };
const sf::Vector2f rect1Size{ 200, 200.f };
const sf::Vector2f rect2TopLeft{ 200.f, 200.f };
const sf::Vector2f rect2Size{ 200, 200.f };
const sf::FloatRect rect1{ rect1TopLeft, rect1Size };
const sf::FloatRect rect2{ rect2TopLeft, rect2Size };
bool isRect1CollidingWithRect2{ rect1.intersects(rect2) };
It's a pretty simple function and you can do something similar yourself by comparing the edges: the right side of the leftmost rectangle must be past (more to the right than) the left side of the rightmost rectangle and/or the bottom side of the topmost rectangle must be past (lower than) the top side of the bottommost rectangle.
If you'd like to use rotated rectangles, there is a short simple function that can do that for you too:
https://github.com/SFML/SFML/wiki/Source%3A-Rectangular-Boundary-Collision
Obviously, it works on non-rotated rectangles too!