Compare commits

...

4 Commits

Author SHA1 Message Date
Zynh Ludwig 69ddd9c753 slow acceleration 2024-09-03 18:52:13 -07:00
Zynh Ludwig 342597e086 alignment again 2024-09-03 18:52:08 -07:00
Zynh Ludwig 56cfd97d77 no divide by zero 2024-09-03 16:05:42 -07:00
Zynh Ludwig 62ad660c08 oops, alignment 2024-09-03 15:57:51 -07:00
1 changed files with 43 additions and 10 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::*, reflect::List, sprite::MaterialMesh2dBundle}; use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle};
use boid_mesh::BoidMesh; use boid_mesh::BoidMesh;
use rand::{Rng, SeedableRng}; use rand::{Rng, SeedableRng};
@ -15,8 +15,10 @@ fn main() {
.add_systems( .add_systems(
Update, Update,
( (
build_local_cache,
(seperation, cohesion, alignment), (seperation, cohesion, alignment),
(accelerate, move_boids).chain(), accelerate,
move_boids,
boundary::boid_border_teleport, boundary::boid_border_teleport,
) )
.chain(), .chain(),
@ -51,7 +53,8 @@ struct BoidBundle {
mesh: MaterialMesh2dBundle<ColorMaterial>, mesh: MaterialMesh2dBundle<ColorMaterial>,
velocity: Velocity, velocity: Velocity,
boid: Boid, boid: Boid,
cache: BoidAccelerationBundle, accel_cache: BoidAccelerationBundle,
local_cache: LocalCache,
} }
#[derive(Component, Default)] #[derive(Component, Default)]
@ -80,11 +83,38 @@ 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<(&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<
@ -105,9 +135,12 @@ 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.;
sep.0 *= 0.; coh.0 *= 0.;
sep.0 *= 0.; ali.0 *= 0.;
}); });
} }
@ -137,7 +170,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 _ 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);