oops, alignment

main
Zynh Ludwig 2024-09-03 15:44:48 -07:00
parent 7eba1d88fe
commit 62ad660c08
1 changed files with 33 additions and 10 deletions

View File

@ -1,9 +1,9 @@
#![allow(unused)]
// #![allow(unused)]
mod boid_mesh;
mod boundary;
use bevy::{color::palettes::tailwind, prelude::*, reflect::List, sprite::MaterialMesh2dBundle};
use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle};
use boid_mesh::BoidMesh;
use rand::{Rng, SeedableRng};
@ -15,8 +15,10 @@ fn main() {
.add_systems(
Update,
(
build_local_cache,
(seperation, cohesion, alignment),
(accelerate, move_boids).chain(),
accelerate,
move_boids,
boundary::boid_border_teleport,
)
.chain(),
@ -51,7 +53,8 @@ struct BoidBundle {
mesh: MaterialMesh2dBundle<ColorMaterial>,
velocity: Velocity,
boid: Boid,
cache: BoidAccelerationBundle,
accel_cache: BoidAccelerationBundle,
local_cache: LocalCache,
}
#[derive(Component, Default)]
@ -80,11 +83,29 @@ fn build_local_cache(
});
}
fn seperation(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
fn seperation(
mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With<Boid>>,
) {
}
fn cohesion(mut boids: Query<(Entity, &Transform, &mut CohesionAcceleration), With<Boid>>) {}
fn cohesion(mut boids: Query<(&Transform, &LocalCache, &mut CohesionAcceleration), With<Boid>>) {}
fn alignment(mut boids: Query<(Entity, &Transform, &mut AlignmentAcceleration), With<Boid>>) {}
fn alignment(
mut boids: Query<(&LocalCache, &mut AlignmentAcceleration), With<Boid>>,
other_boids: Query<&Velocity, With<Boid>>,
) {
boids.par_iter_mut().for_each(|(local, mut accel)| {
accel.0 = Vec2::ZERO;
let mut count = 0;
for velocity in other_boids.iter_many(&local.0[..]) {
accel.0 += velocity.0;
count += 1;
}
accel.0 /= count as f32;
});
}
fn accelerate(
mut boids: Query<
@ -105,9 +126,11 @@ fn accelerate(
vel.0 += coh.0 * time.delta_seconds();
vel.0 += ali.0 * time.delta_seconds();
vel.0 = vel.0.clamp_length_max(200.);
sep.0 *= 0.;
sep.0 *= 0.;
sep.0 *= 0.;
coh.0 *= 0.;
ali.0 *= 0.;
});
}
@ -137,7 +160,7 @@ fn setup(
let lower_height = -half_height;
let upper_height = half_height;
for i in 0..1000 {
for _ in 0..1000 {
use std::f32::consts::TAU;
let x = small_rng.gen_range(lower_width..=upper_width);