From 38c1d779c118dafd2e9de511bc11828c0722f15a Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Wed, 10 Jul 2024 08:40:48 -0500 Subject: [PATCH] Just use a for loop in the cohesion function I was trying to work out a way to run the loop using iterators, but that doesn't really matter. I had an empty loop to consume the results, which means I just have a for loop anyway. --- src/birdoids_plugin.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/birdoids_plugin.rs b/src/birdoids_plugin.rs index bc6b5b06..7064da41 100644 --- a/src/birdoids_plugin.rs +++ b/src/birdoids_plugin.rs @@ -159,24 +159,20 @@ fn cohesion( boids: Query<&Transform, With>, mut velocities: Query<&mut Velocity, With>, ) { - let it = boids.iter() - .map(|transform| { - let neighbors = spatial_tree.within_distance( - transform.translation.xy(), - BOID_VIEW_RANGE - ); - if neighbors.len() > 0 { - let center_of_mass = neighbors.iter() - .map(|(pos, _opt_entity)| pos ) - .sum::() / neighbors.len() as f32; - - for mut velocity in &mut velocities { - let towards = (center_of_mass - transform.translation.xy()).normalize(); - **velocity += towards.extend(0.0) * COHESION_FACTOR; - } + for transform in &boids { + let neighbors = spatial_tree.within_distance( + transform.translation.xy(), + BOID_VIEW_RANGE + ); + if neighbors.len() > 0 { + let center_of_mass = neighbors.iter() + .map(|(pos, _opt_entity)| pos ) + .sum::() / neighbors.len() as f32; + + for mut velocity in &mut velocities { + let towards = (center_of_mass - transform.translation.xy()).normalize(); + **velocity += towards.extend(0.0) * COHESION_FACTOR; } - }); - for _ in it{ - continue; + } } }