3

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.

4

2 回答 2

1

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

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将其沿相同方向移动。你想挤出来
  • 克隆面顶点并使用移动(挤出)法线位置移动它们

有关更完整、功能更丰富的示例,请参阅Procedural Modeling Unity 示例。它们还包括一个不错的 Mesh 挤压示例(请参阅使用 MeshExtrusion.cs 的 ExtrudedMeshTrail.js)。

祝你好运!

于 2011-02-21T17:57:27.437 回答
1

要创建拉伸墙:

对于多边形中的每个顶点 a (坐标 a x, a y): - 调用下一个顶点 'b' (坐标 b x, b y) - 创建对应于从 'a' 到 'b 的线的拉伸矩形': - 矩形有顶点 (a x ,a y ,z 0 ), (a x ,a y ,z 1 ), (b x ,b y ,z 0 ), (b x ,b y ,z 1 ) - 这个矩形可以由两个三角形创建: - (a x ,a y ,z 0 ), (ax ,a y ,z 1 ), (b x , by ,z 0 ) 和 (a x ,a y ,z 1 ), (b x , by ,z 0 ), (b x , by ,z 1 )

如果你想创建一个三角带,那就更简单了。对于每个顶点 a,只需添加 (a x ,a y ,z 0 ) 和 (a x ,a y ,z 1 )。在遍历所有其他顶点之后,您首先处理的任何顶点也需要再次处理。

要创建端盖:

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

于 2011-02-28T22:56:08.940 回答