1
Fork 0

food detection

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

View file

@ -2,10 +2,12 @@ use std::cmp::min;
use macroquad::prelude::*;
use parry2d::math::Pose;
use parry2d::math::Pose2;
use parry2d::math::Rot2;
use parry2d::math::Vec2;
use parry2d::query::contact;
use parry2d::shape::Ball;
use parry2d::shape::Segment;
use parry2d::shape::Shape;
const VIEW_DIST: f32 = 80.0;
@ -44,10 +46,10 @@ impl Robot {
}
fn detect<'a>(&self, things: impl Iterator<Item = (&'a Pose, &'a dyn Shape)>) -> Option<f32> {
let collider = Ball::new(BOT_RADIUS);
let collider = Segment::new(self.pose.translation, self.view_end());
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: f32,y: f32| x.min(y))
.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))
}
}
@ -80,9 +82,10 @@ async fn main() {
let mut robots = Vec::new();
robots.push(Robot::new(Vec2 { x: 100.0, y: 100.0 }));
robots.push(Robot::new(Vec2 { x: 140.0, y: 100.0 }));
let mut food = Vec::new();
food.push(Food::new(Vec2 { x: 100.0, y: 100.0 }));
food.push(Food::new(Vec2 { x: 160.0, y: 100.0 }));
loop {
clear_background(LIGHTGRAY);