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

Author Topic: detect a mouseclick on a Sprite  (Read 13106 times)

0 Members and 1 Guest are viewing this topic.

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
detect a mouseclick on a Sprite
« on: March 14, 2017, 07:11:19 pm »
Hello,

although i think there are already posts for this topic, i just couldnt find exactly what i wanted.

So i want to detect if the mouse is colliding with a Sprite. This question may seem idiotic, but i just want an easy way to detect that. I tried to do it with xy positions or with globalbounds of the sprite but it all didnt worked as i wanted.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: detect a mouseclick on a Sprite
« Reply #1 on: March 14, 2017, 07:37:11 pm »
Why don't you show us what you already got? :)

It's a pretty easy problem and all these other threads on the forum explain it just fine, so you must have a problem in understanding it somehow.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Satus

  • Guest
Re: detect a mouseclick on a Sprite
« Reply #2 on: March 15, 2017, 12:06:28 pm »
1. Get the position of a click.
2. Get coordinates of a sprite.
3. Check if click position was inside those coordinates.

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: detect a mouseclick on a Sprite
« Reply #3 on: March 15, 2017, 05:08:05 pm »
Why don't you show us what you already got? :)

It's a pretty easy problem and all these other threads on the forum explain it just fine, so you must have a problem in understanding it somehow.
i solved this with
if (sprite.getPosition().x <= Mouse::getPosition().x + 100 && sprite.getPosition().x >= Mouse::getPosition().x - 100 && sprite.getPosition().y <= Mouse::getPosition().y +100 && sprite.getPosition().y >= Mouse::getPosition -100)
{
//code goes here
}
 
but this only checks a hitbox of 200 + 200, if i want to change the hitbox, i could change the numbers, but it the hitbox does not depend on the actual size of the sprite/texture. i want to use this in a class and have multiple objects of this class, some with an other size than others.


1. Get the position of a click.
2. Get coordinates of a sprite.
3. Check if click position was inside those coordinates.

but wouldnt this only work if i clicked the exact origin?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: detect a mouseclick on a Sprite
« Reply #4 on: March 15, 2017, 05:13:20 pm »
Santus was nearly right, instead of the position of the object you need to get its global bounding box.
And in case you adjust the view in some way, you want to translate the position from screen pixel position to world coordinates.

auto mouse_pos = sf::Mouse::getPosition(window); // Mouse position relative to the window
auto translated_pos = window.mapPixelToCoords(mouse_pos); // Mouse position translated into world coordinates
if(sprite.getGlobalBounds().contains(translated_pos)) // Rectangle-contains-point check
    // Mouse is inside the sprite.
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: detect a mouseclick on a Sprite
« Reply #5 on: March 15, 2017, 05:16:14 pm »
thank you. I didnt know there was something like "auto", what exactly is it and what can be stored in it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
detect a mouseclick on a Sprite
« Reply #6 on: March 15, 2017, 06:07:12 pm »
auto is a C++11 feature and the compiler will automatically determine the correct type (e.g. it already knows what the return type is). You should look that up.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: detect a mouseclick on a Sprite
« Reply #7 on: March 15, 2017, 06:22:20 pm »
Thank you. Now i think i know enough to continue.

 

anything