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

Author Topic: mapPixelToCoords and mapCoordsToPixels  (Read 128 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Newbie
  • *
  • Posts: 12
    • View Profile
mapPixelToCoords and mapCoordsToPixels
« on: August 09, 2019, 07:15:32 am »
Hello,

So I know you can use MapPixelToCoords to get the target coordinates to world coordinates using an optional view. Is it similar to sf::Mouse::getPosition(window), except relative to a view?

When would you need to perhaps use MapCoordsToPixels (an example?). Obviously, it does the opposite, but why would you ever need that?

I'm just trying to understand this more because I've always used MapPixelToCoords to get my mouse position correctly, but it seems getPosition(window) does the exact same thing. Which then made me think more about it.

Much appreciated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32123
    • View Profile
    • SFML's website
    • Email
Re: mapPixelToCoords and mapCoordsToPixels
« Reply #1 on: August 09, 2019, 08:06:53 am »
sf::Mouse::getPosition(window) gives you the position of the mouse cursor, in pixel coordinates. It has nothing to do with scene coordinates, and thus with mapPixelToCoords. These two functions do different things, and they are often used together.

By default, pixels and scene unit match perfectly, so mapPixelToCoords does nothing. But as soon as you resize the window, or use a custom view, this is no longer true. Imagine the extreme case of a 1x1 view (so all coordinates are between 0 and 1) in a 1000x1000 pixels window; sf::Mouse::getPosition(window) returns coordinates between 0 and 1000, and only mapPixelToCoords can transform them correctly to scene coordinates between 0 and 1.

The use for mapCoordsToPixels is less obvious, you're right. It can be used to map coordinates between two different views (the world and the UI, for example), like this: view_1 -> mapCoordsToPixels -> mapPixelToCoords -> view_2.
Laurent Gomila - SFML developer

SFMLNewGuy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: mapPixelToCoords and mapCoordsToPixels
« Reply #2 on: August 09, 2019, 11:58:11 pm »
Thank you so much, Laurent, for the clear answer. This makes total sense!

Much appreciated, have a great weekend.