旋转点大约另一点(2D) [英] Rotating a point about another point (2D)

查看:145
本文介绍了旋转点大约另一点(2D)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个纸牌游戏,卡扇出。现在使用的Allegro API,它具有这样的功能,以显示它林:

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);

所以这个我可以让我的球迷效果容易。这个问题,然后知道哪些卡是下鼠标。要做到这一点,我觉得做一个多边形的碰撞测试。我只是不知道如何旋转4分卡上弥补多边形。基本上,我需要做同样的操作快板。

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:

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)
{
}

感谢

推荐答案

呵呵,很简单。首先减去支点(CX,CY),将其旋转,然后再添加点。

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

未经测试的:

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

如果旋转是完全错误的一边围绕我搞砸旋转。在这种情况下,它是:

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).

这篇关于旋转点大约另一点(2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆