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

Author Topic: Is it possible to rotate a FloatRect to detect collisions?  (Read 1828 times)

0 Members and 1 Guest are viewing this topic.

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Is it possible to rotate a FloatRect to detect collisions?
« on: January 15, 2012, 07:15:14 pm »
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.

Code: [Select]
#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;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Is it possible to rotate a FloatRect to detect collisions?
« Reply #1 on: January 16, 2012, 05:27:01 pm »
The Separating Axis Theorem might help you, www.metanetsoftware.com contains some tutorials.

You could also use something existing like Boost.Geometry, but I have no experience with it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: