alignment again
This commit is contained in:
parent
56cfd97d77
commit
342597e086
1 changed files with 19 additions and 12 deletions
15
src/main.rs
15
src/main.rs
|
@ -91,20 +91,27 @@ fn seperation(
|
|||
fn cohesion(mut boids: Query<(&Transform, &LocalCache, &mut CohesionAcceleration), With<Boid>>) {}
|
||||
|
||||
fn alignment(
|
||||
mut boids: Query<(&LocalCache, &mut AlignmentAcceleration), With<Boid>>,
|
||||
mut boids: Query<(&Velocity, &LocalCache, &mut AlignmentAcceleration), With<Boid>>,
|
||||
other_boids: Query<&Velocity, With<Boid>>,
|
||||
) {
|
||||
boids.par_iter_mut().for_each(|(local, mut accel)| {
|
||||
const MAX_FORCE: f32 = 50.;
|
||||
|
||||
boids
|
||||
.par_iter_mut()
|
||||
.for_each(|(velocity, local, mut accel)| {
|
||||
accel.0 = Vec2::ZERO;
|
||||
let mut count = 0;
|
||||
for velocity in other_boids.iter_many(&local.0[..]) {
|
||||
accel.0 += velocity.0;
|
||||
for other_velocity in other_boids.iter_many(&local.0[..]) {
|
||||
accel.0 += other_velocity.0;
|
||||
|
||||
count += 1;
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
accel.0 /= count as f32;
|
||||
accel.0 -= velocity.0;
|
||||
|
||||
accel.0 = accel.0.clamp_length_max(MAX_FORCE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue