c++ - 旋转后找到三角形的点

标签 c++ opengl math geometry

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

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

到目前为止,我一直采用的方法是简单地存储三角形的中心点 (CP),然后即时计算最终的顶点位置。船的“点”是( vector 是 x,y)the (CP.x, CP.y + H/2)。另外两点是(CP.X - H/4, CP.Y - H/2) 和(CP.X + H/4, CP.Y - H/2)。

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

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

我尝试的方法是使用余弦法则求出未旋转的三角形与旋转后的三角形之间的距离。举个例子,以下是我尝试计算旋转后“尖”顶点位置的方法:

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

知道我可能做错了什么吗?

最佳答案

感谢大家的帮助,但我认为这些解释对我来说有点太技术性了。尽管如此,您向我明确表示三角形没有特殊情况(事后看来,我应该知道这一点)所以我尝试搜索并尝试了几种方法后,找到了一种对我有用的方法。

来自 estain 的帖子 GameDev forums成功了。引用他的帖子(对 c&p 感到抱歉,但可能对遇到类似问题的其他人有用):

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!

关于c++ - 旋转后找到三角形的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3837266/

相关文章:

C++ 访问不属于对象本身的内存

c++ - 未定义对 Qt 项目中 glew 方法的引用

javascript - 计算三 Angular 形 Angular 时的小数舍入问题

C++ 如何复制字符串中的文本(从 8 个字母到 12 个字母)

c++ - 当 C++ 实现向 C 代码抛出异常时会发生什么

c++ - g++ 链接器 :/usr/lib/libGL. so.1:无法读取符号:无效操作

javascript - 计算要填充的可用插槽数量

opengl - opengl 中的 mipmap 生成 - 是硬件加速吗?

OpenGL 纹理交换

python - 计算矩阵的零空间