SFML community forums

Help => Window => Topic started by: player931402 on October 25, 2012, 12:15:53 am

Title: [sfml2]Mouse pos in 2d world! [SOLVED]
Post by: player931402 on October 25, 2012, 12:15:53 am
I need to rework my classes for update my little engine but today i'm encountered in an error:


I run those "check" codes for finding the position of the mouse and :

This show the position of the mouse on desktop
sf::Vector2i mousepos = sf::Mouse::getPosition();

This show the position of the mouse on window
sf::Vector2i mousepos = sf::Mouse::getPosition(window);


But what code need I to run for know what is the position of the mouse on my 2D world ?


For example, now the funtion return that my mouse is on position (0,0) (i placed it at left-top) but in my world it's in position (213,352);
How can I find the 2D coords position of mouse with a sf::(mouse ?)::function ?

sfml2 lastest release and windows 7
ty!
Title: Re: [sfml2]Mouse pos in 2d world!
Post by: eXpl0it3r on October 25, 2012, 12:31:35 am
Use the convertCoords() (http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderTarget.php#afc047333937f7cb7fe557aec60239233) function of the sf::RenderTarget (i.e. sf::RenderWindow). If you want the transformation for a specific sf::View you can also supply that view as second parameter. ;)
Title: Re: [sfml2]Mouse pos in 2d world!
Post by: player931402 on October 25, 2012, 02:48:47 pm
mmm

i tried

                sf::Vector2i mousepos = sf::Mouse::getPosition(window);
                window.convertCoords(mousepos,window.getView());



and without window.getview() too, but still giving wrong coord.
Title: Re: [sfml2]Mouse pos in 2d world!
Post by: Laurent on October 25, 2012, 02:51:48 pm
convertCoords doesn't transform the given coordinates, it returns a new point.
sf::Vector2f converted = window.convertCoords(mousepos);

Note that calling it with window.getView() is useless, it always takes the current view by default.
Title: Re: [sfml2]Mouse pos in 2d world! [SOLVED]
Post by: player931402 on October 25, 2012, 02:55:47 pm
ty laurent :)