# How to make a 2D polygon collider from a mesh

I have a grid of Tiles that are under one chunk gameobject and each tile is a small square mesh. I was wondering how to generate a 2d polygon collider from this 3d mesh on only the dark tiles. I can separate the dark and light tiles into 2 meshes if need be.

After spending a huge amount of time, I finally found the solution to the collide generation

``````    void MakeCollisions()
{
List<Edge> edges = new List<Edge>();
edges = GetEdges(meshCollision.triangles);
outsideEdges = FindBoundary(edges, meshCollision.vertices);

sortedOutsideEdges = SortEdges(outsideEdges);
print(sortedOutsideEdges.Count);
collisionPoints = convertEdgePathsToVector2Arrays(sortedOutsideEdges);
PC2D.pathCount = collisionPoints.Length;

for (int i = 0; i < collisionPoints.Length; i++)
{
PC2D.SetPath(i, collisionPoints*);*
``````

}

}

public struct Edge
{
public int v1;
public int v2;
public int triangleIndex;
public Edge(int aV1, int aV2, int aIndex)
{
v1 = aV1;
v2 = aV2;
triangleIndex = aIndex;
}
}

public static List GetEdges(int[] aIndices)
{
List result = new List();
for (int i = 0; i < aIndices.Length; i += 3)
{
int v1 = aIndices*;*
int v2 = aIndices[i + 1];
int v3 = aIndices[i + 2];
}
return result;
}

public static List FindBoundary(List aEdges, Vector3[] verts)
{
List result = new List(aEdges);
for (int i = result.Count - 1; i > 0; i–)
{
for (int n = i - 1; n >= 0; n–)
{
if (verts[result_.v1] == verts[result[n].v2] && verts[result*.v2] == verts[result[n].v1])
{
result.RemoveAt(i);
result.RemoveAt(n);
i–;
break;
}
}
}
return result;
}*_

public List<List> SortEdges(List bEdges)
{
List borderEdges = bEdges;

List<List> paths = new List<List>();
List currentPath = new List();

while (borderEdges.Count > 0)
{
borderEdges.RemoveAt(0);
makePath(currentPath, borderEdges);
currentPath = new List();
print(borderEdges.Count);
}
return paths;
}

public void makePath(List currentPath, List borderEdges)
{
bool continuePath = true;

List verts = VerticiesCollide;

while (continuePath)
{
for (int i = 0; i < borderEdges.Count; i++)
{
if (verts[currentPath[currentPath.Count - 1].v2] == verts[borderEdges_.v1] || verts[currentPath[currentPath.Count - 1].v1] == verts[borderEdges*.v2])
{_

borderEdges.RemoveAt(i);
continuePath = true;
break;
}
else
{
continuePath = false;
}
}
if (borderEdges.Count == 0)
{
continuePath = false;
}
}
}*_

public Vector2[][] convertEdgePathsToVector2Arrays(List<List> edgePaths)
{
Vector2[][] collisionPoints = new Vector2[edgePaths.Count][];

for (int i = 0; i < edgePaths.Count; i++)
{
collisionPoints = new Vector2[edgePaths*.Count];*
for (int j = 0; j < edgePaths*.Count; j++)*
{
collisionPoints_[j] = VerticiesCollide[edgePaths*[j].v1];
}
}
return collisionPoints;
}*

Below all this code is about 350 lines commented out. Anyway if it doesn’t work for you just message me_