onready var PolA = $P1.polygon
onready var PolB = $P2.polygon
func _ready():
print (get_intersections())
func get_intersections() -> PoolVector2Array:
var g = Geometry
var result: PoolVector2Array
var p11: Vector2
var p12: Vector2
var p21: Vector2
var p22: Vector2
# nested loops checking intersections
# between all segments of both polygons
for i in range(0, PolA.size()):
p11 = PolA[i]
p12 = PolA[i + 1] if i + 1 < PolA.size() else PolA[0]
for j in range(0, PolB.size()):
p21 = PolB[j]
p22 = PolB[j + 1] if j + 1 < PolB.size() else PolB[0]
# use Geometry function to evaluate intersections
var intersect = g.segment_intersects_segment_2d(p11, p12, p21, p22)
if intersect != null: result.append(intersect)
return result