intersectsWithAabb2 method

bool intersectsWithAabb2(
  1. Aabb2 box
)

Whether the ray intersects the box or not.

Rays that originate on the edge of the box are considered to be intersecting with the box no matter what direction they have.

Implementation

// This uses the Branchless Ray/Bounding box intersection method by Tavian,
// but since +-infinity is replaced by +-maxFinite for directionInvX and
// directionInvY, rays that originate on an edge will always be considered to
// intersect with the aabb, no matter what direction they have.
// https://tavianator.com/2011/ray_box.html
// https://tavianator.com/2015/ray_box_nan.html
bool intersectsWithAabb2(Aabb2 box) {
  final tx1 = (box.min.x - origin.x) * directionInvX;
  final tx2 = (box.max.x - origin.x) * directionInvX;

  final ty1 = (box.min.y - origin.y) * directionInvY;
  final ty2 = (box.max.y - origin.y) * directionInvY;

  final tMin = max(min(tx1, tx2), min(ty1, ty2));
  final tMax = min(max(tx1, tx2), max(ty1, ty2));

  return tMax >= max(tMin, 0);
}