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

Author Topic: Collision with Rotating Objects  (Read 2014 times)

0 Members and 1 Guest are viewing this topic.

randy808

  • Newbie
  • *
  • Posts: 6
    • View Profile
Collision with Rotating Objects
« on: February 18, 2014, 11:58:54 pm »
Does getGlobalBounds really work on rotating objects? I'm trying to detect a collision and currently the collision is setting off even when it's not intersecting the intended object. I'm on Ubuntu and believe I am running SFML 2.0. Below is a rough, off hand copy of the part of code in question, any responses as to why it might not be working would be greatly appreciated :D.

Also, if the code below for some reason doesn't compile in your compiler, remember it was a quick snippet put together to help explain my problem and that the actual code in question is error free so u can pretty much ignore any insignificant syntax errors lol.


#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
sf::RenderWindow window(sf::VideoMode(800,600),"");
bool play = true;

sf::RectangleShape rocks;
rocks.setSize(sf::Vector2f(800,50));
rocks.setFillColor(sf::Color(169,169,169));
rocks.setPosition(400,200);

sf::RectangleShape rect;
rocks.setSize(sf::Vector2f(50,50));
rocks.setFillColor(sf::Color::Black);
rocks.setPosition(350,250);

while(play == true){

rocks.rotate(1);

if(rocks.getGlobalBounds().intersects(rect.getGlobalBounds()) == true){
static int g = 0;
if (g <=5){
rect.setFillColor(sf::Color::Yellow);
g++;
}

if (g >5){
rect.setFillColor(sf::Color::Blue);
g++;
if (g >= 11) g = 0;
        }

window.draw(rect);
window.draw(rocks);

window.display();

}

}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Collision with Rotating Objects
« Reply #1 on: February 19, 2014, 12:38:52 am »
The bounding rectangle of a shape is axis aligned, like the red bounding rectangle of this rotated cat sprite on the upper row.



The one on the lower row is an oriented bounding box and if you want it you'll have to compute it yourself. :p

randy808

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Collision with Rotating Objects
« Reply #2 on: February 19, 2014, 04:58:35 am »
Yeah, I thought I would end up having to do it myself *sigh*... thanks for the response though, it's greatly appreciated ;D

 

anything