围绕另一个点旋转一个点 (2D)

Rotating a point about another point (2D)(围绕另一个点旋转一个点 (2D))
本文介绍了围绕另一个点旋转一个点 (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 个点来组成多边形.我基本上需要做和Allegro一样的操作.

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点是:

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),然后旋转,再添加点.

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

这篇关于围绕另一个点旋转一个点 (2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)