overlapsRay method

bool overlapsRay(
  1. Ray ray
)

Check if the AABB is hit by a ray.

Implementation

bool overlapsRay(Ray ray) {
  final direction = ray.direction;
  final from = ray.from;
  // final t = 0

  // ray.direction is unit direction vector of ray
  final dirFracX = 1 / direction.x;
  final dirFracY = 1 / direction.y;
  final dirFracZ = 1 / direction.z;

  // this.lowerBound is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
  final t1 = (lowerBound.x - from.x) * dirFracX;
  final t2 = (upperBound.x - from.x) * dirFracX;
  final t3 = (lowerBound.y - from.y) * dirFracY;
  final t4 = (upperBound.y - from.y) * dirFracY;
  final t5 = (lowerBound.z - from.z) * dirFracZ;
  final t6 = (upperBound.z - from.z) * dirFracZ;

  // final tmin = math.max(math.max(math.min(t1, t2), math.min(t3, t4)));
  // final tmax = math.min(math.min(math.max(t1, t2), math.max(t3, t4)));
  final tmin = math.max(math.max(math.min(t1, t2), math.min(t3, t4)), math.min(t5, t6));
  final tmax = math.min(math.min(math.max(t1, t2), math.max(t3, t4)), math.max(t5, t6));

  // if tmax < 0, ray (line) is intersecting AABB, but whole AABB is behing us
  if (tmax < 0) {
    //t = tmax;
    return false;
  }

  // if tmin > tmax, ray doesn't intersect AABB
  if (tmin > tmax) {
    //t = tmax;
    return false;
  }

  return true;
}