From 342597e0867b36c7674fb231d3852e21ea0d4b54 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Tue, 3 Sep 2024 18:52:08 -0700 Subject: [PATCH] alignment again --- src/main.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index a96a428..ec7f7f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,22 +91,29 @@ fn seperation( fn cohesion(mut boids: Query<(&Transform, &LocalCache, &mut CohesionAcceleration), With>) {} fn alignment( - mut boids: Query<(&LocalCache, &mut AlignmentAcceleration), With>, + mut boids: Query<(&Velocity, &LocalCache, &mut AlignmentAcceleration), With>, other_boids: Query<&Velocity, With>, ) { - boids.par_iter_mut().for_each(|(local, mut accel)| { - accel.0 = Vec2::ZERO; - let mut count = 0; - for velocity in other_boids.iter_many(&local.0[..]) { - accel.0 += velocity.0; + const MAX_FORCE: f32 = 50.; - count += 1; - } + boids + .par_iter_mut() + .for_each(|(velocity, local, mut accel)| { + accel.0 = Vec2::ZERO; + let mut count = 0; + for other_velocity in other_boids.iter_many(&local.0[..]) { + accel.0 += other_velocity.0; - if count > 0 { - accel.0 /= count as f32; - } - }); + count += 1; + } + + if count > 0 { + accel.0 /= count as f32; + accel.0 -= velocity.0; + + accel.0 = accel.0.clamp_length_max(MAX_FORCE); + } + }); } fn accelerate(