public override bool Hit(Ray ray, float tMin, float tMax, out RayHit hit) { var oc = ray.Origin - Center; var a = ray.Dir.LengthSquared; var halfB = 2 * Vector3.Dot(oc, ray.Dir); var c = oc.LengthSquared - (Radius * Radius); float discriminant = (halfB * halfB) - (4 * (a * c)); if (discriminant < Utils.Epsilon) { hit = new RayHit(); return(false); } float tmp = MathF.Sqrt(discriminant); float t = (-halfB - tmp) / (2 * a); if (t > tMin && t < tMax) { Vector3 position = ray.PointAt(t); Vector3 normal = (position - Center) / Radius; hit = new RayHit(position, t, this); hit.SetNormal(ray, normal); return(true); } t = (-halfB + tmp) / (2 * a); if (t > tMin && t < tMax) { Vector3 position = ray.PointAt(t); Vector3 normal = (position - Center) / Radius; hit = new RayHit(position, t, this); hit.SetNormal(ray, -normal); return(true); } hit = new RayHit(); return(false); }
public override bool Hit(Ray ray, float tMin, float tMax, out RayHit hit) { hit = new RayHit(); for (int i = 0; i < Faces.Count; i++) { float denom = Vector3.Dot(ray.Dir, Faces[i].Normal); if (denom < -Utils.Epsilon) // denom can't be 0 { float t = (Vector3.Dot(Faces[i].PointInPlane - ray.Origin, Faces[i].OriginalNormal)) / denom; if (t > tMin && t < tMax) { hit = new RayHit(ray.PointAt(t), t, this); hit.SetNormal(ray, Faces[i].Normal.Normalized()); return(true); } } } return(false); }