@@ -12,7 +12,7 @@ static func makeTriangles(poly : PoolVector2Array, triangle_points : PoolIntArra
12
12
var total_area : float = 0.0
13
13
for i in range (triangle_points .size () / 3 ):
14
14
var index : int = i * 3
15
- var points : Array = [poly [triangle_points [index ]], poly [triangle_points [index + 1 ]], poly [triangle_points [index + 2 ]]]
15
+ var points : PoolVector2Array = [poly [triangle_points [index ]], poly [triangle_points [index + 1 ]], poly [triangle_points [index + 2 ]]]
16
16
17
17
var area : float = 0.0
18
18
if with_area :
@@ -34,7 +34,7 @@ static func makeTriangle(points : PoolVector2Array, area : float, centroid : Vec
34
34
# triangulates a polygon and additionally calculates the centroid and area of each triangle alongside the total area of the polygon
35
35
static func triangulatePolygon (poly : PoolVector2Array , with_area : bool = true , with_centroid : bool = true ) -> Dictionary :
36
36
var total_area : float = 0.0
37
- var triangle_points = Geometry .triangulate_polygon (poly )
37
+ var triangle_points : PoolIntArray = Geometry .triangulate_polygon (poly )
38
38
return makeTriangles (poly , triangle_points , with_area , with_centroid )
39
39
40
40
# triangulates a polygon with the delaunay method and additionally calculates the centroid and area of each triangle alongside the total area of the polygon
@@ -161,7 +161,11 @@ static func getTriangleArea(points : PoolVector2Array) -> float:
161
161
var c : float = (points [0 ] - points [1 ]).length ()
162
162
var s : float = (a + b + c ) * 0.5
163
163
164
- return sqrt (s * (s - a ) * (s - b ) * (s - c ))
164
+ var value : float = s * (s - a ) * (s - b ) * (s - c )
165
+ if value < 0.0 :
166
+ return 1.0
167
+ var area : float = sqrt (value )
168
+ return area
165
169
166
170
167
171
static func getTriangleCentroid (points : PoolVector2Array ) -> Vector2 :
@@ -260,8 +264,8 @@ static func cutShape(source_polygon : PoolVector2Array, cut_polygon : PoolVector
260
264
var intersected_polygons : Array = intersectPolygons (source_polygon , cut_polygon , true )
261
265
if intersected_polygons .size () <= 0 :
262
266
return {"final" : [], "intersected" : []}
263
- var final_polygons : Array = clipPolygons (source_polygon , cut_polygon , true )
264
267
268
+ var final_polygons : Array = clipPolygons (source_polygon , cut_polygon , true )
265
269
266
270
return {"final" : final_polygons , "intersected" : intersected_polygons }
267
271
@@ -270,13 +274,18 @@ static func cutShape(source_polygon : PoolVector2Array, cut_polygon : PoolVector
270
274
static func makeShapeInfo (centered_shape : PoolVector2Array , centroid : Vector2 , world_pos : Vector2 , area : float ) -> Dictionary :
271
275
return {"centered_shape" : centered_shape , "centroid" : centroid , "world_pos" : world_pos , "area" : area }
272
276
273
- static func getShapeInfo (source_global_trans : Transform2D , source_polygon : PoolVector2Array , calculate_area : bool = false ) -> Dictionary :
274
- var centroid : Vector2 = calculatePolygonCentroid (source_polygon )
277
+ static func getShapeInfo (source_global_trans : Transform2D , source_polygon : PoolVector2Array ) -> Dictionary :
278
+ var triangulation : Dictionary = triangulatePolygon (source_polygon , true , true )
279
+ var centroid : Vector2 = getPolygonCentroid (triangulation .triangles , triangulation .area )
280
+ var centered_shape : PoolVector2Array = translatePolygon (source_polygon , - centroid )
281
+ return makeShapeInfo (centered_shape , centroid , source_global_trans .get_origin (), triangulation .area )# {"spawn_pos" : spawn_pos, "centered_shape" : centered_shape, "centroid" : centroid, "world_pos" : source_global_trans.get_origin()}
282
+
283
+ static func getShapeInfoSimple (source_global_trans : Transform2D , source_polygon : PoolVector2Array , triangulation : Dictionary ) -> Dictionary :
284
+ var centroid : Vector2 = getPolygonCentroid (triangulation .triangles , triangulation .area )
275
285
var centered_shape : PoolVector2Array = translatePolygon (source_polygon , - centroid )
276
- var area : float = 0.0
277
- if calculate_area :
278
- area = getPolygonArea (centered_shape )
279
- return makeShapeInfo (centered_shape , centroid , source_global_trans .get_origin (), area )# {"spawn_pos" : spawn_pos, "centered_shape" : centered_shape, "centroid" : centroid, "world_pos" : source_global_trans.get_origin()}
286
+ # print("Get shape info simple Poly: ", source_polygon, " centroid: ", centroid, "triangulation area: ", triangulation.area, " Centered Shape: ", centered_shape)
287
+ # print("Triangulation: ", triangulation, " Centered Shape: ", centered_shape)
288
+ return makeShapeInfo (centered_shape , centroid , source_global_trans .get_origin (), triangulation .area )
280
289
281
290
static func getShapeSpawnPos (parent_global_trans : Transform2D , centroid : Vector2 , source_global_pos : Vector2 ) -> Vector2 :
282
291
var spawn_pos : Vector2 = toGlobal (parent_global_trans , centroid ) + source_global_pos
0 commit comments