Compare commits
3 Commits
4317ccf3a3
...
6ca933a1b1
Author | SHA1 | Date |
---|---|---|
Zynh Ludwig | 6ca933a1b1 | |
Zynh Ludwig | 94ff4edcca | |
Zynh Ludwig | d89666b3f0 |
|
@ -14,6 +14,7 @@ features = [
|
||||||
"bevy_color",
|
"bevy_color",
|
||||||
"bevy_render",
|
"bevy_render",
|
||||||
"bevy_winit",
|
"bevy_winit",
|
||||||
|
"bevy_gizmos",
|
||||||
"multi_threaded",
|
"multi_threaded",
|
||||||
"x11",
|
"x11",
|
||||||
"wayland",
|
"wayland",
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
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,12 +1,20 @@
|
||||||
|
#![allow(clippy::type_complexity)]
|
||||||
// #![allow(unused)]
|
// #![allow(unused)]
|
||||||
|
|
||||||
|
mod acceleration;
|
||||||
mod boid_mesh;
|
mod boid_mesh;
|
||||||
mod boundary;
|
mod boundary;
|
||||||
|
|
||||||
|
use acceleration::{
|
||||||
|
AlignmentAcceleration, BoidAccelerationBundle, CohesionAcceleration, SeperationAcceleration,
|
||||||
|
};
|
||||||
|
|
||||||
use bevy::{color::palettes::tailwind, prelude::*, 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};
|
||||||
|
|
||||||
|
const VISION_RANGE: f32 = 100.;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.init_resource::<BoidMesh>()
|
.init_resource::<BoidMesh>()
|
||||||
|
@ -16,7 +24,8 @@ fn main() {
|
||||||
Update,
|
Update,
|
||||||
(
|
(
|
||||||
build_local_cache,
|
build_local_cache,
|
||||||
(seperation, cohesion, alignment),
|
(seperation_gizmos),
|
||||||
|
// (seperation, cohesion, alignment),
|
||||||
accelerate,
|
accelerate,
|
||||||
move_boids,
|
move_boids,
|
||||||
boundary::boid_border_teleport,
|
boundary::boid_border_teleport,
|
||||||
|
@ -26,28 +35,15 @@ fn main() {
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Default)]
|
||||||
|
struct DrawGizmos;
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
struct Boid;
|
struct Boid;
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
struct Velocity(Vec2);
|
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)]
|
#[derive(Bundle, Default)]
|
||||||
struct BoidBundle {
|
struct BoidBundle {
|
||||||
mesh: MaterialMesh2dBundle<ColorMaterial>,
|
mesh: MaterialMesh2dBundle<ColorMaterial>,
|
||||||
|
@ -61,9 +57,7 @@ struct BoidBundle {
|
||||||
struct LocalCache(Vec<Entity>);
|
struct LocalCache(Vec<Entity>);
|
||||||
|
|
||||||
fn is_local(a: Vec3, b: Vec3) -> bool {
|
fn is_local(a: Vec3, b: Vec3) -> bool {
|
||||||
const LOCAL_DISTANCE: f32 = 100.;
|
a.distance(b) < VISION_RANGE
|
||||||
|
|
||||||
a.distance(b) < LOCAL_DISTANCE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_local_cache(
|
fn build_local_cache(
|
||||||
|
@ -93,7 +87,7 @@ fn cohesion(
|
||||||
accel.0 = Vec2::ZERO;
|
accel.0 = Vec2::ZERO;
|
||||||
|
|
||||||
let mut count = 0;
|
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();
|
accel.0 += transform.translation.xy();
|
||||||
|
|
||||||
count += 1;
|
count += 1;
|
||||||
|
@ -104,6 +98,37 @@ 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(
|
fn seperation(
|
||||||
mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With<Boid>>,
|
mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With<Boid>>,
|
||||||
other_boids: Query<&Transform, With<Boid>>,
|
other_boids: Query<&Transform, With<Boid>>,
|
||||||
|
@ -121,7 +146,7 @@ fn alignment(
|
||||||
.for_each(|(velocity, local, mut accel)| {
|
.for_each(|(velocity, local, mut accel)| {
|
||||||
accel.0 = Vec2::ZERO;
|
accel.0 = Vec2::ZERO;
|
||||||
let mut count = 0;
|
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;
|
accel.0 += other_velocity.0;
|
||||||
|
|
||||||
count += 1;
|
count += 1;
|
||||||
|
@ -177,7 +202,13 @@ fn setup(
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
boid_mesh: Res<BoidMesh>,
|
boid_mesh: Res<BoidMesh>,
|
||||||
windows: Query<&Window>,
|
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 window = windows.get_single().unwrap();
|
||||||
let half_width = window.resolution.width() / 4.;
|
let half_width = window.resolution.width() / 4.;
|
||||||
let half_height = window.resolution.height() / 2.;
|
let half_height = window.resolution.height() / 2.;
|
||||||
|
@ -190,7 +221,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..500 {
|
||||||
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);
|
||||||
|
@ -198,7 +229,7 @@ fn setup(
|
||||||
|
|
||||||
let angle = small_rng.gen_range(-TAU..=TAU);
|
let angle = small_rng.gen_range(-TAU..=TAU);
|
||||||
|
|
||||||
commands.spawn(BoidBundle {
|
let mut entity_commands = commands.spawn(BoidBundle {
|
||||||
mesh: MaterialMesh2dBundle {
|
mesh: MaterialMesh2dBundle {
|
||||||
mesh: boid_mesh.0.clone(),
|
mesh: boid_mesh.0.clone(),
|
||||||
material: boid_color.clone(),
|
material: boid_color.clone(),
|
||||||
|
@ -209,6 +240,10 @@ fn setup(
|
||||||
velocity: Velocity(Vec2::from_angle(angle) * 200.),
|
velocity: Velocity(Vec2::from_angle(angle) * 200.),
|
||||||
..default()
|
..default()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
|
entity_commands.insert(DrawGizmos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2dBundle::default());
|
||||||
|
|
Loading…
Reference in New Issue