From 7b380196a5ff150424c08b128fff0bd191b2f8a1 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 2 Sep 2025 16:25:10 -0500 Subject: [PATCH] Filter-map the entities to avoid self-alignment The boids shouldn't try to align with themselves. That doesn't really make any sense. --- src/birdoids/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/birdoids/mod.rs b/src/birdoids/mod.rs index 482c6454..ee1a6f11 100644 --- a/src/birdoids/mod.rs +++ b/src/birdoids/mod.rs @@ -189,7 +189,7 @@ fn separation( fn alignment( spatial_tree: Res>, - mut boids: Query<(&Transform, &mut Force), With>, + mut boids: Query<(Entity, &Transform, &mut Force), With>, boid_velocities: Query<&Velocity, With>, ) { // for each boid @@ -199,17 +199,21 @@ fn alignment( // perpendicular so that magnitude is constant // apply steering force - for (transform, mut force) in &mut boids { + for (this_entt, transform, mut force) in &mut boids { let neighbors = spatial_tree.within_distance(transform.translation.xy(), BOID_VIEW_RANGE); // averaging divides by length. Guard against an empty set of neighbors let (len, sum) = neighbors .iter() // Extract the velocities by `get()`ing from another query param. - .map(|(_pos, maybe_entt)| { + .filter_map(|(_pos, maybe_entt)| { let entt = maybe_entt .expect("Neighbor boid has no Entity ID. I don't know what this means"); + if this_entt == entt { + None + } else { let vel = boid_velocities.get(entt).expect("Boid has no velocity!"); - vel.xy() + Some(vel.xy()) + } }) .enumerate() .fold((0, Vec2::ZERO), |(_len, vel_acc), (idx, vel)| {