Compare commits

..

No commits in common. "69ddd9c7532b0a1f78b51d78cab89e7506de7bd8" and "7eba1d88fe922141126a5288fcba37894e3ba0cb" have entirely different histories.

1 changed files with 10 additions and 43 deletions

View File

@ -1,9 +1,9 @@
// #![allow(unused)] #![allow(unused)]
mod boid_mesh; mod boid_mesh;
mod boundary; 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 boid_mesh::BoidMesh;
use rand::{Rng, SeedableRng}; use rand::{Rng, SeedableRng};
@ -15,10 +15,8 @@ fn main() {
.add_systems( .add_systems(
Update, Update,
( (
build_local_cache,
(seperation, cohesion, alignment), (seperation, cohesion, alignment),
accelerate, (accelerate, move_boids).chain(),
move_boids,
boundary::boid_border_teleport, boundary::boid_border_teleport,
) )
.chain(), .chain(),
@ -53,8 +51,7 @@ struct BoidBundle {
mesh: MaterialMesh2dBundle<ColorMaterial>, mesh: MaterialMesh2dBundle<ColorMaterial>,
velocity: Velocity, velocity: Velocity,
boid: Boid, boid: Boid,
accel_cache: BoidAccelerationBundle, cache: BoidAccelerationBundle,
local_cache: LocalCache,
} }
#[derive(Component, Default)] #[derive(Component, Default)]
@ -83,38 +80,11 @@ fn build_local_cache(
}); });
} }
fn seperation( fn seperation(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With<Boid>>,
) {
}
fn cohesion(mut boids: Query<(&Transform, &LocalCache, &mut CohesionAcceleration), With<Boid>>) {} fn cohesion(mut boids: Query<(Entity, &Transform, &mut CohesionAcceleration), With<Boid>>) {}
fn alignment( fn alignment(mut boids: Query<(Entity, &Transform, &mut AlignmentAcceleration), With<Boid>>) {}
mut boids: Query<(&Velocity, &LocalCache, &mut AlignmentAcceleration), With<Boid>>,
other_boids: Query<&Velocity, With<Boid>>,
) {
const MAX_FORCE: f32 = 50.;
boids
.par_iter_mut()
.for_each(|(velocity, local, mut accel)| {
accel.0 = Vec2::ZERO;
let mut count = 0;
for other_velocity in other_boids.iter_many(&local.0[..]) {
accel.0 += other_velocity.0;
count += 1;
}
if count > 0 {
accel.0 /= count as f32;
accel.0 -= velocity.0;
accel.0 = accel.0.clamp_length_max(MAX_FORCE);
}
});
}
fn accelerate( fn accelerate(
mut boids: Query< mut boids: Query<
@ -135,12 +105,9 @@ fn accelerate(
vel.0 += coh.0 * time.delta_seconds(); vel.0 += coh.0 * time.delta_seconds();
vel.0 += ali.0 * time.delta_seconds(); vel.0 += ali.0 * time.delta_seconds();
vel.0 *= 1.005;
vel.0 = vel.0.clamp_length_max(200.);
sep.0 *= 0.; sep.0 *= 0.;
coh.0 *= 0.; sep.0 *= 0.;
ali.0 *= 0.; sep.0 *= 0.;
}); });
} }
@ -170,7 +137,7 @@ fn setup(
let lower_height = -half_height; let lower_height = -half_height;
let upper_height = half_height; let upper_height = half_height;
for _ in 0..1000 { for i in 0..1000 {
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);