从 3D 网格生成 2D 横截面多边形
我正在编写一个游戏,它使用 3D 模型来绘制场景(自上而下的正交投影),但使用 2D 物理引擎来计算对碰撞的响应等。我有一些 3D 资产,我希望能够通过使用 XY 平面“切片”3D 网格并从生成的边创建多边形来自动生成碰撞盒。
谷歌在这方面让我失望了(而且也没有太多有用的材料)。建议?
我正在处理的网格将是所显示模型的简化版本,它们是连接的、闭合的、非凸的并且具有零亏格。
I'm writing a game which uses 3D models to draw a scene (top-down orthographic projection), but a 2D physics engine to calculate response to collisions, etc. I have a few 3D assets for which I'd like to be able to automatically generate a hitbox by 'slicing' the 3D mesh with the X-Y plane and creating a polygon from the resultant edges.
Google is failing me on this one (and not much helpful material on SO either). Suggestions?
The meshes I'm dealing with will be simplified versions of the displayed models, which are connected, closed, non-convex and have zero genus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于网格不是凸的,因此生成的横截面可能会断开,因此实际上由多个多边形组成。这意味着必须检查每个三角形,因此对 n 个三角形至少需要 O(n) 次操作。
这是一种方法:
对于 n 个三角形,这将在 O(n) 时间内运行,前提是您的三角形具有指向其三个邻居的指针,并且
T
支持恒定时间删除(例如散列放)。与所有几何算法一样,细节决定成败。例如,仔细考虑三角形的顶点恰好位于平面内的情况。
Since your meshes are not convex, the resulting cross-section may be disconnected, so actually consist of multiple polygons. This means that every triangle must be checked, so you'll need at least O(n) operations for n triangles.
Here's one way to do it:
This will run in O(n) time for n triangles, provided that your triangles have pointers to their three neighbours, and that
T
supports constant-time removals (e.g. a hash set).As with all geometric algorithms, the devil is in the details. Think carefully about cases where a triangle's vertex is exactly in the plane, for example.
您可以通过查找与平面相交的所有多边形,然后找到相交的确切线段,利用一些几何知识来完成此操作。这些线段是您正在寻找的二维多边形的线。
You can do this with abit of geometry by finding all of the polygons which intersect with the plane and then finding the exact segment of the intersection. these segments are the lines of the 2D polygon you're looking for.