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:
我想做一个纸牌游戏,把牌扇出。现在我要用一个有功能的Allegro API来显示它:
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.
这样我就可以很容易地使我的扇子效果。问题是知道哪张牌在鼠标下面。为了做到这一点,我想做一个多边形碰撞测试。我只是不确定如何旋转卡片上的4个点来组成多边形。我基本上需要做和快板相同的操作。
for example, the 4 points of the card are:
例如,卡片的4点是:
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 个解决方案
#1
232
Oh, that's easy.. first subtract the pivot point (cx,cy), then rotate it, then add the point again.
哦,这很简单. .首先减去枢轴点(cx,cy),然后旋转它,然后再加一点。
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
56
If you rotate point (px, py)
around point (ox, oy)
by angle theta you'll get:
如果你旋转点(px, py)你会得到:
p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
p'x = cos() * (px-ox) - sin() * (p -oy) + ox。
p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy
p'y = sin() * (px-ox) + cos() * (p -oy) + oy。
this is an easy way to rotate a point in 2D.
这是在2D中旋转点的简单方法。
#3
26
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.
屏幕上的坐标系统是左撇子,即x坐标从左向右增加,y坐标从上到下递增。原点O(0,0)在屏幕的左上角。
A clockwise rotation around the origin of a point with coordinates (x, y) is given by the following equations:
在坐标(x, y)的原点附近的顺时针旋转是由下列方程给出的:
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).
其中(x', y')是旋转角和角的坐标,旋转角度(需要用弧度表示,即乘以:/ 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).
要在一个点上执行与原点O(0,0)不同的旋转,让我们说点a (a, b)(枢轴点)。首先我们翻译点旋转,即(x,y)回到原点,减去枢轴点的坐标,(x - y - b)。然后我们执行旋转,得到新的坐标(x,y),最后我们翻译,通过添加轴心点到新坐标系的坐标(x + 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:
使用函数原型:(x, y) -> (p。x,p.y);(a, b) -> (cx, cy);θ角- >:
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
14
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;
#1
232
Oh, that's easy.. first subtract the pivot point (cx,cy), then rotate it, then add the point again.
哦,这很简单. .首先减去枢轴点(cx,cy),然后旋转它,然后再加一点。
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
56
If you rotate point (px, py)
around point (ox, oy)
by angle theta you'll get:
如果你旋转点(px, py)你会得到:
p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
p'x = cos() * (px-ox) - sin() * (p -oy) + ox。
p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy
p'y = sin() * (px-ox) + cos() * (p -oy) + oy。
this is an easy way to rotate a point in 2D.
这是在2D中旋转点的简单方法。
#3
26
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.
屏幕上的坐标系统是左撇子,即x坐标从左向右增加,y坐标从上到下递增。原点O(0,0)在屏幕的左上角。
A clockwise rotation around the origin of a point with coordinates (x, y) is given by the following equations:
在坐标(x, y)的原点附近的顺时针旋转是由下列方程给出的:
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).
其中(x', y')是旋转角和角的坐标,旋转角度(需要用弧度表示,即乘以:/ 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).
要在一个点上执行与原点O(0,0)不同的旋转,让我们说点a (a, b)(枢轴点)。首先我们翻译点旋转,即(x,y)回到原点,减去枢轴点的坐标,(x - y - b)。然后我们执行旋转,得到新的坐标(x,y),最后我们翻译,通过添加轴心点到新坐标系的坐标(x + 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:
使用函数原型:(x, y) -> (p。x,p.y);(a, b) -> (cx, cy);θ角- >:
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
14
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;