From f4079d173a041f44e7e8d9b75f3c290179611c7b Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Tue, 3 Sep 2024 15:14:21 -0700 Subject: [PATCH] cache local boids --- src/main.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ef34552..624044c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod boid_mesh; mod boundary; -use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle}; +use bevy::{color::palettes::tailwind, prelude::*, reflect::List, sprite::MaterialMesh2dBundle}; use boid_mesh::BoidMesh; use rand::{Rng, SeedableRng}; @@ -54,6 +54,32 @@ struct BoidBundle { cache: BoidAccelerationBundle, } +#[derive(Component, Default)] +struct LocalCache(Vec); + +fn is_local(a: Vec3, b: Vec3) -> bool { + const LOCAL_DISTANCE: f32 = 100.; + + a.distance(b) < LOCAL_DISTANCE +} + +fn build_local_cache( + mut boids_with_cache: Query<(Entity, &Transform, &mut LocalCache), With>, + other_boids: Query<(Entity, &Transform), With>, +) { + boids_with_cache + .par_iter_mut() + .for_each(|(id, transform, mut local_cache)| { + local_cache.0.clear(); + + for (other_id, other_transform) in other_boids.iter() { + if id != other_id && is_local(transform.translation, other_transform.translation) { + local_cache.0.push(other_id); + } + } + }); +} + fn seperation(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With>) {} fn cohesion(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With>) {}