From 2ead92970ebfd765b41f3de1f6a6a68b4dc7b5c7 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Sat, 7 Sep 2024 05:23:53 -0700 Subject: [PATCH] last tinkering --- src/main.rs | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index b0a858a..3616445 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,8 @@ fn main() { Update, ( build_local_cache, - (seperation_gizmos), - // (seperation, cohesion, alignment), + // (seperation_gizmos), + (seperation, cohesion, alignment), accelerate, move_boids, boundary::boid_border_teleport, @@ -93,8 +93,10 @@ fn cohesion( count += 1; } - accel.0 /= count as f32; - accel.0 -= transform.translation.xy(); + if count > 0 { + accel.0 /= count as f32; + accel.0 -= transform.translation.xy(); + } }) } @@ -108,11 +110,9 @@ fn seperation_gizmos( .iter_many(&local.0) .take(1) .for_each(|other_transform| { - let distance = transform.translation.distance(other_transform.translation); - let diff_scale = (distance / VISION_RANGE); + // let distance = transform.translation.distance(other_transform.translation); - let mut diff = transform.translation - other_transform.translation; - diff *= diff_scale; + let diff = transform.translation - other_transform.translation; gizmos.arrow( transform.translation, @@ -133,6 +133,27 @@ fn seperation( mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With>, other_boids: Query<&Transform, With>, ) { + const MAX_FORCE: f32 = 100.; + + boids + .par_iter_mut() + .for_each(|(transform, local, mut accel)| { + accel.0 = Vec2::ZERO; + let mut count = 0; + other_boids + .iter_many(&local.0) + .take(1) + .for_each(|other_transform| { + let diff = transform.translation - other_transform.translation; + let diff = diff.normalize(); + accel.0 += diff.xy(); + + count += 1; + }); + + accel.0 *= 150.; + // accel.0 = accel.0.clamp_length_max(MAX_FORCE); + }); } fn alignment( @@ -205,9 +226,9 @@ fn setup( mut time: ResMut>, mut config_store: ResMut, ) { - time.set_relative_speed(0.1); + // time.set_relative_speed(1.); let (gizmo_config, _) = config_store.config_mut::(); - gizmo_config.line_width = 5.; + gizmo_config.line_width = 2.5; let window = windows.get_single().unwrap(); let half_width = window.resolution.width() / 4.; @@ -221,7 +242,7 @@ fn setup( let lower_height = -half_height; let upper_height = half_height; - for i in 0..500 { + for i in 0..250 { use std::f32::consts::TAU; let x = small_rng.gen_range(lower_width..=upper_width);