From 515f5b866abe705c14b19e11a0fa58e6f78e2d44 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Sat, 23 Sep 2023 09:27:21 -0700 Subject: [PATCH] Fix: hit record selection mechanism Because of the mutable record being used in the loop, the previous version had a somewhat obscured way to track the nearest collision. Switching to an optional (so I can have a non-optional Material in it) means I'm not interrogating that value.... So it gets to be explicit again. I'll refactor the entire for-loop into an iterator with the min() adapter at some point. For now: Material lifetimes! --- src/scene.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/scene.rs b/src/scene.rs index 017834b..741f562 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -32,10 +32,11 @@ impl Hittable { Hittable::HittableList { hittables } => { let mut might_return: Option = None; let mut hit_anything = false; - + let mut nearest_t = t_max; for item in hittables { - if let Some(record) = item.hit(r, t_min, t_max){ + if let Some(record) = item.hit(r, t_min, nearest_t){ hit_anything = true; + nearest_t = record.t; might_return = Some(record); } }