diff --git a/src/main.rs b/src/main.rs index bca759d..b0a858a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,8 @@ use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle}; use boid_mesh::BoidMesh; use rand::{Rng, SeedableRng}; +const VISION_RANGE: f32 = 100.; + fn main() { App::new() .init_resource::() @@ -23,7 +25,7 @@ fn main() { ( build_local_cache, (seperation_gizmos), - (seperation, cohesion, alignment), + // (seperation, cohesion, alignment), accelerate, move_boids, boundary::boid_border_teleport, @@ -55,9 +57,7 @@ struct BoidBundle { struct LocalCache(Vec); fn is_local(a: Vec3, b: Vec3) -> bool { - const LOCAL_DISTANCE: f32 = 100.; - - a.distance(b) < LOCAL_DISTANCE + a.distance(b) < VISION_RANGE } fn build_local_cache( @@ -104,13 +104,28 @@ fn seperation_gizmos( mut gizmos: Gizmos, ) { boids.iter().for_each(|(transform, local)| { - other_boids.iter_many(&local.0).for_each(|other_transform| { - gizmos.arrow_2d( - transform.translation.xy(), - other_transform.translation.xy(), - tailwind::ROSE_500, - ); - }) + other_boids + .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 mut diff = transform.translation - other_transform.translation; + diff *= diff_scale; + + gizmos.arrow( + transform.translation, + transform.translation + diff, + tailwind::RED_500, + ); + + gizmos.line( + transform.translation, + other_transform.translation, + tailwind::GREEN_500, + ); + }) }); } @@ -187,7 +202,13 @@ fn setup( mut materials: ResMut>, boid_mesh: Res, windows: Query<&Window>, + mut time: ResMut>, + mut config_store: ResMut, ) { + time.set_relative_speed(0.1); + let (gizmo_config, _) = config_store.config_mut::(); + gizmo_config.line_width = 5.; + let window = windows.get_single().unwrap(); let half_width = window.resolution.width() / 4.; let half_height = window.resolution.height() / 2.; @@ -200,7 +221,7 @@ fn setup( let lower_height = -half_height; let upper_height = half_height; - for i in 0..1000 { + for i in 0..500 { use std::f32::consts::TAU; let x = small_rng.gen_range(lower_width..=upper_width);