getCollisionShapeHitbox static method

ShapeHitbox? getCollisionShapeHitbox(
  1. Iterable<ShapeHitbox> shapeHitboxes,
  2. Set<Vector2> intersectionPoints
)

Implementation

static ShapeHitbox? getCollisionShapeHitbox(
  Iterable<ShapeHitbox> shapeHitboxes,
  Set<Vector2> intersectionPoints,
) {
  if (shapeHitboxes.isEmpty || intersectionPoints.isEmpty) {
    return null;
  }
  if (shapeHitboxes.length == 1) {
    return shapeHitboxes.first;
  }
  final distances = <ShapeHitbox, double>{};
  for (final hitbox in shapeHitboxes) {
    for (final element in intersectionPoints) {
      distances[hitbox] = hitbox.absoluteCenter.distanceTo(element);
      if (hitbox.containsPoint(element)) {
        return hitbox;
      }
    }
  }

  return distances.entries.reduce((a, b) => a.value < b.value ? a : b).key;
}