From f65f0a5bb07766fe05623494ec9abdce8138191f Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 10 Oct 2025 12:33:26 -0700 Subject: [PATCH] avoids walls but really wants to turn clockwise only --- src/main.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index a51aa82..9317a5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,30 @@ impl Joid { self.pos + (rot * right), ) } + + pub fn avoid_walls(&mut self) { + let y_max = screen_height(); + let x_max = screen_width(); + let max = y_max.max(x_max); + let push_down = Vec2::Y * y_max / (self.pos.y); + let push_up = Vec2::NEG_Y * y_max / (y_max - self.pos.y); + let push_right = Vec2::X * x_max / (self.pos.x); + let push_left = Vec2::NEG_X * x_max / (x_max - self.pos.x); + let steer = push_down + push_up + push_left + push_right; + let rot = Vec2::from_angle(self.rot.to_radians()); + let len = steer.length(); + dbg!(len); + let steer = rot.lerp(steer, len / max); + self.rot = steer.to_angle().to_degrees(); + draw_line( + 0., + 0., + steer.x * 10.0, + steer.y * 10.0, + 20.0, + Color::from_hex(0xff0000), + ); + } } #[macroquad::main("joe's boids")] @@ -31,7 +55,8 @@ async fn main() { let x = screen_width() / 2.0; let y = screen_height() / 2.0; - joid.pos = Vec2::new(x, y); + let mid = Vec2::new(x, y); + joid.pos = mid; loop { let mut rot = joid.rot; @@ -61,12 +86,17 @@ async fn main() { break; } + if is_key_down(KeyCode::R) { + joid.pos = mid; + } + let (nose, left, right) = joid.tpoints(); draw_triangle(nose, left, right, Color::from_hex(0xffffff)); - let acc = (nose - joid.pos).normalize() * joid.speed; + let step = (nose - joid.pos).normalize() * joid.speed; + joid.pos += step; - joid.pos += acc; + joid.avoid_walls(); next_frame().await; }