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.

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.

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.

·
Santiago Trujillo

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!

·
Santiago Trujillo
Denunciar

**To create the extruded walls:**

For each vertex a (with coordinates a_{x}, a_{y}) in your polygon:
- call the next vertex 'b' (with coordinates b_{x}, b_{y})
- create the extruded rectangle corresponding to the line from 'a' to 'b':
- The rectangle has vertices (a_{x},a_{y},z_{0}), (a_{x},a_{y},z_{1}), (b_{x},b_{y},z_{0}), (b_{x},b_{y},z_{1})
- This rectangle can be created from two triangles:
- (a_{x},a_{y},z_{0}), (a_{x},a_{y},z_{1}), (b_{x},b_{y},z_{0}) and (a_{x},a_{y},z_{1}), (b_{x},b_{y},z_{0}), (b_{x},b_{y},z_{1})

*If you want to create a triangle strip instead, it's even simpler. For each vertex a, just add (a _{x},a_{y},z_{0}) and (a_{x},a_{y},z_{1}). 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 z_{0} and z_{1}.

·
Santiago Trujillo
Denunciar