3
votes

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.

2
Googling Winding Order or Winding Order Algorithm gives some interesting results.Tony The Lion

2 Answers

1
votes

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!

1
votes

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.