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

Author Topic: Get sprite position relative to view  (Read 4285 times)

0 Members and 1 Guest are viewing this topic.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Get sprite position relative to view
« on: January 10, 2017, 09:07:40 pm »
My window is using letterbox view (which I found on this website), and whenever window is resized, it's pixel size changes but ratio not. The problem is, I don't  know how to get sprite position relative to the view not world position. Btw I fixed mouse position with this code:
                        sf::Vector2f mouse_position = getWindow()->mapPixelToCoords(sf::Mouse::getPosition(*getWindow()));

                        if (event->mouseMove.x >= playbutton->getPosition()->x && mouse_position.x <= playbutton->getPosition()->x + playbutton->getCurrentSize().width
                                && mouse_position.y >= playbutton->getPosition()->y && mouse_position.y <= playbutton->getPosition()->y + playbutton->getCurrentSize().height)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Get sprite position relative to view
« Reply #1 on: January 10, 2017, 11:52:47 pm »
mapPixelToCoords converts screen coordinates to world coordinates
mapCoordsToPixel converts world coordinates to screen coordinates

Am I understanding you right, that you'd now like to know the sprites position in the window's coordinate system relative to the view? If so, you'll first have to calculate the position of the view on the window, the use mapCoordsToPixel and subtract one from the other.

You should be able to extract the position calculation from the aspect ratio code you copied from somewhere (would also allow you to actually read and understand what the code does, instead of just copy & pasting ;)).

As for the example code you posted, I suggest you create an sf::FloatRect and use the contains() function instead of that unreadable if-statement.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Get sprite position relative to view
« Reply #2 on: January 11, 2017, 12:40:23 am »
Thanks.