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

Author Topic: Problem with click and drag  (Read 1473 times)

0 Members and 1 Guest are viewing this topic.

lilbigwill99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Problem with click and drag
« on: December 20, 2016, 06:03:15 pm »
Hi, I am simply trying to click and drag a simple square. The following code compiles, but doesn't seem to work at all. The problem code:


if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {

            if (square.getGlobalBounds().contains(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y)) {
                std::cout << "you good dude" << std::endl;
                square.setPosition(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y);
            }
        }


 

cout doesn't output anything to the console, so i know that it has to be an issue with the first if statement, but I do not know any alternatives. Thank you for your time!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Problem with click and drag
« Reply #1 on: December 20, 2016, 06:06:29 pm »
Check out the official tutorials on mouse input. sf::Mouse::getPosition returns the position relative to your screen. If you want it relative to the window, you can pass the function the window object. Additionally you should use the mapPixelsToCoord function to convert from screen space to world space.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lilbigwill99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Problem with click and drag
« Reply #2 on: December 21, 2016, 02:35:31 am »
Thank you! I new it was a simple fix I just couldn't think of where I went wrong. What do you mean by world space and screen space?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with click and drag
« Reply #3 on: December 23, 2016, 01:30:11 pm »
What do you mean by world space and screen space?
World space is the co-ordinate system used for positioning, defined by the current view.
Any time you "set position" (for example), you are using world space.
Note that this is determined at point of drawing, not point of setting position so the view needs to be correct when drawing.

Screen space, however, is the actual pixels that make up the screen/window. So, moving (5, 3) would be 5 pixels right and 3 pixels down.
Mouse position is given in screen space (which pixel of the screen/window that is currently points to) and needs to be converted to world space to be able to use its position within the co-ordinates that you use in your view.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*