如何检测 Android OpenGL-ES 应用程序中对象的部分或全部是否与另一个对象重叠?
假设我有 3 个立方体,位于随机位置/方向,并且我想检测是否有任何立方体与另一个立方体重叠(或碰撞)。当立方体位置/旋转在每个帧中改变时,也可能发生这种重叠或碰撞。请注意,我正在为此寻找基于 Android 和 OpenGL ES(1.0 或 1.1)的解决方案。
Assume I have 3 cubes at random location/orientation and I want to detect if any of the cube is overlapping (or colliding) with another cube. This overlap or collision could also happen as the cubes location/rotation are changed in each frame. Please note that I am looking for Android based and OpenGL ES (1.0 or 1.1) based solution for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是真正的 OpenGL 问题 - 它只是进行渲染。
我不知道有任何现成的用于 3D 碰撞检测的 Android 库,所以你可能只需要自己计算一下。高效的碰撞检测通常是使用快速、廉价的测试来避免进行更昂贵的分析的艺术。对于您的问题,检测立方体 A 是否与立方体 b 相交的一个好方法是进行快速拒绝测试,或者
如果边界测试表明可能发生碰撞,那么是时候进行一些数学运算了。从这里开始有两种方法:测试顶点包含和测试边/面相交
顶点包含正在测试 A 的顶点以查看它们是否位于 B 内:要么将顶点旋转到 B 的参考系中以测试包含,或者直接在 frustum-剔除风格操作。
边/面相交是测试 A 的每条边与 B 的面三角形相交。
虽然顶点包含测试比边/面测试便宜一些,但立方体有可能相交而不包含彼此的顶点,因此负结果并不意味着没有相交。类似地,立方体也可能相交,但边和面之间没有相交(如果一个位于另一个内)。您必须做一些这两项测试才能赶上每个交叉路口。如果您可以对立方体如何从一帧移动到另一帧做出一些假设,即:如果 A 和 B 没有接触上一帧,那么它们现在不太可能完全在 B 内。
This isn't really an OpenGL problem - it just does rendering.
I don't know of any ready-made android libraries for 3D collision detection, so you might just have to do the maths yourself. Efficient collision detection is generally the art of using quick, cheap tests to avoid doing more expensive analysis. For your problem, a good approach to detecting if cube A intersects cube b would be to do a quick rejection test, either
If the bounds test indicates possible collision it's time for some maths. There are two ways to go from here: testing for vertex inclusion and testing for edge/face intersection
Vertex inclusion is testing the vertices of A to see if they lie within B: either rotate the vertex into B's frame of reference to test for inclusion, or use the planes of B's faces directly in a frustum-culling style operation.
Edge/Face intersection is testing each of the edges of A for intersection with B's face triangles.
While the vertex inclusion test is a bit cheaper than Edge/Face testing, it's possible for cubes to intersect without encompassing each other's vertices, so a negative result does not mean no intersection. Similarly, it's possible for cubes to intersect without an intersection between an edge and a face (if one lies within the other). You'll have to do a little of both tests to catch every intersection. This can be avoided if you can make some assupmtions about how the cubes can move from frame to frame, i.e.: if the A and B were not touching last frame, it's unlikely that they are A is wholly within B now.