From 79bb452cf621fde5ff885d8ed0de94c3bb898487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Lescaudey=20de=20Maneville?= Date: Wed, 12 Jul 2023 17:00:49 +0200 Subject: [PATCH] bevy 0.11 (#11) --- CHANGELOG.md | 4 +++ Cargo.toml | 12 +++------ README.md | 5 ++-- examples/2d_cloth.rs | 11 ++++---- examples/2d_cloth_cutter.rs | 29 ++++++++++----------- examples/2d_line.rs | 13 ++++------ examples/3d_cloth.rs | 11 ++++---- examples/3d_line.rs | 13 ++++------ src/lib.rs | 51 +++++++++++++++---------------------- src/resources/config.rs | 7 +++-- src/systems/debug.rs | 5 ++-- src/systems/points.rs | 12 +++++---- src/systems/sticks.rs | 30 ++++++++++------------ 13 files changed, 91 insertions(+), 112 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 306bec4..adb1782 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## Unreleased + +* `bevy` 0.10 + ## 0.5.0 * `bevy` 0.9 diff --git a/Cargo.toml b/Cargo.toml index 0322c38..d74f06b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,23 +14,19 @@ documentation = "https://docs.rs/bevy_verlet" [features] default = [] -debug = ["bevy_prototype_debug_lines"] +debug = ["bevy/bevy_gizmos", "bevy/bevy_render"] [dependencies] [dependencies.bevy] -version = "0.9" +version = "0.11" default-features = false -[dependencies.bevy_prototype_debug_lines] -version = "0.9" -optional = true - [dev-dependencies] [dev-dependencies.bevy] -version = "0.9" -features = ["render", "bevy_winit", "x11"] +version = "0.11" +features = ["bevy_render", "bevy_winit", "x11", "bevy_core_pipeline", "bevy_sprite", "bevy_pbr"] default-features = false [[example]] diff --git a/README.md b/README.md index 119dae8..04b0ea8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ - - # Verlet Integration for Bevy [![workflow](https://github.com/ManevilleF/bevy_verlet/actions/workflows/rust.yml/badge.svg)](https://github.com/ManevilleF/bevy_verlet/actions/workflows/rust.yml) @@ -10,6 +8,8 @@ [![Docs.rs](https://docs.rs/bevy_verlet/badge.svg)](https://docs.rs/bevy_verlet) [![dependency status](https://deps.rs/crate/bevy_verlet/0.5.0/status.svg)](https://deps.rs/crate/bevy_verlet) + + Simple Verlet points and sticks implementation for bevy. If you are looking for cloth physics, please check [`bevy_silk`](https://github.com/ManevilleF/bevy_silk) instead, @@ -23,6 +23,7 @@ If you are looking for cloth physics, please check [`bevy_silk`](https://github. | 0.3.x | 0.7.x | | 0.4.x | 0.8.x | | 0.5.x | 0.9.x | + | 0.6.x | 0.10.x | ## Features diff --git a/examples/2d_cloth.rs b/examples/2d_cloth.rs index da991d2..2a2d4f8 100644 --- a/examples/2d_cloth.rs +++ b/examples/2d_cloth.rs @@ -4,16 +4,15 @@ use bevy_verlet::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { - window: WindowDescriptor { + primary_window: Some(Window { title: "2D cloth".to_string(), - width: 1000., - height: 800., + resolution: (1000., 800.).into(), ..default() - }, + }), ..default() })) - .add_plugin(VerletPlugin::default()) - .add_startup_system(setup) + .add_plugins(VerletPlugin::default()) + .add_systems(Startup, setup) .run(); } diff --git a/examples/2d_cloth_cutter.rs b/examples/2d_cloth_cutter.rs index 7c7f445..6d64538 100644 --- a/examples/2d_cloth_cutter.rs +++ b/examples/2d_cloth_cutter.rs @@ -1,24 +1,19 @@ -use bevy::prelude::*; +use bevy::{prelude::*, window::PrimaryWindow}; use bevy_verlet::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { - window: WindowDescriptor { + primary_window: Some(Window { title: "2D Cloth cutter".to_string(), - width: 1400., - height: 900., + resolution: (1400., 900.).into(), ..default() - }, + }), ..default() })) - .add_plugin(VerletPlugin::default()) - .insert_resource(VerletConfig { - parallel_processing_batch_size: Some(500), - ..Default::default() - }) - .add_startup_system(setup) - .add_system(cut_sticks) + .add_plugins(VerletPlugin::default()) + .add_systems(Startup, setup) + .add_systems(Update, cut_sticks) .run(); } @@ -78,8 +73,10 @@ fn spawn_stick( } fn mouse_coords(window: &Window, position: Vec2) -> Vec2 { - let window_size = Vec2::new(window.width(), window.height()); - position - window_size / 2. + Vec2::new( + position.x - window.width() / 2.0, + window.height() / 2.0 - position.y, + ) } fn cut_sticks( @@ -87,12 +84,12 @@ fn cut_sticks( points: Query<&Transform, With>, sticks: Query<(Entity, &VerletStick)>, mouse_input: Res>, - windows: Res, + windows: Query<&Window, With>, ) { if !mouse_input.pressed(MouseButton::Left) { return; } - let window = windows.get_primary().unwrap(); + let window = windows.single(); let p = match window.cursor_position() { None => return, Some(p) => mouse_coords(window, p), diff --git a/examples/2d_line.rs b/examples/2d_line.rs index 57dea33..92e67bb 100644 --- a/examples/2d_line.rs +++ b/examples/2d_line.rs @@ -4,18 +4,15 @@ use bevy_verlet::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { - window: WindowDescriptor { + primary_window: Some(Window { title: "2D line".to_string(), - width: 1000., - height: 800., + resolution: (1000., 800.).into(), ..default() - }, + }), ..default() })) - .add_plugin(VerletPlugin::default()) - .add_startup_system(setup_camera) - .add_startup_system(setup_free_line) - .add_startup_system(setup_fixed_line) + .add_plugins(VerletPlugin::default()) + .add_systems(Startup, (setup_camera, setup_free_line, setup_fixed_line)) .run(); } diff --git a/examples/3d_cloth.rs b/examples/3d_cloth.rs index 082b16f..98eb869 100644 --- a/examples/3d_cloth.rs +++ b/examples/3d_cloth.rs @@ -4,16 +4,15 @@ use bevy_verlet::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { - window: WindowDescriptor { + primary_window: Some(Window { title: "3D cloth".to_string(), - width: 1000., - height: 800., + resolution: (1000., 800.).into(), ..default() - }, + }), ..default() })) - .add_plugin(VerletPlugin::default()) - .add_startup_system(setup) + .add_plugins(VerletPlugin::default()) + .add_systems(Startup, setup) .insert_resource(VerletConfig { sticks_computation_depth: 5, ..Default::default() diff --git a/examples/3d_line.rs b/examples/3d_line.rs index fc507a6..45a0763 100644 --- a/examples/3d_line.rs +++ b/examples/3d_line.rs @@ -4,18 +4,15 @@ use bevy_verlet::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { - window: WindowDescriptor { + primary_window: Some(Window { title: "3D line".to_string(), - width: 1000., - height: 800., + resolution: (1000., 800.).into(), ..default() - }, + }), ..default() })) - .add_plugin(VerletPlugin::default()) - .add_startup_system(setup_camera) - .add_startup_system(setup_free_line) - .add_startup_system(setup_fixed_line) + .add_plugins(VerletPlugin::default()) + .add_systems(Startup, (setup_camera, setup_free_line, setup_fixed_line)) .insert_resource(VerletConfig { sticks_computation_depth: 5, ..Default::default() diff --git a/src/lib.rs b/src/lib.rs index 48991e2..664fbaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,3 @@ -//! # Verlet Integration for Bevy -//! -//! [![workflow](https://github.com/ManevilleF/bevy_verlet/actions/workflows/rust.yml/badge.svg)](https://github.com/ManevilleF/bevy_verlet/actions/workflows/rust.yml) -//! -//! [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) -//! [![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/) -//! [![Crates.io](https://img.shields.io/crates/v/bevy_verlet.svg)](https://crates.io/crates/bevy_verlet) -//! [![Docs.rs](https://docs.rs/bevy_verlet/badge.svg)](https://docs.rs/bevy_verlet) -//! [![dependency status](https://deps.rs/crate/bevy_verlet/0.5.0/status.svg)](https://deps.rs/crate/bevy_verlet) -//! //! Simple Verlet points and sticks implementation for bevy. //! //! If you are looking for cloth physics, please check [`bevy_silk`](https://github.com/ManevilleF/bevy_silk) instead, @@ -21,6 +11,7 @@ //! | 0.3.x | 0.7.x | //! | 0.4.x | 0.8.x | //! | 0.5.x | 0.9.x | +//! | 0.6.x | 0.11.x | //! //! ## Features //! @@ -40,7 +31,7 @@ //! //! This feature will add a *system* drawing debug lines for every stick using [`bevy_prototype_debug_lines`](https://crates.io/crates/bevy_prototype_debug_lines) //! -#![forbid(missing_docs)] +#![warn(missing_docs)] #![forbid(unsafe_code)] #![warn( clippy::nursery, @@ -63,9 +54,12 @@ mod systems; use crate::verlet_time_step::VerletTimeStep; use bevy::log; use bevy::prelude::*; -use bevy::time::FixedTimestep; -#[cfg(feature = "debug")] -use bevy_prototype_debug_lines::DebugLinesPlugin; +use bevy::time::common_conditions::on_fixed_timer; +use std::time::Duration; +use systems::{ + points::update_points, + sticks::{handle_stick_constraints, update_sticks}, +}; /// Prelude pub mod prelude { @@ -73,37 +67,34 @@ pub mod prelude { pub use crate::resources::*; pub use crate::VerletPlugin; } - /// Plugin for Verlet physics #[derive(Debug, Copy, Clone, Default)] pub struct VerletPlugin { - /// Custom time step for verlet physics, if set to `None` physics will run every frame + /// Custom time step in seconds for verlet physics, if set to `None` physics will run every frame pub time_step: Option, } impl Plugin for VerletPlugin { fn build(&self, app: &mut App) { app.init_resource::(); - let system_set = SystemSet::new() - .with_system(systems::points::update_points.label("VERLET_UPDATE_POINTS")) - .with_system( - systems::sticks::update_sticks - .label("VERLET_UPDATE_STICKS") - .after("VERLET_UPDATE_POINTS"), - ) - .with_system(systems::sticks::handle_stick_constraints.after("VERLET_UPDATE_STICKS")); - let system_set = if let Some(step) = self.time_step { + 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))), + ); app.insert_resource(VerletTimeStep::FixedDeltaTime(step)); - system_set.with_run_criteria(FixedTimestep::step(step)) } else { + app.add_systems( + Update, + (update_points, update_sticks, handle_stick_constraints).chain(), + ); app.insert_resource(VerletTimeStep::DeltaTime); - system_set }; - app.add_system_set(system_set); #[cfg(feature = "debug")] { - app.add_plugin(DebugLinesPlugin::default()); - app.add_system(systems::debug::debug_draw_sticks); + app.add_systems(PostUpdate, systems::debug::debug_draw_sticks); } app.register_type::() .register_type::() diff --git a/src/resources/config.rs b/src/resources/config.rs index 8955479..4ff6e2d 100644 --- a/src/resources/config.rs +++ b/src/resources/config.rs @@ -13,9 +13,8 @@ pub struct VerletConfig { /// Sets the number of sticks computation iteration. /// The higher the value, the more precision and less elasticity for the sticks but the cost is increased pub sticks_computation_depth: u8, - /// Enables parallel computing for points, setting the parallel batch size - // TODO: Once https://github.com/bevyengine/bevy/pull/4777 is merged use automatic batching and make this a custom field - pub parallel_processing_batch_size: Option, + /// Enables parallel computing for points + pub parallel_processing: bool, } impl Default for VerletConfig { @@ -24,7 +23,7 @@ impl Default for VerletConfig { gravity: Vec3::new(0., -9.81, 0.), friction: 0.02, sticks_computation_depth: 5, - parallel_processing_batch_size: None, + parallel_processing: true, } } } diff --git a/src/systems/debug.rs b/src/systems/debug.rs index 1534365..41d66a0 100644 --- a/src/systems/debug.rs +++ b/src/systems/debug.rs @@ -1,7 +1,6 @@ use crate::{VerletPoint, VerletStick}; use bevy::log; use bevy::prelude::*; -use bevy_prototype_debug_lines::DebugLines; macro_rules! get_point_debug { ($res:expr) => { @@ -26,13 +25,13 @@ fn draw_stick( #[allow(clippy::needless_pass_by_value)] pub fn debug_draw_sticks( - mut lines: ResMut, + mut gizmos: Gizmos, sticks_query: Query<&VerletStick>, points_query: Query<&GlobalTransform, With>, ) { for stick in sticks_query.iter() { if let Some((a, b)) = draw_stick(stick, &points_query) { - lines.line(a, b, 0.); + gizmos.line(a, b, Color::WHITE); } } } diff --git a/src/systems/points.rs b/src/systems/points.rs index a74e591..47c1f4d 100644 --- a/src/systems/points.rs +++ b/src/systems/points.rs @@ -28,11 +28,13 @@ pub fn update_points( }; let gravity = config.gravity * delta_time; let friction = config.friction_coefficient(); - // TODO: Once https://github.com/bevyengine/bevy/pull/4777 is merged use automatic batching - if let Some(batch_size) = config.parallel_processing_batch_size { - points_query.par_for_each_mut(batch_size, |(mut transform, mut point)| { - update_point(&mut transform, &mut point, gravity, friction); - }); + + if config.parallel_processing { + points_query + .par_iter_mut() + .for_each_mut(|(mut transform, mut point)| { + update_point(&mut transform, &mut point, gravity, friction); + }); } else { for (mut transform, mut point) in points_query.iter_mut() { update_point(&mut transform, &mut point, gravity, friction); diff --git a/src/systems/sticks.rs b/src/systems/sticks.rs index e457455..e55b11e 100644 --- a/src/systems/sticks.rs +++ b/src/systems/sticks.rs @@ -20,7 +20,7 @@ pub fn update_sticks( match points_query.get_many_mut([stick.point_a_entity, stick.point_b_entity]) { Ok(v) => v, Err(e) => { - log::error!("Could not find point entity for stick: {}", e); + log::error!("Could not find point entities for stick: {}", e); continue; } }; @@ -84,19 +84,10 @@ pub fn handle_stick_constraints( points_query: Query<&Transform, With>, config: Res, ) { - // TODO: Once https://github.com/bevyengine/bevy/pull/4777 is merged use automatic batching - config.parallel_processing_batch_size.map_or_else( - || { - for (entity, stick, max_tension) in sticks_query.iter() { - if let Some(entity) = - handle_stick_constraint(entity, stick, **max_tension, &points_query) - { - commands.entity(entity).despawn_recursive(); - } - } - }, - |batch_size| { - sticks_query.par_for_each(batch_size, |(entity, stick, max_tension)| { + if config.parallel_processing { + sticks_query + .par_iter() + .for_each(|(entity, stick, max_tension)| { if let Some(entity) = handle_stick_constraint(entity, stick, **max_tension, &points_query) { @@ -105,6 +96,13 @@ pub fn handle_stick_constraints( }); } }); - }, - ); + } else { + for (entity, stick, max_tension) in sticks_query.iter() { + if let Some(entity) = + handle_stick_constraint(entity, stick, **max_tension, &points_query) + { + commands.entity(entity).despawn_recursive(); + } + } + } }