SFML community forums

Help => Graphics => Topic started by: wh1t3crayon on September 10, 2014, 01:12:11 am

Title: SFML 2.1 equivalent of .contains()?
Post by: wh1t3crayon on September 10, 2014, 01:12:11 am
So in this thread http://en.sfml-dev.org/forums/index.php?topic=11195.0 (http://en.sfml-dev.org/forums/index.php?topic=11195.0), the OP is trying to test mouse collision on a sprite, and solves his problem with the function:
object.contains(mouse)
where object is a sf::Rect. However, this is for sfml 2.0, correct? If I'm trying to test for mouse on sf::RectangleShape collision in sfml 2.1, how would I go about that?
Title: Re: SFML 2.1 equivalent of .contains()?
Post by: G. on September 10, 2014, 03:32:26 am
Exactly the same. With your RectangleShape instead of the Sprite.
Title: Re: SFML 2.1 equivalent of .contains()?
Post by: wh1t3crayon on September 10, 2014, 04:44:35 am
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
                        sf::Vector2f localMousePos = sf::Mouse::getPosition(window);
                        int xc = localMousePos.x;
                        int yc = localMousePos.y;
                        for(int i = 0; i < board.GetBoxes().size(); i++){
                                sf::FloatRect fr = board.GetRect()[i].getGlobalBounds();
                                if(fr.contains(localMousePos)){

                                }
                        }

sf::Moue::getPosition(window) throws an error, saying "no conversion fromsf::vector 2i to sf::vector2f exists." If I change everything to an int value instead of float (i.e. sf::IntRect, changing GetRect()'s return to an int), then the dot left of contains() throws an error, because contains() only searches for floats. So basically the compiler is telling me I can neither have a float rect nor an int rect to solve this problem.
Title: Re: SFML 2.1 equivalent of .contains()?
Post by: G. on September 10, 2014, 05:04:15 am
Well, convert your mouse coordinates to float then... :p
Or use window.mapPixelToCoords (http://sfml-dev.org/tutorials/2.1/graphics-view.php#coordinates-conversions) like in the example you linked, it returns an sf::Vector2f.