| Name | Message | Date |
|---|---|---|
| 📁 bunnies | 1 month ago | |
| 📄 confetti.rs | 1 month ago | |
| 📄 dog.rs | 1 month ago | |
| 📄 goal.rs | 1 month ago | |
| 📄 main.rs | 1 month ago | |
| 📄 obstacles.rs | 1 month ago |
📄
src/obstacles.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use bevy::{ ecs::system::SystemParam, math::{FloatPow, Ray3d, Vec3}, picking::mesh_picking::ray_cast::{MeshRayCast, MeshRayCastSettings}, }; #[derive(SystemParam)] pub struct Obstacles<'w, 's> { ray_caster: MeshRayCast<'w, 's>, } const TURNS_TO_TEST: usize = 4; const ROTATION: f32 = std::f32::consts::PI / TURNS_TO_TEST as f32; impl<'w, 's> Obstacles<'w, 's> { pub fn avoid(&mut self, from: Vec3, to: Vec3) -> Option<Vec3> { 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 { 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() { 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()), )); } }; } try_turn!(ROTATION); try_turn!(-ROTATION); } best_effort.map(|(_, dir)| from + dir) } }