cache local boids

This commit is contained in:
Zynh Ludwig 2024-09-03 15:14:21 -07:00
parent 8d4b01f1ab
commit f4079d173a

View file

@ -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<Entity>);
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<Boid>>,
other_boids: Query<(Entity, &Transform), With<Boid>>,
) {
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<Boid>>) {}
fn cohesion(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}