-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallel queries Time step management fixed Improved CI
- Loading branch information
1 parent
0b7165e
commit a0b7bc2
Showing
8 changed files
with
162 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub use config::*; | ||
|
||
mod config; | ||
pub(crate) mod verlet_time_step; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[derive(Debug, Clone)] | ||
pub enum VerletTimeStep { | ||
DeltaTime, | ||
FixedDeltaTime(f64), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,47 @@ | ||
use crate::components::{VerletLocked, VerletPoint}; | ||
use crate::resources::verlet_time_step::VerletTimeStep; | ||
use crate::resources::VerletConfig; | ||
use bevy::prelude::*; | ||
use bevy::tasks::ComputeTaskPool; | ||
use std::ops::Deref; | ||
|
||
fn update_point( | ||
transform: &mut Transform, | ||
point: &mut VerletPoint, | ||
down_force: Vec3, | ||
friction: f32, | ||
) { | ||
let position = transform.translation; | ||
let velocity = if let Some(pos) = point.old_position { | ||
position - pos | ||
} else { | ||
Vec3::ZERO | ||
}; | ||
transform.translation += velocity * friction + down_force; | ||
point.old_position = Some(position); | ||
} | ||
|
||
pub fn update_points( | ||
time_step: Res<VerletTimeStep>, | ||
mut points_query: Query<(&mut Transform, &mut VerletPoint), Without<VerletLocked>>, | ||
pool: Res<ComputeTaskPool>, | ||
time: Res<Time>, | ||
config: Option<Res<VerletConfig>>, | ||
) { | ||
let config = config.map(|g| *g).unwrap_or_default(); | ||
let delta_time = time.delta_seconds(); | ||
let delta_time = match time_step.deref() { | ||
VerletTimeStep::DeltaTime => time.delta_seconds(), | ||
VerletTimeStep::FixedDeltaTime(dt) => *dt as f32, | ||
}; | ||
let down_force = config.gravity * delta_time; | ||
for (mut transform, mut point) in points_query.iter_mut() { | ||
let position = transform.translation; | ||
let velocity = if let Some(pos) = point.old_position { | ||
position - pos | ||
} else { | ||
Vec3::ZERO | ||
}; | ||
transform.translation += velocity * config.friction_coefficient() + down_force; | ||
point.old_position = Some(position); | ||
let friction = config.friction_coefficient(); | ||
if let Some(batch_size) = config.parallel_processing_batch_size { | ||
points_query.par_for_each_mut(&pool, batch_size, |(mut transform, mut point)| { | ||
update_point(&mut transform, &mut point, down_force, friction); | ||
}); | ||
} else { | ||
for (mut transform, mut point) in points_query.iter_mut() { | ||
update_point(&mut transform, &mut point, down_force, friction); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters