I am working a procedural terrain system, which can be destroyed and build. The problem I am having now, is that the collider does not match the terrain. The way I am getting points, is by checking if a pixel as 1 or 2 neighbor pixels. Only then the pixel's location will be used as a point for the collider. Then I sort the List of Vector2 locations in a clockwise way. This sorting works when the points are in a certain way. Code (CSharp): //from: https://pastebin.com/1RkaP28U //from: https://answers.unity.com/questions/877169/vector2-array-sort-clockwise.html public class ClockwiseComparer : IComparer<Vector2> { private Vector2 m_Origin; public ClockwiseComparer(Vector2 origin) { m_Origin = origin; } public int Compare(Vector2 first, Vector2 second) { return IsClockwise(first, second, m_Origin); } public static int IsClockwise(Vector2 first, Vector2 second, Vector2 origin) { if (first == second) return 0; Vector2 firstOffset = first - origin; Vector2 secondOffset = second - origin; float angle1 = Mathf.Atan2(firstOffset.x, firstOffset.y); float angle2 = Mathf.Atan2(secondOffset.x, secondOffset.y); if (angle1 < angle2) return -1; if (angle1 > angle2) return 1; return (firstOffset.sqrMagnitude < secondOffset.sqrMagnitude) ? -1 : 1; } } Can someone help me with this, or maybe suggest a different method of doing this.
All these red points will become part of the collider. And because terrain can be destroyed, this might also happen
Take a peak at this https://unity3d.com/learn/tutorials/projects/procedural-cave-generation-tutorial/marching-squares