Move physics parts to a physics module

This commit is contained in:
2025-09-02 10:06:08 -05:00
parent 532025b42f
commit 3eb23fb4bf
3 changed files with 23 additions and 18 deletions

View File

@@ -1,8 +1,12 @@
pub mod physics;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_spatial::{ use bevy_spatial::{
kdtree::KDTree2, AutomaticUpdate, SpatialAccess, SpatialStructure, TransformMode, kdtree::KDTree2, AutomaticUpdate, SpatialAccess, SpatialStructure, TransformMode,
}; };
use crate::birdoids::physics::{apply_velocity, Force, Velocity};
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.0; const TURN_FACTOR: f32 = 1.0;
@@ -47,14 +51,8 @@ pub(crate) struct Boid;
#[derive(Component)] #[derive(Component)]
struct PlayerBoid; struct PlayerBoid;
#[derive(Component, Deref, DerefMut)]
pub(crate) struct Velocity(Vec3);
#[derive(Component, Default, Deref, DerefMut, PartialEq, Debug)]
pub(crate) struct Force(Vec3);
#[derive(Component)] #[derive(Component)]
pub(crate) struct TrackedByKdTree; pub struct TrackedByKdTree;
#[derive(Bundle)] #[derive(Bundle)]
struct BoidBundle { struct BoidBundle {
@@ -136,15 +134,6 @@ fn turn_if_edge(
} }
} }
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity, &mut Force)>, time: Res<Time>) {
for (mut transform, velocity, mut acceleration) in &mut query {
let delta_v = **acceleration * time.delta_secs();
**acceleration = Vec3::ZERO;
let delta_position = (**velocity + delta_v) * time.delta_secs();
transform.translation += delta_position;
}
}
fn check_keyboard( fn check_keyboard(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>, mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
@@ -324,7 +313,7 @@ mod tests {
use crate::birdoids::{cohesive_force, separation_force}; use crate::birdoids::{cohesive_force, separation_force};
use super::{Force, BOID_VIEW_RANGE}; use super::{physics::Force, BOID_VIEW_RANGE};
// forces are relative to the boid's view range, so all // forces are relative to the boid's view range, so all
// distances need to be fractions of that // distances need to be fractions of that

16
src/birdoids/physics.rs Normal file
View File

@@ -0,0 +1,16 @@
use bevy::prelude::*;
#[derive(Component, Deref, DerefMut)]
pub struct Velocity(pub Vec3);
#[derive(Component, Default, Deref, DerefMut, PartialEq, Debug)]
pub struct Force(pub Vec3);
pub fn apply_velocity(mut query: Query<(&mut Transform, &Velocity, &mut Force)>, time: Res<Time>) {
for (mut transform, velocity, mut acceleration) in &mut query {
let delta_v = **acceleration * time.delta_secs();
**acceleration = Vec3::ZERO;
let delta_position = (**velocity + delta_v) * time.delta_secs();
transform.translation += delta_position;
}
}

View File

@@ -2,7 +2,7 @@ use bevy::{prelude::*, window::PrimaryWindow};
use bevy_spatial::{kdtree::KDTree2, SpatialAccess}; use bevy_spatial::{kdtree::KDTree2, SpatialAccess};
use crate::birdoids::{ use crate::birdoids::{
center_of_boids, velocity_of_boids, Boid, Force, TrackedByKdTree, Velocity, center_of_boids, physics::Force, physics::Velocity, velocity_of_boids, Boid, TrackedByKdTree,
}; };
const SCANRADIUS: f32 = 50.0; const SCANRADIUS: f32 = 50.0;