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

Author Topic: How do I get mouse position relative to a sprite?  (Read 3863 times)

0 Members and 1 Guest are viewing this topic.

xzbobzx

  • Newbie
  • *
  • Posts: 26
    • View Profile
How do I get mouse position relative to a sprite?
« on: October 14, 2013, 06:50:46 pm »
I'm using mouse position relative to center of the screen, but I found that for my project it would be far more useful to use mouse position relative to a sprite.

How do I do this?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How do I get mouse position relative to a sprite?
« Reply #1 on: October 14, 2013, 07:40:34 pm »
Subtract the sprite's position from the mouse position?

(If your sprite is rotated or scaled, take into account its whole transform.)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

xzbobzx

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: How do I get mouse position relative to a sprite?
« Reply #2 on: October 14, 2013, 07:59:41 pm »
Subtract the sprite's position from the mouse position?

(If your sprite is rotated or scaled, take into account its whole transform.)

But that would require sprite position relative to the window. My sprites have their position relative to the world. :\
« Last Edit: October 14, 2013, 08:04:18 pm by xzbobzx »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: How do I get mouse position relative to a sprite?
« Reply #3 on: October 14, 2013, 08:05:46 pm »
Either convert the world coordinates to pixel coordinates or the other way around. The functions you'll be interested in are: window.mapPixelToCoords or window.mapCoordsToPixel
For further details look at the view tutorial and the documentation. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

xzbobzx

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: How do I get mouse position relative to a sprite?
« Reply #4 on: October 14, 2013, 08:17:09 pm »
Either convert the world coordinates to pixel coordinates or the other way around. The functions you'll be interested in are: window.mapPixelToCoords or window.mapCoordsToPixel
For further details look at the view tutorial and the documentation. :)

Cool! Just what I was looking for, I think.
Now to figure out how to use this.

Edit: thank you! It works perfectly!
« Last Edit: October 14, 2013, 08:29:00 pm by xzbobzx »