intersect method

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

Returns the intersection points of polygonA and polygonB The two polygons are required to be convex If they share a segment of a line, both end points and the center point of that line segment will be counted as collision points

Implementation

@override
Set<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) {
      intersectionPoints.addAll(lineA.intersections(lineB));
    }
  }
  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;
}