Make cohesion() affect the acceleration

This commit is contained in:
2024-07-10 14:38:41 -05:00
parent eb6d4b2e32
commit 8fb11fe800

View File

@@ -168,14 +168,14 @@ fn check_keyboard(
fn cohesion( fn cohesion(
spatial_tree: Res<KDTree2<TrackedByKdTree>>, spatial_tree: Res<KDTree2<TrackedByKdTree>>,
mut boids: Query<(&Transform, &mut Velocity), With<Boid>>, mut boids: Query<(&Transform, &mut Acceleration), With<Boid>>,
) { ) {
// for each boid // for each boid
// find neighbors // find neighbors
// find center-of-mass of neighbors // find center-of-mass of neighbors
// find vector from boid to flock CoM // find vector from boid to flock CoM
// apply force // apply force
for (transform, mut velocity) in &mut boids { for (transform, mut acceleration) in &mut boids {
let neighbors = spatial_tree.within_distance( let neighbors = spatial_tree.within_distance(
transform.translation.xy(), transform.translation.xy(),
BOID_VIEW_RANGE BOID_VIEW_RANGE
@@ -188,7 +188,7 @@ fn cohesion(
}) / (neighbors.len()) as f32; }) / (neighbors.len()) as f32;
let towards = (center_of_mass - transform.translation).normalize(); let towards = (center_of_mass - transform.translation).normalize();
velocity.0 += towards * COHESION_FACTOR; acceleration.0 += towards * COHESION_FACTOR;
} }
} }
} }