hey programmers, how to detect mouse pointer in irregular shapes?

lozina

Lifer
Sep 10, 2001
11,711
8
81
Ok so capturing mouse movement in regular shapes like squares is easy, but what if you have several irregular shape objects on your screen and you want to determine when the user's mouse pointer enters those irregular shapes..

or to put it another way, if you wanted to draw a box and then highlight the box when someone mouses over it, that's easy- but what if you wanted to highlight someone mousing over something like a blob of solid color ?

just give me a rough idea of what the "best practice" or most efficient algorithm for such a task would do in general
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
There are different approaches to "collision detection" depending on what data structures you have for the underlying image, and whether you can use tricks like uniquely color coding the blobs or using a second bitmap as an image map.

Google or wiki collision detection and/or hit test
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
If you want a truly generic approach, see this.. It's a pretty nontrivial problem in some ways. If the contents are static do as DaveSimmmons recommended and use a seperate image map. That way you can simply grab the pixel under the mouse pointer and figure out where you are.
 

Atheus

Diamond Member
Jun 7, 2005
7,313
2
0
For a completely irregular shape you can either detect pixel by pixel, or make a polygon around the image and collide with that, and neither is ideal. That's why people use rendered polygons in games.
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
To be more specific, it's a 2D image of solid -olored "blobs" patched accross the screen. I want to be able to detect when the mouse cursor is over any particular blob so that I can somehow highlight it (which will be a whole other problem to figure out)

thanks for the information you guys provided i'll need to pour over it now and see what I can learn!

 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Create a rectangle for each drawn object that will enclose the complete object.

When the mouse is detected within that object; you can then run a pixel/pixel compare.

Above was stated previously.

To enhance such a concept:

Setup some recursion levels of valid/invald rectangles that are smaller and either completely inside or outside the blob. for the pixel test above to detect.

The rectangles could be created on the fly when you draw the blob object.

If the blob objects overlay each other; you will have to setup a priority level scheme for the "Z" axis.
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
Java has this feature built in. I am sure some directx libraries has this as well for C++.
Most of the time programmers use a rect or sphere for detecting collision as it can be very costly without hardware acceleration to calculate accurately. I know for a fact blizzard used sphere in wc3 for this even though the engine supported irregular 3d polygon collision.