Compare commits
No commits in common. "7d5335fb2e8e4957737b1bc718e554fad8ff6799" and "b2b59d68d5b86cb4c78f80e1a5e3860ab140c7ba" have entirely different histories.
7d5335fb2e
...
b2b59d68d5
|
@ -1209,7 +1209,6 @@ name = "boids-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"rand",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2963,15 +2962,6 @@ dependencies = [
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ppv-lite86"
|
|
||||||
version = "0.2.20"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
|
||||||
dependencies = [
|
|
||||||
"zerocopy",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "presser"
|
name = "presser"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
@ -3023,18 +3013,6 @@ version = "0.8.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
|
||||||
"rand_chacha",
|
|
||||||
"rand_core",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rand_chacha"
|
|
||||||
version = "0.3.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
|
||||||
dependencies = [
|
|
||||||
"ppv-lite86",
|
|
||||||
"rand_core",
|
"rand_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -3043,9 +3021,6 @@ name = "rand_core"
|
||||||
version = "0.6.4"
|
version = "0.6.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
dependencies = [
|
|
||||||
"getrandom",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "range-alloc"
|
name = "range-alloc"
|
||||||
|
@ -4307,7 +4282,6 @@ version = "0.7.35"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder",
|
|
||||||
"zerocopy-derive",
|
"zerocopy-derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.14.1"
|
bevy = "0.14.1"
|
||||||
rand = { version = "0.8.5", features = ["small_rng"] }
|
|
||||||
|
|
||||||
# Enable a small amount of optimization in the dev profile.
|
# Enable a small amount of optimization in the dev profile.
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
|
70
src/main.rs
70
src/main.rs
|
@ -1,26 +1,15 @@
|
||||||
#![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::*, sprite::MaterialMesh2dBundle};
|
||||||
use boid_mesh::BoidMesh;
|
use boid_mesh::BoidMesh;
|
||||||
use rand::{Rng, SeedableRng};
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.init_resource::<BoidMesh>()
|
.init_resource::<BoidMesh>()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain())
|
.add_systems(Startup, (boid_mesh::build_boid_mesh, setup).chain())
|
||||||
.add_systems(
|
.add_systems(Update, (move_boids, boundary::boid_border_teleport).chain())
|
||||||
Update,
|
|
||||||
(
|
|
||||||
(seperation, cohesion, alignment),
|
|
||||||
move_boids,
|
|
||||||
boundary::boid_border_teleport,
|
|
||||||
)
|
|
||||||
.chain(),
|
|
||||||
)
|
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,19 +20,19 @@ struct Boid;
|
||||||
struct Velocity(Vec2);
|
struct Velocity(Vec2);
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
struct SeperationAcceleration(Vec2);
|
struct SeperationCache(Vec2);
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
struct CohesionAcceleration(Vec2);
|
struct CohesionCache(Vec2);
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
struct AlignmentAcceleration(Vec2);
|
struct AlignmentCache(Vec2);
|
||||||
|
|
||||||
#[derive(Bundle, Default)]
|
#[derive(Bundle, Default)]
|
||||||
struct BoidAccelerationBundle {
|
struct BoidCacheBundle {
|
||||||
s_cache: SeperationAcceleration,
|
s_cache: SeperationCache,
|
||||||
c_cache: CohesionAcceleration,
|
c_cache: CohesionCache,
|
||||||
a_cache: AlignmentAcceleration,
|
a_cache: AlignmentCache,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Bundle, Default)]
|
#[derive(Bundle, Default)]
|
||||||
|
@ -51,61 +40,34 @@ struct BoidBundle {
|
||||||
mesh: MaterialMesh2dBundle<ColorMaterial>,
|
mesh: MaterialMesh2dBundle<ColorMaterial>,
|
||||||
velocity: Velocity,
|
velocity: Velocity,
|
||||||
boid: Boid,
|
boid: Boid,
|
||||||
cache: BoidAccelerationBundle,
|
cache: BoidCacheBundle,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn seperation(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
|
fn seperation(mut boids: Query<(&Transform, &mut SeperationCache), With<Boid>>) {}
|
||||||
|
|
||||||
fn cohesion(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
|
|
||||||
|
|
||||||
fn alignment(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With<Boid>>) {}
|
|
||||||
|
|
||||||
fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>, time: Res<Time>) {
|
fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>, time: Res<Time>) {
|
||||||
boids.par_iter_mut().for_each(|(mut transform, velocity)| {
|
for (mut transform, velocity) in &mut boids {
|
||||||
transform.translation += velocity.0.extend(0.) * time.delta_seconds();
|
transform.translation += velocity.0.extend(0.) * time.delta_seconds();
|
||||||
transform.rotation =
|
}
|
||||||
Quat::from_rotation_z(velocity.0.to_angle() - std::f32::consts::FRAC_PI_2);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(
|
fn setup(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
boid_mesh: Res<BoidMesh>,
|
boid_mesh: Res<BoidMesh>,
|
||||||
windows: Query<&Window>,
|
|
||||||
) {
|
) {
|
||||||
let window = windows.get_single().unwrap();
|
commands.spawn(Camera2dBundle::default());
|
||||||
let half_width = window.resolution.width() / 4.;
|
|
||||||
let half_height = window.resolution.height() / 2.;
|
|
||||||
|
|
||||||
let boid_color = materials.add(Color::from(tailwind::NEUTRAL_50));
|
let boid_color = materials.add(Color::from(tailwind::NEUTRAL_50));
|
||||||
|
|
||||||
let mut small_rng = rand::rngs::SmallRng::from_entropy();
|
|
||||||
let lower_width = -half_width;
|
|
||||||
let upper_width = half_width;
|
|
||||||
let lower_height = -half_height;
|
|
||||||
let upper_height = half_height;
|
|
||||||
|
|
||||||
for i in 0..1000 {
|
|
||||||
use std::f32::consts::TAU;
|
|
||||||
|
|
||||||
let x = small_rng.gen_range(lower_width..=upper_width);
|
|
||||||
let y = small_rng.gen_range(lower_height..=upper_height);
|
|
||||||
|
|
||||||
let angle = small_rng.gen_range(-TAU..=TAU);
|
|
||||||
|
|
||||||
commands.spawn(BoidBundle {
|
commands.spawn(BoidBundle {
|
||||||
mesh: MaterialMesh2dBundle {
|
mesh: MaterialMesh2dBundle {
|
||||||
mesh: boid_mesh.0.clone(),
|
mesh: boid_mesh.0.clone(),
|
||||||
material: boid_color.clone(),
|
material: boid_color,
|
||||||
transform: Transform::from_scale(Vec3::splat(2.5))
|
transform: Transform::from_scale(Vec3::splat(2.5)),
|
||||||
.with_translation(Vec3::new(x, y, 0.)),
|
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
velocity: Velocity(Vec2::from_angle(angle) * 200.),
|
velocity: Velocity(Vec2::Y * 100.),
|
||||||
..default()
|
..default()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.spawn(Camera2dBundle::default());
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue