Example #1
0
        public bool Hit(Ray ray, ref float t_min, ref float t_max, out Hit_record record)
        {
            record = new Hit_record();
            Vector3 oc = ray.origin - center;
            float   a  = Vector3.Dot(ray.direction, ray.direction);
            float   b  = Vector3.Dot(oc, ray.direction);
            float   c  = Vector3.Dot(oc, oc) - radius * radius;
            float   d  = b * b - a * c;

            if (d > 0)
            {
                float temp = (-b - Mathf.Sqrt(d)) / a;
                if (temp < t_max && temp > t_min)
                {
                    record.t        = temp;
                    record.hitpoint = ray.GetPoint(temp);
                    record.normal   = (record.hitpoint - center) / radius;
                    return(true);
                }
                temp = (-b + Mathf.Sqrt(d)) / a;
                if (temp < t_max && temp > t_min)
                {
                    record.t        = temp;
                    record.hitpoint = ray.GetPoint(temp);
                    record.normal   = (record.hitpoint - center) / radius;
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        public bool Hit(Ray r, ref float t_min, ref float t_max, out Hit_record record)
        {
            Hit_record temp_rec = new Hit_record();

            record = temp_rec;
            bool  hit_anything   = false;
            float closest_so_far = t_max;

            for (int i = 0; i < list.Count; ++i)
            {
                if (list[i].Hit(r, ref t_min, ref closest_so_far, out temp_rec))
                {
                    hit_anything   = true;
                    closest_so_far = temp_rec.t;
                    record         = temp_rec;
                }
            }

            return(hit_anything);
        }