avoids walls but really wants to turn clockwise only

This commit is contained in:
Joe 2025-10-10 12:33:26 -07:00
parent 0ce6be4a5a
commit f65f0a5bb0

View file

@ -23,6 +23,30 @@ impl Joid {
self.pos + (rot * right), 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")] #[macroquad::main("joe's boids")]
@ -31,7 +55,8 @@ async fn main() {
let x = screen_width() / 2.0; let x = screen_width() / 2.0;
let y = screen_height() / 2.0; let y = screen_height() / 2.0;
joid.pos = Vec2::new(x, y); let mid = Vec2::new(x, y);
joid.pos = mid;
loop { loop {
let mut rot = joid.rot; let mut rot = joid.rot;
@ -61,12 +86,17 @@ async fn main() {
break; break;
} }
if is_key_down(KeyCode::R) {
joid.pos = mid;
}
let (nose, left, right) = joid.tpoints(); let (nose, left, right) = joid.tpoints();
draw_triangle(nose, left, right, Color::from_hex(0xffffff)); 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; next_frame().await;
} }