Fix hit determination inside a sphere

This commit is contained in:
2022-07-02 06:30:42 +02:00
parent 89a2333644
commit 46ec8663ff
5 changed files with 9 additions and 23 deletions

@ -42,16 +42,16 @@ impl Hittable for Sphere {
let discriminant = half_b * half_b - a * c;
if discriminant < 0.0 { return None; }
let sqrtd = f64::sqrt(discriminant);
let root = (-half_b - sqrtd) / a;
let sqrtd = discriminant.sqrt();
let mut root = (-half_b - sqrtd) / a;
if root < t_min || t_max < root {
let root = (-half_b + sqrtd) / a;
root = (-half_b + sqrtd) / a;
if root < t_min || t_max < root { return None; }
}
let point = ray.at(root);
let normal = (point - self.center) / self.radius;
let front_face = ray.direction().dot(&normal) < 0.0;
let dot = ray.direction().dot(&normal);
let front_face = dot < 0.0;
let normal = if front_face { normal } else { -normal };
Some(HitRecord {
point,