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.
This commit is contained in:
@@ -159,24 +159,20 @@ fn cohesion(
|
|||||||
boids: Query<&Transform, With<Boid>>,
|
boids: Query<&Transform, With<Boid>>,
|
||||||
mut velocities: Query<&mut Velocity, With<Boid>>,
|
mut velocities: Query<&mut Velocity, With<Boid>>,
|
||||||
) {
|
) {
|
||||||
let it = boids.iter()
|
for transform in &boids {
|
||||||
.map(|transform| {
|
let neighbors = spatial_tree.within_distance(
|
||||||
let neighbors = spatial_tree.within_distance(
|
transform.translation.xy(),
|
||||||
transform.translation.xy(),
|
BOID_VIEW_RANGE
|
||||||
BOID_VIEW_RANGE
|
);
|
||||||
);
|
if neighbors.len() > 0 {
|
||||||
if neighbors.len() > 0 {
|
let center_of_mass = neighbors.iter()
|
||||||
let center_of_mass = neighbors.iter()
|
.map(|(pos, _opt_entity)| pos )
|
||||||
.map(|(pos, _opt_entity)| pos )
|
.sum::<Vec2>() / neighbors.len() as f32;
|
||||||
.sum::<Vec2>() / neighbors.len() as f32;
|
|
||||||
|
|
||||||
for mut velocity in &mut velocities {
|
for mut velocity in &mut velocities {
|
||||||
let towards = (center_of_mass - transform.translation.xy()).normalize();
|
let towards = (center_of_mass - transform.translation.xy()).normalize();
|
||||||
**velocity += towards.extend(0.0) * COHESION_FACTOR;
|
**velocity += towards.extend(0.0) * COHESION_FACTOR;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
for _ in it{
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user