How can I sort given points to make non-intersecting polygon?

Godot Version

godot-4

Question

Hi, I’m trying to make game that player can create polygon in game. The problem is that in some cases player might choose the point that might create intersecting polygon so I want to sort every point in polygon so that every line in polygon is not intersecting each other before creating polygon

$Polygon2D.polygon = Geometry2D.convex_hull($Polygon2D.polygon)


You could also do this:

var polygons = Geometry2D.merge_polygons($Polygon2D.polygon, PackedVector2Array([]))

which will return an array of polygons, which you could then draw each individually (using their own, separate Polygon2D node) to arrive at the exact shape of the original polygon, not just their convex hull.

2 Likes

Thanks a lot! It worked