From d2a9d1214a1857e2d512402a6865802019735e3f Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 11 Jul 2024 15:40:51 -0500 Subject: [PATCH] Refactor cohesion() to use center_of_boids() I made the center_of_boids() and velocity_of_boids() functions so that they can be reused in the debug overlay operations. I should probably use them in the main system, too. --- src/birdoids_plugin.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/birdoids_plugin.rs b/src/birdoids_plugin.rs index 66325895..89a5c97f 100644 --- a/src/birdoids_plugin.rs +++ b/src/birdoids_plugin.rs @@ -194,14 +194,10 @@ fn cohesion( transform.translation.xy(), BOID_VIEW_RANGE ); - if neighbors.len() > 0 { - let center_of_mass = neighbors.iter() - .map(|(pos, _)| pos.extend(0.0) ) - .fold(transform.translation, |acc, neighbor| { - acc + neighbor - }) / (neighbors.len()) as f32; - - let towards = (center_of_mass - transform.translation).normalize(); + if let Some(center_mass) = center_of_boids( + neighbors.iter().map(|boid| boid.0 ) + ) { + let towards = (center_mass.extend(0.0) - transform.translation).normalize(); acceleration.0 += towards * COHESION_FACTOR; } }