Skip to content

Commit

Permalink
bevy 0.11 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed Jul 12, 2023
1 parent 33118c6 commit 79bb452
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 112 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

* `bevy` 0.10

## 0.5.0

* `bevy` 0.9
Expand Down
12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<!-- cargo-sync-readme start -->

# 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)
Expand All @@ -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)

<!-- cargo-sync-readme start -->

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,
Expand All @@ -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

Expand Down
11 changes: 5 additions & 6 deletions examples/2d_cloth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
29 changes: 13 additions & 16 deletions examples/2d_cloth_cutter.rs
Original file line number Diff line number Diff line change
@@ -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();
}

Expand Down Expand Up @@ -78,21 +73,23 @@ 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(
mut commands: Commands,
points: Query<&Transform, With<VerletPoint>>,
sticks: Query<(Entity, &VerletStick)>,
mouse_input: Res<Input<MouseButton>>,
windows: Res<Windows>,
windows: Query<&Window, With<PrimaryWindow>>,
) {
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),
Expand Down
13 changes: 5 additions & 8 deletions examples/2d_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
11 changes: 5 additions & 6 deletions examples/3d_cloth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 5 additions & 8 deletions examples/3d_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
51 changes: 21 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
//!
Expand All @@ -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,
Expand All @@ -63,47 +54,47 @@ 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 {
pub use crate::components::*;
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<f64>,
}

impl Plugin for VerletPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<VerletConfig>();
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::<VerletPoint>()
.register_type::<VerletLocked>()
Expand Down
7 changes: 3 additions & 4 deletions src/resources/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
/// Enables parallel computing for points
pub parallel_processing: bool,
}

impl Default for VerletConfig {
Expand All @@ -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,
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/systems/debug.rs
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -26,13 +25,13 @@ fn draw_stick(

#[allow(clippy::needless_pass_by_value)]
pub fn debug_draw_sticks(
mut lines: ResMut<DebugLines>,
mut gizmos: Gizmos,
sticks_query: Query<&VerletStick>,
points_query: Query<&GlobalTransform, With<VerletPoint>>,
) {
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);
}
}
}
12 changes: 7 additions & 5 deletions src/systems/points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 79bb452

Please sign in to comment.