rayIntersection method

  1. @override
RaycastResult<ShapeHitbox>? rayIntersection(
  1. Ray2 ray, {
  2. RaycastResult<ShapeHitbox>? out,
})
override

Returns information about how the ray intersects the closest of the polygons.

If you are only interested in the intersection point use RaycastResult.intersectionPoint of the result.

Implementation

@override
RaycastResult<ShapeHitbox>? rayIntersection(
  Ray2 ray, {
  RaycastResult<ShapeHitbox>? out,
}) {
  var closestDistance = double.infinity;
  for (final vertices in globalPolygons()) {
    final result = PolygonRayIntersection.intersectPolygon(
      ray,
      vertices,
      hitbox: this,
      normal: _temporaryNormal,
      out: _temporaryResult,
    );
    final distance = result?.distance;
    if (distance != null && distance < closestDistance) {
      closestDistance = distance;
      _closestResult.setFrom(result!);
    }
  }
  if (closestDistance.isInfinite) {
    out?.reset();
    return null;
  }
  return (out ?? RaycastResult<ShapeHitbox>())..setFrom(_closestResult);
}