views:

309

answers:

2

I have been able to find a lot of information on actual logic development for games. I would really like to make a card game, but I just dont understand how, based on the mouse position, an object can be selected (or atleast the proper way) First I thought of bounding box checking but not all my bitmaps are rectangles. Then I thought f making a hidden buffer wih each object having a different color, but it seems ridiculous to have to do it this way. I'm wondering how it is really done. For example, how does Adobe Flash know the object under the mouse?

Thanks

+6  A: 

Your question is how to tell if the mouse is above a non-rectangular bitmap. I am assuming all your bitmaps are really rectangular, but they have transparent regions. You must already somehow be able to tell which part of your (rectangular) bitmap is transparent, depending on the scheme you use (e.g. if you designate a color as transparent or if you use a bit mask). You will also know the z-order (layering) of bitmaps on your canvas. Then when you detect a click at position (x,y), you need to find the list of rectangular bitmaps that span over that pixel. Sort them by z-order and for each one check whether the pixel is transparent or not. If yes, move on to the next bitmap. If no, then this is the selected bitmap.

abc
+1  A: 

Or you may use geometric solution. You should store / manage the geometry of the card / item. For example a list of shapes like circles, rectangles.

Maybe triangles or ellipses if you have lots of time. Telling that a triangle has a point or not is a mathematical question and can be numerically unstable if the triangle is very thin (algorithm has a dividing).

I voted for abc.

Notinlist
Numeric instability isn't an issue. Bitmaps (e.g. the screen) are discrete, so you use integer maths. You don't calculate slope values - you calculate weighted averages of the end points. If a line runs from (x1, y1) to (x2, y2) for a point with xa you get ya=y1+(((y2-y1)*(xa-x1))/(x2-x1)). You need to make sure you have enough bits in your integers for the intermediate results, but there's only one division, and the divisor is quite tame.
Steve314
Still worth an upvote, though.
Steve314