Rotating a point about another point (2D)

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

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

回答1:

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;   return p; } 


回答2:

If you rotate point (px, py) around point (ox, oy) by angle theta you'll get:

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox

p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy

this is an easy way to rotate a point in 2D.



回答3:

The coordinate system on the screen is left-handed, i.e. the x coordinate increases from left to right and the y coordinate increases from top to bottom. The origin, O(0, 0) is at the upper left corner of the screen.

A clockwise rotation around the origin of a point with coordinates (x, y) is given by the following equations:

where (x', y') are the coordinates of the point after rotation and angle theta, the angle of rotation (needs to be in radians, i.e. multiplied by: PI / 180).

To perform rotation around a point different from the origin O(0,0), let's say point A(a, b) (pivot point). Firstly we translate the point to be rotated, i.e. (x, y) back to the origin, by subtracting the coordinates of the pivot point, (x - a, y - b). Then we perform the rotation and get the new coordinates (x', y') and finally we translate the point back, by adding the coordinates of the pivot point to the new coordinates (x' + a, y' + b).

Following the above description:

a 2D clockwise theta degrees rotation of point (x, y) around point (a, b) is:

Using your function prototype: (x, y) -> (p.x, p.y); (a, b) -> (cx, cy); theta -> angle:

POINT rotate_point(float cx, float cy, float angle, POINT p){       return POINT(cos(angle) * (p.x - cx) - sin(angle) * (p.y - cy) + cx,                   sin(angle) * (p.x - cx) + cos(angle) * (p.y - cy) + cy); } 


回答4:

float s = sin(angle); // angle is in radians float c = cos(angle); // angle is in radians 

For clockwise rotation :

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

For counter clockwise rotation :

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!