Compare commits
No commits in common. "6ca933a1b1eff1db4725f3854cfa18357ae9c2f8" and "4317ccf3a35409c04cbbd61cfe0a065ce1d1b6a6" have entirely different histories.
6ca933a1b1
...
4317ccf3a3
|
@ -14,7 +14,6 @@ features = [
|
|||
"bevy_color",
|
||||
"bevy_render",
|
||||
"bevy_winit",
|
||||
"bevy_gizmos",
|
||||
"multi_threaded",
|
||||
"x11",
|
||||
"wayland",
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct SeperationAcceleration(pub Vec2);
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct CohesionAcceleration(pub Vec2);
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct AlignmentAcceleration(pub Vec2);
|
||||
|
||||
#[derive(Bundle, Default)]
|
||||
pub struct BoidAccelerationBundle {
|
||||
pub s_cache: SeperationAcceleration,
|
||||
pub c_cache: CohesionAcceleration,
|
||||
pub a_cache: AlignmentAcceleration,
|
||||
}
|
83
src/main.rs
83
src/main.rs
|
@ -1,20 +1,12 @@
|
|||
#![allow(clippy::type_complexity)]
|
||||
// #![allow(unused)]
|
||||
|
||||
mod acceleration;
|
||||
mod boid_mesh;
|
||||
mod boundary;
|
||||
|
||||
use acceleration::{
|
||||
AlignmentAcceleration, BoidAccelerationBundle, CohesionAcceleration, SeperationAcceleration,
|
||||
};
|
||||
|
||||
use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle};
|
||||
use boid_mesh::BoidMesh;
|
||||
use rand::{Rng, SeedableRng};
|
||||
|
||||
const VISION_RANGE: f32 = 100.;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.init_resource::<BoidMesh>()
|
||||
|
@ -24,8 +16,7 @@ fn main() {
|
|||
Update,
|
||||
(
|
||||
build_local_cache,
|
||||
(seperation_gizmos),
|
||||
// (seperation, cohesion, alignment),
|
||||
(seperation, cohesion, alignment),
|
||||
accelerate,
|
||||
move_boids,
|
||||
boundary::boid_border_teleport,
|
||||
|
@ -35,15 +26,28 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
struct DrawGizmos;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
struct Boid;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
struct Velocity(Vec2);
|
||||
|
||||
#[derive(Component, Default)]
|
||||
struct SeperationAcceleration(Vec2);
|
||||
|
||||
#[derive(Component, Default)]
|
||||
struct CohesionAcceleration(Vec2);
|
||||
|
||||
#[derive(Component, Default)]
|
||||
struct AlignmentAcceleration(Vec2);
|
||||
|
||||
#[derive(Bundle, Default)]
|
||||
struct BoidAccelerationBundle {
|
||||
s_cache: SeperationAcceleration,
|
||||
c_cache: CohesionAcceleration,
|
||||
a_cache: AlignmentAcceleration,
|
||||
}
|
||||
|
||||
#[derive(Bundle, Default)]
|
||||
struct BoidBundle {
|
||||
mesh: MaterialMesh2dBundle<ColorMaterial>,
|
||||
|
@ -57,7 +61,9 @@ struct BoidBundle {
|
|||
struct LocalCache(Vec<Entity>);
|
||||
|
||||
fn is_local(a: Vec3, b: Vec3) -> bool {
|
||||
a.distance(b) < VISION_RANGE
|
||||
const LOCAL_DISTANCE: f32 = 100.;
|
||||
|
||||
a.distance(b) < LOCAL_DISTANCE
|
||||
}
|
||||
|
||||
fn build_local_cache(
|
||||
|
@ -87,7 +93,7 @@ fn cohesion(
|
|||
accel.0 = Vec2::ZERO;
|
||||
|
||||
let mut count = 0;
|
||||
for transform in other_boids.iter_many(&local.0) {
|
||||
for transform in other_boids.iter_many(&local.0[..]) {
|
||||
accel.0 += transform.translation.xy();
|
||||
|
||||
count += 1;
|
||||
|
@ -98,37 +104,6 @@ fn cohesion(
|
|||
})
|
||||
}
|
||||
|
||||
fn seperation_gizmos(
|
||||
boids: Query<(&Transform, &LocalCache), (With<Boid>, With<DrawGizmos>)>,
|
||||
other_boids: Query<&Transform, With<Boid>>,
|
||||
mut gizmos: Gizmos,
|
||||
) {
|
||||
boids.iter().for_each(|(transform, local)| {
|
||||
other_boids
|
||||
.iter_many(&local.0)
|
||||
.take(1)
|
||||
.for_each(|other_transform| {
|
||||
let distance = transform.translation.distance(other_transform.translation);
|
||||
let diff_scale = (distance / VISION_RANGE);
|
||||
|
||||
let mut diff = transform.translation - other_transform.translation;
|
||||
diff *= diff_scale;
|
||||
|
||||
gizmos.arrow(
|
||||
transform.translation,
|
||||
transform.translation + diff,
|
||||
tailwind::RED_500,
|
||||
);
|
||||
|
||||
gizmos.line(
|
||||
transform.translation,
|
||||
other_transform.translation,
|
||||
tailwind::GREEN_500,
|
||||
);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn seperation(
|
||||
mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With<Boid>>,
|
||||
other_boids: Query<&Transform, With<Boid>>,
|
||||
|
@ -146,7 +121,7 @@ fn alignment(
|
|||
.for_each(|(velocity, local, mut accel)| {
|
||||
accel.0 = Vec2::ZERO;
|
||||
let mut count = 0;
|
||||
for other_velocity in other_boids.iter_many(&local.0) {
|
||||
for other_velocity in other_boids.iter_many(&local.0[..]) {
|
||||
accel.0 += other_velocity.0;
|
||||
|
||||
count += 1;
|
||||
|
@ -202,13 +177,7 @@ fn setup(
|
|||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
boid_mesh: Res<BoidMesh>,
|
||||
windows: Query<&Window>,
|
||||
mut time: ResMut<Time<Virtual>>,
|
||||
mut config_store: ResMut<GizmoConfigStore>,
|
||||
) {
|
||||
time.set_relative_speed(0.1);
|
||||
let (gizmo_config, _) = config_store.config_mut::<DefaultGizmoConfigGroup>();
|
||||
gizmo_config.line_width = 5.;
|
||||
|
||||
let window = windows.get_single().unwrap();
|
||||
let half_width = window.resolution.width() / 4.;
|
||||
let half_height = window.resolution.height() / 2.;
|
||||
|
@ -221,7 +190,7 @@ fn setup(
|
|||
let lower_height = -half_height;
|
||||
let upper_height = half_height;
|
||||
|
||||
for i in 0..500 {
|
||||
for _ in 0..1000 {
|
||||
use std::f32::consts::TAU;
|
||||
|
||||
let x = small_rng.gen_range(lower_width..=upper_width);
|
||||
|
@ -229,7 +198,7 @@ fn setup(
|
|||
|
||||
let angle = small_rng.gen_range(-TAU..=TAU);
|
||||
|
||||
let mut entity_commands = commands.spawn(BoidBundle {
|
||||
commands.spawn(BoidBundle {
|
||||
mesh: MaterialMesh2dBundle {
|
||||
mesh: boid_mesh.0.clone(),
|
||||
material: boid_color.clone(),
|
||||
|
@ -240,10 +209,6 @@ fn setup(
|
|||
velocity: Velocity(Vec2::from_angle(angle) * 200.),
|
||||
..default()
|
||||
});
|
||||
|
||||
if i == 0 {
|
||||
entity_commands.insert(DrawGizmos);
|
||||
}
|
||||
}
|
||||
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
|
|
Loading…
Reference in New Issue