Hello Guest

Author Topic: Detecting when the mouse is hovering over the visible parts of a Sprite  (Read 4429 times)

Tiarny

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
I've got some sprites in my scene of various different shapes and I want to detect which one the user is currently hovering over, but only when they are hovering over the "visible" parts of a sprite.

It is easy enough to do a bounding box check to see which sprite the mouse is currently over, but the problem is that this doesn't handle transparency. I was trying to think of the best way to handle this and I believe it would be to pull colour information for the sprite, as then I could do a simply check on the pixel co-ordinate to see if the current part of the sprite is transparent or not. If it is transparent then I can ignore it.

I believe what I need is a way to check the material on the atlas for my sprite, but to do that I would need some kind of bounding rectangle that tells me where my sprite is saved on the atlas (I think, but I might be wrong). Is it possible to get this information from the sprite itself?

MooNiZZ

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
Re: Detecting when the mouse is hovering over the visible parts of a Sprite
« Reply #1 on: August 13, 2012, 03:56:59 pm »
You could use the Polygon Option in the sprite collection to define the collision areas yourself. That's what i do :)

Edit: I only use it for buttons so there aren't many sprites for me to do it for. Still, it would be faster doing it that way than colorchecking the atlas.
So if you still want a general solution for colorchecking, you can get the textureCoord information from a raycast hit info~.
« Last Edit: August 13, 2012, 04:07:28 pm by MooNiZZ »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Detecting when the mouse is hovering over the visible parts of a Sprite
« Reply #2 on: August 13, 2012, 06:57:15 pm »
As MooNiZZ says, a polygon collider with caps would be the easiest option. It is likely to be the most efficient too.

Tiarny

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Detecting when the mouse is hovering over the visible parts of a Sprite
« Reply #3 on: August 13, 2012, 09:34:36 pm »
Yup, that does sound like the perfect implementation. I was able to get it up and running pretty quickly too. That's what I get for not properly reading the documentation!  :-[

Thanks for your help!