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

Author Topic: SFML 2.1 equivalent of .contains()?  (Read 2486 times)

0 Members and 1 Guest are viewing this topic.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
SFML 2.1 equivalent of .contains()?
« on: September 10, 2014, 01:12:11 am »
So in this thread 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: SFML 2.1 equivalent of .contains()?
« Reply #1 on: September 10, 2014, 03:32:26 am »
Exactly the same. With your RectangleShape instead of the Sprite.

wh1t3crayon

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: SFML 2.1 equivalent of .contains()?
« Reply #2 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.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: SFML 2.1 equivalent of .contains()?
« Reply #3 on: September 10, 2014, 05:04:15 am »
Well, convert your mouse coordinates to float then... :p
Or use window.mapPixelToCoords like in the example you linked, it returns an sf::Vector2f.