Skip to content

Commit

Permalink
Bevy 0.12 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed Dec 2, 2023
1 parent 437e65f commit d3dbf84
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Bevy 0.12 support (#14)
* Verlet physics always happens on `FixedUpdate` schedule (#12)

## 0.6.1

* Performance improvements (#12)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ debug = ["bevy/bevy_gizmos", "bevy/bevy_render"]
[dependencies]

[dependencies.bevy]
version = "0.11"
version = "0.12"
default-features = false

[dev-dependencies]

[dev-dependencies.bevy]
version = "0.11"
version = "0.12"
features = ["bevy_render", "bevy_winit", "x11", "bevy_core_pipeline", "bevy_sprite", "bevy_pbr"]
default-features = false

Expand Down
File renamed without changes.
23 changes: 8 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@
)]

pub use components::*;
pub use resources::*;
pub use config::*;

mod components;
mod resources;
mod config;
mod systems;

use crate::verlet_time_step::VerletTimeStep;
use bevy::{log, prelude::*, time::common_conditions::on_fixed_timer};
use bevy::{log, prelude::*, time::common_conditions::on_timer};
use std::time::Duration;
use systems::{
points::update_points,
Expand All @@ -65,33 +64,27 @@ use systems::{

/// Prelude
pub mod prelude {
pub use crate::{components::*, resources::*, VerletPlugin};
pub use crate::{components::*, config::*, VerletPlugin};
}
/// Plugin for Verlet physics
#[derive(Debug, Copy, Clone, Default)]
pub struct VerletPlugin {
/// Custom time step in seconds for verlet physics, if set to `None` physics
/// will run every frame
/// will run every [`FixedUpdate`] frame
pub time_step: Option<f64>,
}

impl Plugin for VerletPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<VerletConfig>();
let system_set = (update_points, update_sticks, handle_stick_constraints).chain();
if let Some(step) = self.time_step {
app.add_systems(
FixedUpdate,
(update_points, update_sticks, handle_stick_constraints)
.chain()
.run_if(on_fixed_timer(Duration::from_secs_f64(step))),
system_set.run_if(on_timer(Duration::from_secs_f64(step))),
);
app.insert_resource(VerletTimeStep::FixedDeltaTime(step));
} else {
app.add_systems(
Update,
(update_points, update_sticks, handle_stick_constraints).chain(),
);
app.insert_resource(VerletTimeStep::DeltaTime);
app.add_systems(FixedUpdate, system_set);
};
#[cfg(feature = "debug")]
{
Expand Down
4 changes: 0 additions & 4 deletions src/resources/mod.rs

This file was deleted.

7 changes: 0 additions & 7 deletions src/resources/verlet_time_step.rs

This file was deleted.

11 changes: 3 additions & 8 deletions src/systems/points.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
components::{VerletLocked, VerletPoint},
resources::{verlet_time_step::VerletTimeStep, VerletConfig},
config::VerletConfig,
};
use bevy::prelude::*;

Expand All @@ -18,22 +18,17 @@ fn update_point(

#[allow(clippy::needless_pass_by_value, clippy::cast_possible_truncation)]
pub fn update_points(
time_step: Res<VerletTimeStep>,
mut points_query: Query<(&mut Transform, &mut VerletPoint), Without<VerletLocked>>,
time: Res<Time>,
config: Res<VerletConfig>,
) {
let delta_time = match &*time_step {
VerletTimeStep::DeltaTime => time.delta_seconds(),
VerletTimeStep::FixedDeltaTime(dt) => (*dt * *dt) as f32,
};
let gravity = config.gravity * delta_time;
let gravity = config.gravity * time.delta_seconds();
let friction = config.friction_coefficient();

if config.parallel_processing {
points_query
.par_iter_mut()
.for_each_mut(|(mut transform, mut point)| {
.for_each(|(mut transform, mut point)| {
update_point(&mut transform, &mut point, gravity, friction);
});
} else {
Expand Down

0 comments on commit d3dbf84

Please sign in to comment.