intersectsWithAabb3 method Null safety

double? intersectsWithAabb3(
  1. Aabb3 other
)

Return the distance from the origin of this to the intersection with other if this intersects with other, or null if the don't intersect.

Implementation

double? intersectsWithAabb3(Aabb3 other) {
  final otherMin = other.min;
  final otherMax = other.max;

  var tNear = -double.maxFinite;
  var tFar = double.maxFinite;

  for (var i = 0; i < 3; ++i) {
    if (_direction[i] == 0.0) {
      if (_origin[i] < otherMin[i] || _origin[i] > otherMax[i]) {
        return null;
      }
    } else {
      var t1 = (otherMin[i] - _origin[i]) / _direction[i];
      var t2 = (otherMax[i] - _origin[i]) / _direction[i];

      if (t1 > t2) {
        final temp = t1;
        t1 = t2;
        t2 = temp;
      }

      if (t1 > tNear) {
        tNear = t1;
      }

      if (t2 < tFar) {
        tFar = t2;
      }

      if (tNear > tFar || tFar < 0) {
        return null;
      }
    }
  }

  return tNear;
}