public bool Hit(Ray r, float t_min, float t_max, ref Hit_Record rec) { Vector3 oc = r.Origin - this.Center; float a = r.Direction.LengthSquared(); float half_b = Vector3.Dot(oc, r.Direction); float c = oc.LengthSquared() - this.Radius * this.Radius; float discriminant = half_b * half_b - a * c; if (discriminant > 0) { float root = (float)Math.Sqrt(discriminant); float temp = (-half_b - root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); Vector3 outward_normal = (rec.P - this.Center) / this.Radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = Material; return(true); } temp = (-half_b + root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); Vector3 outward_normal = (rec.P - this.Center) / this.Radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = Material; return(true); } } return(false); }
public bool Hit(Ray r, float t_min, float t_max, ref Hit_Record rec) { Vector3 oc = r.Origin - this.Center(r.Time); var a = r.Direction.LengthSquared(); var half_b = Vector3.Dot(oc, r.Direction); var c = oc.LengthSquared() - radius * radius; var discriminant = half_b * half_b - a * c; if (discriminant > 0) { var root = (float)Math.Sqrt(discriminant); var temp = (-half_b - root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); var outward_normal = (rec.P - this.Center(r.Time)) / radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = mat_ptr; return(true); } temp = (-half_b + root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); var outward_normal = (rec.P - this.Center(r.Time)) / radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = mat_ptr; return(true); } } return(false); }