1
Fork 0

added food spheres

This commit is contained in:
Andy Killorin 2026-11-09 00:20:38 -05:00
parent bf79f46c8b
commit c167f1d99c
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -36,35 +36,65 @@ impl Robot {
let tl2 = self.view_end(); let tl2 = self.view_end();
draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK);
draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE); 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 { fn view_end(&self) -> Vec2 {
self.pose.translation + self.ahead() * VIEW_DIST self.pose.translation + self.ahead() * VIEW_DIST
} }
fn detect(&self, things: &[(&Pose, &dyn Shape)]) -> Option<f32> { fn detect<'a>(&self, things: impl Iterator<Item = (&'a Pose, &'a dyn Shape)>) -> Option<f32> {
let collider = Ball::new(BOT_RADIUS); let collider = Ball::new(BOT_RADIUS);
things.iter() things
.map(|thing| contact(&self.pose, &collider, thing.0, thing.1, 0.0).unwrap()).flatten() .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")] #[macroquad::main("Adversarial Foraging Simulator")]
async fn main() { async fn main() {
let mut robots = Vec::new(); let mut robots = Vec::new();
robots.push(Robot::new(Vec2 { x: 100.0, y: 100.0 })); 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 { loop {
clear_background(LIGHTGRAY); clear_background(LIGHTGRAY);
for food in &food{
food.draw();
}
for robot in &mut robots { for robot in &mut robots {
robot.move_step(1.1,0.01); robot.move_step(1.1,0.01);
println!("loop"); println!("loop");
println!("Food detection: {:?}",robot.detect(food.iter().map(Food::collider)));
robot.draw(); robot.draw();
} }
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);