Check if mouse is on rotated sprite?

I'm new to Allegro. I have an array of 13 playing cards that I display. Each card is at an angle to make an arc. Basically, if I iterate through the cards, how can I check if the mouse is touching a card so i can do an action? I would use bounding box, but I dont feel it is precise enough.


Tobias Dammers

Two algorithms spring to mind that are easy enough to implement.

Your first (obvious) option is to use rotated bounding boxes. Since you are only comparing a point against a rotated box, you can simplify the problem by applying the same transform to both the box and the point, so that the box isn't rotated anymore. Once you have that, the collision check is trivial.

The second option is to render the cards to an invisible buffer; but instead of using the actual graphics, render each card in a distinct solid color, say, RGB(0, 0, 1) for the first card, RGB(0, 0, 2) for the second, and so on. When you're done rendering, read the pixel at the mouse position and find the card that belongs to the color.

The first option is probably faster, but limited to rotated rectangles, plus you need to work out the drawing order (if one card obscures another, some pixels will give you a collision for both cards, and you need to pick the one on top).


EDIT: beaten

Create an off-screen bitmap. In the same location where you'd draw your card to the screen that the player sees, you would draw (to this off-screen bitmap) a rotated rectangle with some solid pre-determined color.

Your mouse, then, calculates which color it's over. The color drawn tells you which card it's over. Where there is no color (transparent), the mouse isn't over anything.

Essentially, what the "player" sees and what the "mouse" sees are two different bitmaps.

Thread #601818. Printed from