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

Author Topic: Convert mouse coordinates to world coordinates.  (Read 1490 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Convert mouse coordinates to world coordinates.
« on: October 25, 2022, 10:09:26 pm »
How do you convert mouse coordinates to game world coordinates?I've been trying to use mapcoordsTopixel but i'm not sure how.
This way doesn't work.It keeps saying about expecting a primary expression before the token ","

Vector2i Position2=Mouse::getPosition(window);
Vector2i Position=RenderTarget::mapCoordsToPixel(Position2& ,window);

So i've tried many different ways with this and I still can't find a way to make it work.What am I doing wrong?Thanks in advance.
    

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Convert mouse coordinates to world coordinates.
« Reply #1 on: October 25, 2022, 10:46:50 pm »
If you want to convert your mouse position (in pixels) into coordinates in your game world then it's mapPixelToCoords.

As you can see in the documentation it is not a static function, you have to call that method on an instance of RenderTarget.
You have to pass a Vector2i (it will be passed as a reference, don't try nonsensical things like Position2&) to that function, and the sf::View (not a RenderWindow) you want to convert your position in (you may (or will, eventually) have multiple sf::View). If you don't pass an sf::View then it will use the current view of your window, which may be ok.
Note that it returns a Vector2f (floats), not a Vector2i (ints), so if you store the result in an sf::Vector2i you may lose precision.

There is an example of what you're trying to do at the end of the view tutorial.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Convert mouse coordinates to world coordinates.
« Reply #2 on: October 26, 2022, 06:27:43 pm »
Thanks!