| 27 | } |
| 28 | |
| 29 | fn hit(&self, ray: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> { |
| 30 | let oc = ray.origin - self.center; |
| 31 | let a = ray.dir.dot(ray.dir); |
| 32 | let b = oc.dot(ray.dir); |
| 33 | let c = oc.dot(oc) - self.radius * self.radius; |
| 34 | let discriminant = b * b - a * c; |
| 35 | |
| 36 | if discriminant > 0.0 { |
| 37 | let temp = (-b - discriminant.sqrt()) / a; |
| 38 | if temp < t_max && temp > t_min { |
| 39 | return Some(HitRecord { |
| 40 | t: temp, |
| 41 | point: ray.at(temp), |
| 42 | normal: (ray.at(temp) - self.center) / self.radius, |
| 43 | material_handle: self.mat, |
| 44 | }); |
| 45 | } |
| 46 | let temp = (-b + discriminant.sqrt()) / a; |
| 47 | if temp < t_max && temp > t_min { |
| 48 | return Some(HitRecord { |
| 49 | t: temp, |
| 50 | point: ray.at(temp), |
| 51 | normal: (ray.at(temp) - self.center) / self.radius, |
| 52 | material_handle: self.mat, |
| 53 | }); |
| 54 | } |
| 55 | } |
| 56 | None |
| 57 | } |
| 58 | } |