This commit is contained in:
Zynh Ludwig 2024-09-04 12:58:52 -07:00
parent 94ff4edcca
commit 6ca933a1b1

View file

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