1
Fork 0

seek out food

This commit is contained in:
Andy Killorin 2026-11-09 00:41:39 -05:00
parent fe5f5b5f93
commit 33f322c0b1
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -10,7 +10,7 @@ use parry2d::shape::Ball;
use parry2d::shape::Segment; use parry2d::shape::Segment;
use parry2d::shape::Shape; use parry2d::shape::Shape;
const VIEW_DIST: f32 = 80.0; const VIEW_DIST: f32 = 180.0;
const BOT_RADIUS: f32 = 16.0; const BOT_RADIUS: f32 = 16.0;
struct Robot { struct Robot {
@ -51,6 +51,13 @@ impl Robot {
.map(|thing| contact(&Pose2::IDENTITY, &collider, thing.0, thing.1, 0.0).unwrap()).flatten() .map(|thing| contact(&Pose2::IDENTITY, &collider, thing.0, thing.1, 0.0).unwrap()).flatten()
.map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) .map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y))
} }
pub fn policy(&mut self, sensor: Sensor){
match sensor {
Sensor::Food => self.move_step(1.0, 0.0),
Sensor::None => self.move_step(1.0, 0.01),
}
}
} }
struct Food { struct Food {
@ -59,6 +66,11 @@ struct Food {
alive: bool, alive: bool,
} }
enum Sensor {
Food,
None
}
impl Food { impl Food {
fn new(position: Vec2) -> Self { fn new(position: Vec2) -> Self {
let pose:Pose = Pose::from_translation(position.to_array().into()); let pose:Pose = Pose::from_translation(position.to_array().into());
@ -83,9 +95,13 @@ 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 }));
robots.push(Robot::new(Vec2 { x: 140.0, y: 100.0 })); robots.push(Robot::new(Vec2 { x: 140.0, y: 100.0 }));
robots.push(Robot::new(Vec2 { x: 140.0, y: 200.0 }));
robots.push(Robot::new(Vec2 { x: 180.0, y: 150.0 }));
let mut food = Vec::new(); let mut food = Vec::new();
food.push(Food::new(Vec2 { x: 160.0, y: 100.0 })); food.push(Food::new(Vec2 { x: 160.0, y: 100.0 }));
food.push(Food::new(Vec2 { x: 360.0, y: 100.0 }));
food.push(Food::new(Vec2 { x: 360.0, y: 300.0 }));
loop { loop {
clear_background(LIGHTGRAY); clear_background(LIGHTGRAY);
@ -95,9 +111,11 @@ async fn main() {
} }
for robot in &mut robots { for robot in &mut robots {
robot.move_step(1.1,0.01);
println!("loop"); println!("loop");
println!("Food detection: {:?}",robot.detect(food.iter().map(Food::collider))); let detect = robot.detect(food.iter().map(Food::collider));
let sensor = if detect.is_some() {Sensor::Food} else {Sensor::None};
robot.policy(sensor);
println!("Food detection: {:?}",detect);
robot.draw(); robot.draw();
} }
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);