通过旋转从边缘确定点
我正在使用 OpenGL 在 C++ 中编写一个带有弹跳球(x 和 y,不在 Z 中弹跳)的屏幕保护程序。当这个球接触到屏幕边缘时,球上会出现一小块损坏。 (当球损坏到一定程度时,它就会爆炸。)当球不旋转时,找到球损坏的部分是很容易的。
我为此决定的算法是保持最左、最右、最顶部和最底部顶点的位置。对于每次碰撞,我显然需要知道它击中了哪个屏幕边缘。在球滚动之前,当它接触屏幕边缘时,如果它击中屏幕左侧边缘,我知道最左边的顶点是球上被击中的点。从那里,我得到了距该点 d 距离内的所有顶点。我不需要被击中的实际顶点,只需要球表面的点。
这样做,我不需要读取所有顶点,通过球的 x,y 位置平移它们并查看哪些顶点在屏幕外。这样做可以解决我所有的问题,但速度会很慢。
目前,球的旋转由俯仰、偏航和滚动控制。问题是,根据我的偏航角、俯仰角和滚动角,球外表面的哪一点接触了屏幕边缘?我已经考虑过保持向上、向右和方向向量,但我对此完全陌生,正如有人可能注意到的那样,我完全迷失了。我已经在维基百科上阅读了多次关于旋转矩阵的文章,但仍然一片空白。如果我去掉一个旋转角度,事情会简单得多,但我不想这样做。
I'm writing a screensaver with a bouncing ball (x and y, does not bounce in Z) in C++ using OpenGL. When this ball touches the edges of the screen, a small patch of damage will appear on the ball. (When the ball is damaged enough, it will explode.) Finding the part of the ball to damage is the easy part when the ball isn't rotating.
The algorithm I decided for this is that I'm keeping the position left most, right most, top most and bottom most vertex. For every collision, I obviously need to know which screen edge it hit. Before the ball could roll, when it touches a screen edge, if it hit the left screen edge, I know the left-most vertex is the point on the ball that took a hit. From there, I get all vertices that are within d distance from that point. I don't need the actual vertex that was hit, just the point on the ball's surface.
Doing this, I don't need to read all vertices, translate them by the x,y position of the ball and see which are off-screen. Doing this would solve all my problems but would be slow as hell.
Currently, the ball's rotation is controlled by pitch, yaw and roll. The problem is, what point on the ball's outer surface has touched the edge of the screen given my yaw, pitch and roll angles? I've looked into keeping an up, right and direction vector but I'm totally new to this and as someone might notice, totally lost. I've read the rotation matrix article on Wikipedia several times and still drawing a blank. If I got rid of one rotation angle it would be much simpler but I would prefer not to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有旋转角度,那么您可以在代码中重新创建模型视图矩阵。使用该矩阵,您可以将旋转应用于网格的顶点(只需通过乘法),然后像之前一样找到最左边(或其他)顶点。
本文解释了如何使用您拥有的角度构建旋转矩阵。
If you have your rotation angles then you can recreate the model view matrix in your code. With that matrix you can apply the rotation to the vertices of the mesh (simply by multiplication) and then find the left most (or whatever) vertex as you did before.
This article explains how to construct the rotation matrix with the angles you have.