1
Fork 0

kin visuals

This commit is contained in:
Andy Killorin 2026-11-09 01:01:26 -05:00
parent 33f322c0b1
commit 4bc18b26ff
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -1,4 +1,5 @@
use std::cmp::min;
use std::ptr;
use macroquad::prelude::*;
use parry2d::math::Pose;
@ -16,12 +17,14 @@ const BOT_RADIUS: f32 = 16.0;
struct Robot {
pose: Pose,
alive: bool,
collider: Ball,
sensor: Sensor,
}
impl Robot {
fn new(position: Vec2) -> Self {
let pose:Pose = Pose::from_translation(position.to_array().into());
Self { pose, alive: true }
Self { pose, alive: true, collider: Ball::new(BOT_RADIUS), sensor: Sensor::None}
}
fn move_step(&mut self, forward: f32, right: f32) {
@ -45,6 +48,10 @@ impl Robot {
self.pose.translation + self.ahead() * VIEW_DIST
}
pub fn collider(&self) -> (&Pose, &dyn Shape) {
(&self.pose, &self.collider)
}
fn detect<'a>(&self, things: impl Iterator<Item = (&'a Pose, &'a dyn Shape)>) -> Option<f32> {
let collider = Segment::new(self.pose.translation, self.view_end());
things
@ -52,10 +59,11 @@ impl Robot {
.map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y))
}
pub fn policy(&mut self, sensor: Sensor){
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),
Sensor::Kin => self.move_step(-0.3, -0.02),
Sensor::None => self.move_step(0.3, 0.01),
}
}
}
@ -68,6 +76,7 @@ struct Food {
enum Sensor {
Food,
Kin,
None
}
@ -110,12 +119,19 @@ async fn main() {
food.draw();
}
for robot in &mut robots {
let mut sensors = Vec::new();
for robot in &robots {
println!("loop");
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);
let detect_food = robot.detect(food.iter().map(Food::collider));
let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider));
let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None};
if detect_kin.is_some() {sensor = Sensor::Kin}
println!("Food detection: {:?}",detect_food);
println!("Kin detection: {:?}",detect_kin);
sensors.push(sensor);
}
for (idx, robot) in robots.iter_mut().enumerate() {
robot.policy(&sensors[idx]);
robot.draw();
}
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);