Agent.Bounds.IntersectRay C# (CSharp) Метод

IntersectRay() публичный Метод

public IntersectRay ( Ray ray ) : bool
ray Ray
Результат bool
    public bool IntersectRay(Ray ray)
    {
      // See http://gamedev.stackexchange.com/questions/18436/most-efficient-aabb-vs-ray-collision-algorithms
      // r.dir is unit direction vector of ray
      Vector3 dirfrac = new Vector3(0, 0, 0);
      dirfrac.x = 1.0f / ray.direction.x;
      dirfrac.y = 1.0f / ray.direction.y;
      dirfrac.z = 1.0f / ray.direction.z;
      // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
      // r.org is origin of ray
      float t1 = (lb.x - ray.origin.x) * dirfrac.x;
      float t2 = (rt.x - ray.origin.x) * dirfrac.x;
      float t3 = (lb.y - ray.origin.y) * dirfrac.y;
      float t4 = (rt.y - ray.origin.y) * dirfrac.y;
      float t5 = (lb.z - ray.origin.z) * dirfrac.z;
      float t6 = (rt.z - ray.origin.z) * dirfrac.z;

      float tmin = max(max(min(t1, t2), min(t3, t4)), min(t5, t6));
      float tmax = min(min(max(t1, t2), max(t3, t4)), max(t5, t6));

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

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

      t = tmin;
      return true;
    }
  }