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

Author Topic: [sfml2]Mouse pos in 2d world! [SOLVED]  (Read 5671 times)

0 Members and 1 Guest are viewing this topic.

player931402

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[sfml2]Mouse pos in 2d world! [SOLVED]
« 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!
« Last Edit: October 25, 2012, 02:55:34 pm by player931402 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [sfml2]Mouse pos in 2d world!
« Reply #1 on: October 25, 2012, 12:31:35 am »
Use the convertCoords() 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

player931402

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: [sfml2]Mouse pos in 2d world!
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [sfml2]Mouse pos in 2d world!
« Reply #3 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.
Laurent Gomila - SFML developer

player931402

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: [sfml2]Mouse pos in 2d world! [SOLVED]
« Reply #4 on: October 25, 2012, 02:55:47 pm »
ty laurent :)

 

anything