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

Author Topic: checking for collision against an object  (Read 2670 times)

0 Members and 1 Guest are viewing this topic.

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
checking for collision against an object
« 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();
}

 
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: checking for collision against an object
« Reply #1 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.
« Last Edit: November 12, 2013, 10:55:10 pm by G. »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: checking for collision against an object
« Reply #2 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: checking for collision against an object
« Reply #3 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.
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: checking for collision against an object
« Reply #4 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything