intersect method

  1. @override
List<Vector2> intersect(
  1. PolygonComponent polygonA,
  2. PolygonComponent polygonB, {
  3. Rect? overlappingRect,
})
override

Returns the intersection points of polygonA and polygonB The polygons can be either convex or concave If they share a segment of a line, both end points of that segment will be counted as collision points

Implementation

@override
List<Vector2> intersect(
  PolygonComponent polygonA,
  PolygonComponent polygonB, {
  Rect? overlappingRect,
}) {
  final intersectionPoints = <Vector2>[];
  final intersectionsA = polygonA.possibleIntersectionVertices(
    overlappingRect,
  );
  final intersectionsB = polygonB.possibleIntersectionVertices(
    overlappingRect,
  );
  for (final lineA in intersectionsA) {
    for (final lineB in intersectionsB) {
      final lineIntersections = lineA.intersections(lineB);
      for (final intersection in lineIntersections) {
        if (!intersectionPoints.contains(intersection)) {
          intersectionPoints.add(intersection);
        }
      }
    }
  }
  if (intersectionPoints.isEmpty && (polygonA.isSolid || polygonB.isSolid)) {
    final outerShape = polygonA.containsPoint(polygonB.globalVertices().first)
        ? polygonA
        : (polygonB.containsPoint(polygonA.globalVertices().first)
              ? polygonB
              : null);
    if (outerShape != null && outerShape.isSolid) {
      final innerShape = outerShape == polygonA ? polygonB : polygonA;
      return [innerShape.absoluteCenter];
    }
  }
  return intersectionPoints;
}