From c167f1d99cc9ea1c6bb455e2bf20c33b33131f85 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:20:38 -0500 Subject: [PATCH] added food spheres --- simulator/src/main.rs | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 49603c8..f122afd 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -36,35 +36,65 @@ impl Robot { let tl2 = self.view_end(); draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE); - draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, YELLOW); + draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, RED); } fn view_end(&self) -> Vec2 { self.pose.translation + self.ahead() * VIEW_DIST } - fn detect(&self, things: &[(&Pose, &dyn Shape)]) -> Option { + fn detect<'a>(&self, things: impl Iterator) -> Option { let collider = Ball::new(BOT_RADIUS); - things.iter() + things .map(|thing| contact(&self.pose, &collider, thing.0, thing.1, 0.0).unwrap()).flatten() - .map(|contactl| contactl.point1.distance(self.pose.translation)).reduce(|x,y| x.min(y)) + .map(|contactl| contactl.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) } } +struct Food { + pose: Pose, + collider: Ball, + alive: bool, +} + +impl Food { + fn new(position: Vec2) -> Self { + let pose:Pose = Pose::from_translation(position.to_array().into()); + Self { pose, alive: true, collider: Ball::new(BOT_RADIUS) } + } + + pub fn draw(&self) { + let tl = self.pose.translation; + draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); + draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, GREEN); + } + + pub fn collider(&self) -> (&Pose, &dyn Shape) { + (&self.pose, &self.collider) + } +} + + #[macroquad::main("Adversarial Foraging Simulator")] async fn main() { let mut robots = Vec::new(); - robots.push(Robot::new(Vec2 { x: 100.0, y: 100.0 })); + let mut food = Vec::new(); + food.push(Food::new(Vec2 { x: 100.0, y: 100.0 })); + loop { clear_background(LIGHTGRAY); + for food in &food{ + food.draw(); + } for robot in &mut robots { robot.move_step(1.1,0.01); println!("loop"); + println!("Food detection: {:?}",robot.detect(food.iter().map(Food::collider))); robot.draw(); } draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);