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.
This commit is contained in:
@@ -194,14 +194,10 @@ fn cohesion(
|
|||||||
transform.translation.xy(),
|
transform.translation.xy(),
|
||||||
BOID_VIEW_RANGE
|
BOID_VIEW_RANGE
|
||||||
);
|
);
|
||||||
if neighbors.len() > 0 {
|
if let Some(center_mass) = center_of_boids(
|
||||||
let center_of_mass = neighbors.iter()
|
neighbors.iter().map(|boid| boid.0 )
|
||||||
.map(|(pos, _)| pos.extend(0.0) )
|
) {
|
||||||
.fold(transform.translation, |acc, neighbor| {
|
let towards = (center_mass.extend(0.0) - transform.translation).normalize();
|
||||||
acc + neighbor
|
|
||||||
}) / (neighbors.len()) as f32;
|
|
||||||
|
|
||||||
let towards = (center_of_mass - transform.translation).normalize();
|
|
||||||
acceleration.0 += towards * COHESION_FACTOR;
|
acceleration.0 += towards * COHESION_FACTOR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user