Hay, I've been playing with FloatRect and some collisions and it's going well. This is a simple sample I have made which draws 2 boxes, one of which is movable. It logs whether or not they are colliding. How would I edit this so that I can detect if a rotated Rect is colliding? I see that FloatRect cannot be rotated.
#include <SFML/Graphics.hpp>
int main () {
// Setup a window and limit the framerate
sf::RenderWindow window(sf::VideoMode(640, 480, 32), "SFML Graphics");
window.SetFramerateLimit(60);
// Set up a Vector2 to hold the colliders size (this will also be the box size)
sf::Vector2f colliderSize(50.0f, 50.0f);
// Set up a Vector2 to hold boxA and boxB's positions
sf::Vector2f boxAPosition(0.0f, 0.0f);
sf::Vector2f boxBPosition(200.0f, 200.0f);
// Setup box A
sf::RectangleShape boxA;
boxA.SetSize(colliderSize);
boxA.SetPosition(boxAPosition);
boxA.SetFillColor(sf::Color::Red);
// Setup box B
sf::RectangleShape boxB;
boxB.SetSize(colliderSize);
boxB.SetPosition(boxBPosition);
boxB.SetFillColor(sf::Color::Green);
// Box B will never move in this demo, so we'll set the IntRect outside the game loop
sf::FloatRect rectB(boxBPosition, colliderSize);
// Main Loop
while (window.IsOpened()){
sf::Event event;
while (window.PollEvent(event)){
if (event.Type == sf::Event::Closed){
window.Close();
}
}
// Setup buttons
bool leftBtn = sf::Keyboard::IsKeyPressed(sf::Keyboard::Left);
bool rightBtn = sf::Keyboard::IsKeyPressed(sf::Keyboard::Right);
bool downBtn = sf::Keyboard::IsKeyPressed(sf::Keyboard::Down);
bool upBtn = sf::Keyboard::IsKeyPressed(sf::Keyboard::Up);
// Setup deltaTime and the speed in wich the boxes will move
float deltaTime = window.GetFrameTime();
float moveSpeed = 0.1f;
// Movement
if (leftBtn) {
boxA.Move(-moveSpeed * deltaTime, 0);
}
if (rightBtn) {
boxA.Move(moveSpeed * deltaTime, 0);
}
if (upBtn) {
boxA.Move(0, -moveSpeed * deltaTime);
}
if (downBtn) {
boxA.Move(0, moveSpeed * deltaTime);
}
// rectA will need to be updated everyframe
sf::FloatRect rectA( sf::Vector2f(boxA.GetPosition().x, boxA.GetPosition().y), colliderSize);
window.Clear();
window.Draw(boxA);
window.Draw(boxB);
window.Display();
if (rectA.Intersects(rectB)) {
printf("intersecting\n");
}else{
printf("not intersecting\n");
}
};
return EXIT_SUCCESS;
}