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

Author Topic: Detecting Mouse Inside Sprite  (Read 1719 times)

0 Members and 1 Guest are viewing this topic.

7ark

  • Newbie
  • *
  • Posts: 4
    • View Profile
Detecting Mouse Inside Sprite
« on: March 01, 2018, 05:31:55 am »
Hello, I recently started using SFML, not new to programming, though it's been a few months since I've used a lot of C++.

Trying to detect if the mouse pointer is within the bounds of a sprite, and I cannot seem to get it right. I have a custom view setup just like this
Code: [Select]
mainView.setSize(sf::Vector2f(GameSettings::width, GameSettings::height));
mainView.setCenter(sf::Vector2f(0, 0));

I have a GameObject class and such which contains a sprite, whose position is set in the constructor. In my main loop I'm updating a static reference to a sf::vector3f for the mouse position which I get with sf::Mouse::getPosition(window);

So later I attempt to determine if the mouse pointer is within the area of the sprite, but I've been having trouble getting it to detect correctly.
I am doing this
Code: [Select]
if (gameObject->GetSprite().getGlobalBounds().contains(Game::MousePosition()))
Where it gets the sprite, and Game::MousePosition() returns that mouse position from earlier.

I'd appreciate any help, thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Detecting Mouse Inside Sprite
« Reply #1 on: March 01, 2018, 07:14:22 am »
Since you modify the view, the screen to world space mapping isn't 1:1, thus you need to use mapPixelToCoords to get the mouse coordinates in world space.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

7ark

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Detecting Mouse Inside Sprite
« Reply #2 on: March 01, 2018, 04:52:15 pm »
Since you modify the view, the screen to world space mapping isn't 1:1, thus you need to use mapPixelToCoords to get the mouse coordinates in world space.

Yeah, I actually ended up doing that without it working still. Found out the way I'm dealing with my transform is the problem. I have a sf::Transform on my Object class, which calls the renderer.draw(sprite, transform). So even tho I was scaling my object, the actual script size isn't actually smaller. So I need to find some way of actually changing the sprite.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Detecting Mouse Inside Sprite
« Reply #3 on: March 01, 2018, 06:08:52 pm »
Once you have the world co-ordinates of the mouse position, you can then transform them using the sprite's inverse transform and compare the then (un-)transformed mouse position with the sprite's local instead.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*