views:

315

answers:

1

I'm trying to make a card game where the cards fan out. Right now to display it Im using the Allegro API which has a function:

al_draw_rotated_bitmap(OBJECT_TO_ROTATE,CENTER_X,CENTER_Y,X
        ,Y,DEGREES_TO_ROTATE_IN_RADIANS);

so with this I can make my fan effect easily. The problem is then knowing which card is under the mouse. To do this I thought of doing a polygon collision test. I'm just not sure how to rotate the 4 points on the card to make up the polygon. I basically need to do the same operation as Allegro.

for example, the 4 points of the card are:

card.x

card.y

card.x + card.width

card.y + card.height

I would need a function like:

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
}

Thanks

+4  A: 

Oh, that's easy.. first subtract the pivot point (cx,cy), then rotate it, then add the point again.

untested:

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
  float s = sin(angle);
  float c = cos(angle);

  // translate point back to origin:
  p.x -= cx;
  p.y -= cy;

  // rotate point
  float xnew = p.x * c - p.y * s;
  float ynew = p.x * s + p.y * c;

  // translate point back:
  p.x = xnew + cx;
  p.y = ynew + cy;
}

If the rotation is exactly the wrong side around I've messed up the rotation. In this case it is:

  float xnew = p.x * c + p.y * s;
  float ynew = -p.x * s + p.y * c;

(I hope one day I'll remember which way is the correct one).

Nils Pipenbrinck
Wow you're really good! Thanks, Ill implement it later when I gwt home :-)
Milo
finally, a formula that actually works
DShook