1
Fork 0

detector loop

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

View file

@ -1,7 +1,12 @@
use std::cmp::min;
use macroquad::prelude::*; use macroquad::prelude::*;
use parry2d::math::Pose; use parry2d::math::Pose;
use parry2d::math::Rot2; use parry2d::math::Rot2;
use parry2d::math::Vec2; use parry2d::math::Vec2;
use parry2d::query::contact;
use parry2d::shape::Ball;
use parry2d::shape::Shape;
const VIEW_DIST: f32 = 80.0; const VIEW_DIST: f32 = 80.0;
const BOT_RADIUS: f32 = 16.0; const BOT_RADIUS: f32 = 16.0;
@ -28,11 +33,22 @@ impl Robot {
pub fn draw(&self) { pub fn draw(&self) {
let tl = self.pose.translation; let tl = self.pose.translation;
let tl2 = self.pose.translation + self.ahead() * VIEW_DIST; 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, YELLOW);
} }
fn view_end(&self) -> Vec2 {
self.pose.translation + self.ahead() * VIEW_DIST
}
fn detect(&self, things: &[(&Pose, &dyn Shape)]) -> Option<f32> {
let collider = Ball::new(BOT_RADIUS);
things.iter()
.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))
}
} }
#[macroquad::main("Adversarial Foraging Simulator")] #[macroquad::main("Adversarial Foraging Simulator")]