#include#include int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Rect left"); window.setFramerateLimit(60); sf::RectangleShape rect(sf::Vector2f(100, 50)); rect.setFillColor(sf::Color::Red); rect.setPosition(50, 100); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } std::cout << "Left coordinate: " << rect.getGlobalBounds().left << std::endl; window.clear(); window.draw(rect); window.display(); } return 0; }
#includeIn this example, the Rect left function is used along with other functions from the SFML library to create a collision detection function between two rectangle objects. The function checks if the left edge of one rectangle is overlapping with the right edge of the other rectangle to determine if a collision has occurred. Package/library: SFML#include bool isIntersecting(sf::RectangleShape shape1, sf::RectangleShape shape2) { return (shape1.getGlobalBounds().left + shape1.getGlobalBounds().width >= shape2.getGlobalBounds().left && shape2.getGlobalBounds().left + shape2.getGlobalBounds().width >= shape1.getGlobalBounds().left && shape1.getGlobalBounds().top + shape1.getGlobalBounds().height >= shape2.getGlobalBounds().top && shape2.getGlobalBounds().top + shape2.getGlobalBounds().height >= shape1.getGlobalBounds().top); } int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Rect left"); window.setFramerateLimit(60); sf::RectangleShape rect1(sf::Vector2f(100, 50)); rect1.setFillColor(sf::Color::Red); rect1.setPosition(50, 100); sf::RectangleShape rect2(sf::Vector2f(100, 50)); rect2.setFillColor(sf::Color::Green); rect2.setPosition(200, 150); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } if (isIntersecting(rect1, rect2)) std::cout << "Collision detected" << std::endl; window.clear(); window.draw(rect1); window.draw(rect2); window.display(); } return 0; }