Commit: 16d70fa
Parent: aebd48b

Allow shorter hops

Mårten Åsberg committed on 2026-04-17 at 10:54
src/obstacles.rs +28 -18
diff --git a/src/obstacles.rs b/src/obstacles.rs
index 8ef6a8a..58bbdde 100644
@@ -16,29 +16,39 @@ impl<'w, 's> Obstacles<'w, 's> {
let dir = to - from;
let ray_settings = MeshRayCastSettings::default().with_early_exit_test(&|_| true);
let mut best_effort = None;
for turn in 0..=TURNS_TO_TEST {
{
let dir = dir.rotate_y(ROTATION * turn as f32);
macro_rules! try_turn {
($rotation:expr) => {
let dir = dir.rotate_y($rotation * turn as f32);
let hits = self
.ray_caster
.cast_ray(Ray3d::new(from, dir.try_into().unwrap()), &ray_settings);
if hits.is_empty() || hits[0].1.distance.squared() > dir.length_squared() {
return Some(from + dir);
}
let hits = self
.ray_caster
.cast_ray(Ray3d::new(from, dir.try_into().unwrap()), &ray_settings);
if hits.is_empty() {
return Some(from + dir);
}
let distance_squared = hits[0].1.distance.squared();
if distance_squared > dir.length_squared() {
return Some(from + dir);
}
if if let Some((best_distance_squared, _)) = best_effort {
best_distance_squared > distance_squared
} else {
true
} {
best_effort = Some((
distance_squared,
dir.clamp_length_max(distance_squared.sqrt()),
));
}
};
}
if turn != 0 && turn != TURNS_TO_TEST {
let dir = dir.rotate_y(-ROTATION * turn as f32);
let hits = self
.ray_caster
.cast_ray(Ray3d::new(from, dir.try_into().unwrap()), &ray_settings);
if hits.is_empty() || hits[0].1.distance.squared() > dir.length_squared() {
return Some(from + dir);
}
}
try_turn!(ROTATION);
try_turn!(-ROTATION);
}
None
best_effort.map(|(_, dir)| from + dir)
}
}