Added bevy_spatial for boid interactions

I'm finally starting on the actual Boids flocking algorithm parts.
I don't want to iterate over all the things, and Bevy doesn't seem to
have fast collision testing yet, so I've reached for bevy_spatial to
track my Boids.
This commit is contained in:
2024-07-09 11:56:52 -05:00
parent ecbfea04be
commit 3a680a4ea2
3 changed files with 119 additions and 3 deletions

80
Cargo.lock generated
View File

@@ -308,6 +308,7 @@ name = "bevy-testing"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy_spatial",
] ]
[[package]] [[package]]
@@ -903,6 +904,18 @@ dependencies = [
"uuid", "uuid",
] ]
[[package]]
name = "bevy_spatial"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f0857f70fec5515c9ec93d23f5d4bf9fd9d9863198ce64c5878de5fab1a852d"
dependencies = [
"bevy",
"kd-tree",
"num-traits",
"typenum",
]
[[package]] [[package]]
name = "bevy_sprite" name = "bevy_sprite"
version = "0.14.0" version = "0.14.0"
@@ -1521,6 +1534,25 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.20" version = "0.8.20"
@@ -2178,6 +2210,19 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "kd-tree"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f89ee4e60e82cf7024e5e94618c646fbf61ce7501dc5898b3d12786442d3682"
dependencies = [
"num-traits",
"ordered-float",
"paste",
"rayon",
"typenum",
]
[[package]] [[package]]
name = "khronos-egl" name = "khronos-egl"
version = "6.0.0" version = "6.0.0"
@@ -2808,6 +2853,15 @@ dependencies = [
"libredox", "libredox",
] ]
[[package]]
name = "ordered-float"
version = "4.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19ff2cf528c6c03d9ed653d6c4ce1dc0582dc4af309790ad92f07c1cd551b0be"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "overload" name = "overload"
version = "0.1.1" version = "0.1.1"
@@ -3028,6 +3082,26 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "rectangle-pack" name = "rectangle-pack"
version = "0.4.2" version = "0.4.2"
@@ -3512,6 +3586,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "059d83cc991e7a42fc37bd50941885db0888e34209f8cfd9aab07ddec03bc9cf" checksum = "059d83cc991e7a42fc37bd50941885db0888e34209f8cfd9aab07ddec03bc9cf"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"

View File

@@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
bevy = "0.14.0" bevy = "0.14.0"
bevy_spatial = "0.9.0"
[profile.dev] [profile.dev]
opt-level = 1 opt-level = 1

View File

@@ -1,16 +1,33 @@
use std::time::Duration;
use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use bevy_spatial::{
AutomaticUpdate,
SpatialAccess,
TransformMode,
kdtree::KDTree2,
};
const BACKGROUND_COLOR: Color = Color::srgb(0.4, 0.4, 0.4); const BACKGROUND_COLOR: Color = Color::srgb(0.4, 0.4, 0.4);
const PLAYERBOID_COLOR: Color = Color::srgb(1.0, 0.0, 0.0); const PLAYERBOID_COLOR: Color = Color::srgb(1.0, 0.0, 0.0);
const TURN_FACTOR: f32 = 1.; const TURN_FACTOR: f32 = 1.;
const BOID_VIEW_RANGE: f32 = 50.0;
pub struct BoidsPlugin; pub struct BoidsPlugin;
impl Plugin for BoidsPlugin { impl Plugin for BoidsPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(ClearColor(BACKGROUND_COLOR)) app
.add_plugins(AutomaticUpdate::<TrackedByKdTree>::new()
.with_frequency(Duration::from_secs_f32(0.3))
.with_transform(TransformMode::GlobalTransform))
.insert_resource(ClearColor(BACKGROUND_COLOR))
.add_systems(Startup, (spawn_camera, spawn_boids)) .add_systems(Startup, (spawn_camera, spawn_boids))
.add_systems(FixedUpdate, (apply_velocity, turn_if_edge, check_keyboard)); .add_systems(FixedUpdate, (
apply_velocity,
turn_if_edge,
check_keyboard,
cohesion,
));
} }
} }
@@ -25,10 +42,14 @@ struct PlayerBoid;
#[derive(Component, Deref, DerefMut)] #[derive(Component, Deref, DerefMut)]
struct Velocity(Vec3); struct Velocity(Vec3);
#[derive(Component)]
struct TrackedByKdTree;
#[derive(Bundle)] #[derive(Bundle)]
struct BoidBundle { struct BoidBundle {
boid: Boid, boid: Boid,
velocity: Velocity, velocity: Velocity,
spatial: TrackedByKdTree,
} }
impl BoidBundle { impl BoidBundle {
@@ -36,6 +57,7 @@ impl BoidBundle {
Self { Self {
boid: Boid, boid: Boid,
velocity: Velocity(vel), velocity: Velocity(vel),
spatial: TrackedByKdTree,
} }
} }
} }
@@ -132,3 +154,16 @@ fn check_keyboard(
**pvelocity = **pvelocity + dir.extend(0.0); **pvelocity = **pvelocity + dir.extend(0.0);
} }
fn cohesion(
spatial_tree: Res<KDTree2<TrackedByKdTree>>,
mut query: Query<&mut Boid, With<TrackedByKdTree>>,
player: Query<&Transform, With<PlayerBoid>>,
) {
let player_transform = player.get_single().unwrap();
let neighbors = spatial_tree.within_distance(
player_transform.translation.xy(),
BOID_VIEW_RANGE
);
}