SFML community forums

Help => General => Topic started by: metulburr on November 12, 2013, 10:48:48 pm

Title: checking for collision against an object
Post by: metulburr on November 12, 2013, 10:48:48 pm
I come from python/pygame where the methods are made for you. However looking through sfml, i dont see such methods as in pygame. such as rect.collidepoint() for mouse or rect.colliderect() for collision test against another rect. Do you have to make these yourself in sfml?

If so i am not quite sure on how to make these functions.
I set up the base classes, i am just not sure on how to check if the mouse is inside the rect or not?

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

class Rect{
    public:
        sf::RectangleShape rect;
       
        Rect(){
            rect.setSize(sf::Vector2f(100,100));
            rect.setPosition(sf::Vector2f(10,10));
            rect.setFillColor(sf::Color::Red);
        }
};

class Control{
    public:
        sf::RenderWindow window;
        Rect obj;
       
        Control(){
            window.create(sf::VideoMode(600,400), "Title");
            window.setKeyRepeatEnabled(false);
        }
       
        void update(sf::Event event){
            window.clear(sf::Color::Black);
            window.draw(obj.rect);
            window.display();
        }
       
        void event_handler(sf::Event event){
            while(window.pollEvent(event)){
                if (event.type == sf::Event::Closed){
                    window.close();
                }
            }
        }
       
        void run(){
            while (window.isOpen()){
                sf::Event event;
                event_handler(event);
                update(event);
            }
        }
};

int main(){
    Control app;
    app.run();
}

 
Title: Re: checking for collision against an object
Post by: G. on November 12, 2013, 10:52:33 pm
Get the bounding box of your shapes
http://www.sfml-dev.org/documentation/2.1/classsf_1_1Shape.php#a5257341fe832884dbba6b9dc855e33cc
Check if they intersect
http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011
Check if a point (the mouse) is inside
http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php#a24163acdb9b2987c0ea55c201e270d41
And if you use a view, you'll probably want to take a look at the view tutorial.

Yes, SFML has documentation.
Title: Re: checking for collision against an object
Post by: Nexus on November 12, 2013, 10:56:15 pm
Yes, you have to implement collision detection and response yourself. There is only the sf::Rect class template with a few methods.

Please search the forum for collision, your question has already been asked dozens of times.
Title: Re: checking for collision against an object
Post by: metulburr on November 12, 2013, 11:03:34 pm
i have tried searchig the forums for such, however in the forums if is search i get a database error:
Please try again. If you come back to this error screen, report the error to an administrator.

and googling sfml collision does not return much. I think i found one thread which was unhelpful this method.
Title: Re: checking for collision against an object
Post by: Nexus on November 12, 2013, 11:22:54 pm
Yes, the SFML forum search is of limited utility (when restricting the forums to search, it usually works).

You can also try to search with the site: filter on DuckDuckGo, Google or other search engines, in order to get only results from this forum.