从任何 2D 多边形生成 3D 棱镜

发布于 2024-10-18 12:59:26 字数 609 浏览 3 评论 0原文

我正在 Unity 中创建 2D 精灵游戏,这是一个 3D 游戏开发环境。 我已将对象的所有平移限制在 XY 平面上,并将旋转限制在 Z 轴上。

我的问题是用于检测对象之间碰撞的网格必须仍然是 3D 的。我需要检测玩家对象(胶囊碰撞器)和精灵(其碰撞体积由多边形棱镜定义)之间的碰撞。

我目前正在编写关卡编辑器,我需要让用户定义任何给定图块的碰撞区域。在下图中,用户按顺序单击点 P1、P2、P3、P4。

在此处输入图像描述

显然,这些点连接起来形成一个四边形。这是我想要的碰撞区域,但是我必须将其转换为 3D 网格。基本上,我需要生成多边形的挤压,然后分配顶点缠绕和三角形等。顶点位置不是计算的问题,因为它只是多边形沿 z 轴的平移。

在此处输入图像描述

我在创建分配顶点缠绕顺序的算法时遇到问题,特别是因为网格必须包含仅限于三角形。

显然,我所展示的结构并不重要,多边形可以是任何二维形状,并且始终需要形成棱柱。 有谁知道这方面的方法吗?

非常感谢大家抽出宝贵的时间。

I am creating a 2D sprite game in Unity, which is a 3D game development environment.
I have constrained all translation of objects to the XY-plane and rotation to the Z-axis.

My problem is that the meshes that are used to detect collisions between objects must still be in 3D. I have the need to detect collisions between the player object (a capsule collider) and a sprite (that has its collision volume defined by a polygonal prism).

I am currently writing the level editor and I have the need to let the user define the collision area for any given tile. In the image below the user clicks the points P1, P2, P3, P4 in that order.

enter image description here

Obviously the points join up to form a quadrilateral. This is the collision area I want, however I must then convert that to a 3D mesh. Basically I need to generate an extrusion of the polygon, then assign the vertex winding and triangles etc. The vertex positions is not a problem to figure out as it is merely a translation of the polygon down the z-axis.

enter image description here

I am having trouble creating an algorithm for assigning the winding order of the vertices, especially since the mesh must consist only of triangles.

Obviously the structure I have illustrated is not important, the polygon may be any 2d shape and will always need to form a prism.
Does anyone know any methods for this?

Thank you all very much for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2024-10-25 12:59:26

创建挤压墙

对于多边形中的每个顶点 a(坐标为 ax、ay):
- 调用下一个顶点“b”(坐标为 bx、by
- 创建与从“a”到“b”的线相对应的拉伸矩形:
- 矩形有顶点 (ax,ay,z0), (ax,a< sub>y,z1), (bx,by,z0) , (bx,by,z1)
- 这个矩形可以由两个三角形创建:
- (ax,ay,z0), (ax,ay< /sub>,z1), (bx,by,z0) 和 (a< sub>x,ay,z1), (bx,by, z0), (bx,by,z1)

如果你想创建用三角条代替,就更简单了。对于每个顶点 a,只需添加 (ax,ay,z0) 和 (ax, ay,z1)。无论您首先处理哪个顶点,在循环遍历所有其他顶点后也需要再次处理。

创建端盖:

出于碰撞目的,此步骤可能是不必要的。但是,这里有一种简单的技术: http://www.siggraph .org/education/materials/HyperGraph/scanline/outprims/polygon1.htm
每个生成的三角形应添加在深度 z0 和 z1 处。

To create the extruded walls:

For each vertex a (with coordinates ax, ay) in your polygon:
- call the next vertex 'b' (with coordinates bx, by)
- create the extruded rectangle corresponding to the line from 'a' to 'b':
- The rectangle has vertices (ax,ay,z0), (ax,ay,z1), (bx,by,z0), (bx,by,z1)
- This rectangle can be created from two triangles:
- (ax,ay,z0), (ax,ay,z1), (bx,by,z0) and (ax,ay,z1), (bx,by,z0), (bx,by,z1)

If you want to create a triangle strip instead, it's even simpler. For each vertex a, just add (ax,ay,z0) and (ax,ay,z1). Whichever vertex you processed first will also need to be processed again after looping over all other vertices.

To create the end-caps:

This step is probably unnecessary for collision purposes. But, one simple technique is here: http://www.siggraph.org/education/materials/HyperGraph/scanline/outprims/polygon1.htm
Each resulting triangle should be added at depth z0 and z1.

囚你心 2024-10-25 12:59:26

我想到的一个简单算法是这样的:

extrudedNormal = faceNormal.multiplyScale(sizeOfExtrusion);//multiply the face normal by the extrusion amt. = move along normal
for each(vertex in face){
   vPrime = vertex.clone();//copy the position of each vertex to a new object to be modified later
   vPrime.addSelf(extrudedNormal);//add translation in the direction of the normal, with the amt. used in the 
 }

所以这个想法很基本:

  • 克隆面部法线并将其移入
    amt 的同一方向。你
    想要通过
  • 克隆面顶点并移动它们 来挤出
    使用移动(拉伸)法线
    有关

更完整、功能丰富的示例,请参阅 Unity 程序建模示例。它们还包含一个不错的网格挤出示例(请参阅使用 MeshExtrusion.cs 的 ExtrudedMeshTrail.js)。

祝你好运!

A simple algorithm that comes to mind is something like this:

extrudedNormal = faceNormal.multiplyScale(sizeOfExtrusion);//multiply the face normal by the extrusion amt. = move along normal
for each(vertex in face){
   vPrime = vertex.clone();//copy the position of each vertex to a new object to be modified later
   vPrime.addSelf(extrudedNormal);//add translation in the direction of the normal, with the amt. used in the 
 }

So the idea is basic:

  • clone the face normal and move it in
    the same direction by the amt. you
    want to extrude by
  • clone the face vertices and move them
    using the moved(extruded) normal
    position

For a more complete, feature rich example, refer to the Procedural Modeling Unity samples. They include a nice Mesh extrusion sample too (see ExtrudedMeshTrail.js which uses MeshExtrusion.cs).

Goodluck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文