找到旋转后的三角形的点 [英] Finding the points of a triangle after rotation

查看:205
本文介绍了找到旋转后的三角形的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个(相当)简单的2D项目在OpenGL。这是一种小行星克隆。



这艘船基本上是一个高度为H的等腰三角形,基地长度为H / 2。



我到目前为止的方式只是存储三角形的中心点(CP),然后计算最终的顶点位置。船的点是(向量是x,y)(CP.x,CP.y + H / 2)。其他两点是(CP.X-H / 4,CP.Y-H / 2)和(CP.X + H / 4,CP.Y-H / 2)。



为了让船面朝正确的方向,我首先调用glRotate当前的旋转角度。



这部分工作正常,但我正在运行到碰撞检测的问题。目前,我试图实现三角平面碰撞检测,但是,我首先需要找出船舶顶点的旋转后的实际点。我尝试使用三角法来计算这些点,但是我失败了。



我尝试的方法是使用余弦规则来找到距离未旋转的三角形和旋转后的三角形。举一个例子,下面是我已经尝试计算旋转后的尖顶点位置:

  // pA是一个矢量结构,它保存了船的尖端顶点的位置(centerPoint.x,centerPoint.y + height / 2)

// pA和旋转尖尖顶点之间的距离 - 使用余弦rule
float distance = sqrt((2 * pow(size / 2,2))*(1 - cosf(rotAngle)));

//与计算点的角度
float newPointAngle =(M_PI / 2) - rotAngle;
float xDif = distance * cosf(newPointAngle);
float yDif = distance * sinf(newPointAngle);

//实际绘制新点
glVertex2f(pA.x - xDif,pA.y - yDif);

任何想法我可以做错了什么?

解决方案

感谢帮助家伙,但我认为那些解释对我来说有点太技术。尽管如此,你向我清楚地告诉我,一个三角形没有特殊的情况(事后来看,我应该知道),所以我试图用我的手在搜索和尝试几种方法后,发现一个为我工作。 p>

来自estain的帖子在 GameDev论坛做到了。引用他的帖子(对于c& p抱歉,但对于遇到类似问题的其他人可能有用):


大量进入一般的解决方案和数学(如上面的海报和许多文章已经覆盖了这一点),我可以给你一个例子,如何解决问题围绕点B旋转一个点A C度。



现在。首先,如我在上一篇文章中所描述的,X轴上的点,距离origo的L距离,通过

绕原点旋转C度。

x = L * cos(C)



y = L * sin(C)



垂直矢量为x = -y | y = x,这意味着在Y轴上的点(再次地,来自origo的L)将使用公式旋转C.



x = sin(C)



y = L * cos(C)



如上图所示,最终解是投影向量的旋转的总和,因此我们可以得出公式



x'= x * cos(C)-y * sin



y'= y * cos(C)+ x * sin(C)



...你知道已经,对吧?问题是,这个公式只是绕origo旋转。因此,我们需要做的是将我们旋转的坐标系移动到origo,rotate,然后向后移动。这可以通过复数或使用矩阵的一般解决方案快速完成,但我们将坚持使用向量数学来保持简单。



第一步;移动原点。



x'= Ax - Bx



y'= Ay- By



第二步,执行旋转



x''= x'* cos(C)-y'* sin )=(Ax-Bx)* cos(C) - (Ay-By)* sin(C)

p> y' x'* sin(C)=(Ay-By)* cos(C)+(Ax-Bx)* sin(C)



返回坐标框架



x'= x+ Bx =(Ax-Bx)* cos(C) - (Ay-By)* sin )+ Bx



py= y+ By =(Ay-By)* cos(C)+(Ax-Bx)* sin +发布者:



和presto!我们有我们的旋转公式。没有所有这些>计算,我会给你。



以角度C旋转点B的点A



A.x'=(Ax-Bx)* cos(C) - (Ay-By)* sin(C)+ Bx



<如果你一直跟着我在这里(和我一起)(a-bx)=(Ay-By)* cos(C)+(Ax-Bx)* sin是一个很可爱的老师,很抱歉,如果你没有),你可以看到,你执行这些操作的顺序是非常重要的。尝试混合步骤3和1,看看你得到的公式的不同。



祝你好运和所有!



I'm working on a (rather) simple 2D project in OpenGL. It's some sort of asteroids clone.

The ship is basically an isosceles triangle of height H, with the base have a length of H/2.

The way I've been doing it so far is simply storing the center point (CP) of the triangle and then calculating the final vertex positions on the fly. The 'point' of the ship is (vectors are x,y) the (CP.x, CP.y + H/2). The other two points are (CP.X - H/4, CP.Y - H/2) and (CP.X + H/4, CP.Y - H/2).

To get the ship facing the right direction, I first call glRotate on the current rotation angle.

This part is working fine however I'm running into issues with collision detection. Currently I'm trying to implement triangle-plane collision detection however to do that, I first need to figure out the actual points of the ship vertices after rotation. I've tried using trigonometry to calculate these points, however I've failed.

The way I've attempted is was to use the cosine rule to find the distance between the unrotated triangle and the triangle after rotation. To give an example, the following is how I've tried to calculate the 'pointy' vertex position after rotation:

//pA is a vector struct holding the position of the pointy vertex of the ship (centerPoint.x, centerPoint.y + height / 2)

//Distance between pA and the rotated pointy vertex - using the cosine rule
float distance = sqrt((2 * pow(size / 2, 2)) * (1 - cosf(rotAngle)));

//The angle to the calculated point
float newPointAngle = (M_PI / 2) - rotAngle;
float xDif = distance * cosf(newPointAngle);
float yDif = distance * sinf(newPointAngle);

//Actually drawing the new point
glVertex2f(pA.x - xDif, pA.y - yDif);

Any idea what I could be doing wrong?

解决方案

Thanks for the help guys but I think those explanations were a bit too technical for me. Nonetheless you made it clear to me that there's no special case for a triangle (which, in hindsight, I should've known) so I tried my hand at searching and after trying a few methods, found one which worked for me.

The post from estain at the GameDev forums did the trick. To quote his post (sorry for the c&p but may be useful for someone else who runs into a similar issue):

Without getting to heavily into general solutions and maths (as the above posters and lots of articles have covered this already), I could give you an example on how to solve the problem "rotating a point A around point B by C degrees".

Now. First of all, as I described in the previous post, a point that is on the X axis, L distance from origo, is rotated C degrees around origo by

x = L * cos(C)

y = L * sin(C)

Similarly, the formula for a perpendicular vector is x = -y | y = x, which means that a point that is on the Y axis (again, L from origo) would be rotated by C using the formula

x = - L * sin(C)

y = L * cos(C)

As shown in the above image, the final solution is the sum of the rotations of the projected vectors, so we can derive the formula

x' = x * cos(C) - y * sin(C)

y' = y * cos(C) + x * sin(C)

... but you knew that already, right? problem is, this formula only rotates around origo. So what we need to do is move the coordinate system we're rotating around to origo, rotate and then move back. This can be done quickly with complex numbers or in general solutions with matrices, but we're gonna stick to vector math on this one to keep it simple.

first step; move the origin point.

x' = A.x - B.x

y' = A.y - B.y

second step, perform rotation

x'' = x' * cos(C) - y' * sin(C) = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C)

y'' = y' * cos(C) + x' * sin(C) = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C)

third and final step, move back the coordinate frame

x''' = x'' + B.x = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C) + B.x

y''' = y'' + B.y = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

And presto! we have our rotation formula. I'll give it to you without all those >calculations:

Rotating a point A around point B by angle C

A.x' = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C) + B.x

A.y' = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

If you've been following me here (and I'm a pretty lousy teacher, so sorry if you haven't), you can se that the ordering in which you perform these operations is very important. Try to mix step 3 and 1 and see the difference in the formulae you get.

Good luck and all!

这篇关于找到旋转后的三角形的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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