intersectsWithAabb3 method

double intersectsWithAabb3 (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 Vector3 otherMin = other.min;
  final Vector3 otherMax = other.max;

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

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

      if (t1 > t2) {
        final double 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;
}