Last active
April 18, 2024 21:20
-
-
Save pascalbros/f650da7f9d2302fc0ee470cd6dcc4ab1 to your computer and use it in GitHub Desktop.
Converts a polyline (EdgeCollider2D coordinates) to a PolygonCollider2D points using the given thickness.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PolylineUtils { | |
/* | |
* Converts a polyline to a polygon with the given thickness. | |
*/ | |
static Vector2[] GeneratePolygonFromPolyline(float thickness, Vector2[] points) { | |
if (points.Length < 2) { return points; } | |
Vector2[] result = new Vector2[points.Length * 2]; | |
var normal = Vector2.zero; | |
for (int i = 0; i < points.Length - 1; i++) { | |
var direction = (points[i + 1] - points[i]).normalized; | |
normal = 0.5f * thickness * Vector2.Perpendicular(direction).normalized; | |
result[i] = points[i] + normal; | |
result[points.Length * 2 - 1 - i] = points[i] - normal; | |
} | |
result[points.Length - 1] = points[^1] + normal; | |
result[points.Length] = points[^1] - normal; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment