Skip to content

Commit

Permalink
Migrate from bevy_xpbd to avian physics
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrea-c committed Jul 9, 2024
1 parent 58fb04c commit 53e9371
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 63 deletions.
66 changes: 35 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ bevy = "0.14"
bytemuck = "1.14.3"
rand = "0.8.5"
bevy_utilitarian = { version = "0.5" }
bevy_xpbd_3d = { version = "0.5", features = ["serialize"], optional = true }
avian3d = { version = "0.1", features = ["serialize"], optional = true }
serde = { version = "1.0", features = ["derive"] }

[features]
default = ["physics_xpbd"]
physics_xpbd = ["dep:bevy_xpbd_3d"]
default = ["physics_avian"]
physics_avian = ["dep:avian3d"]

[[example]]
name = "collision"
required-features = ["physics_xpbd"]
required-features = ["physics_avian"]

[[example]]
name = "stress_test"
required-features = ["physics_xpbd"]
required-features = ["physics_avian"]

[[example]]
name = "stress_test_collision"
required-features = ["physics_xpbd"]
required-features = ["physics_avian"]
2 changes: 1 addition & 1 deletion examples/collision.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use avian3d::prelude::*;
use bevy::{core_pipeline::bloom::BloomSettings, prelude::*};
use bevy_firework::{
core::{BlendMode, ParticleCollisionSettings, ParticleSpawnerBundle, ParticleSpawnerSettings},
emission_shape::EmissionShape,
plugin::ParticleSystemPlugin,
};
use bevy_utilitarian::prelude::*;
use bevy_xpbd_3d::prelude::*;
use std::f32::consts::PI;

fn main() {
Expand Down
10 changes: 5 additions & 5 deletions examples/pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn main() {
app.add_plugins(ParticleSystemPlugin)
.add_systems(Startup, setup)
.add_systems(Update, (adjust_time_scale, rotate_point_light));
#[cfg(feature = "physics_xpbd")]
app.add_plugins(bevy_xpbd_3d::prelude::PhysicsPlugins::default());
#[cfg(feature = "physics_avian")]
app.add_plugins(avian3d::prelude::PhysicsPlugins::default());

app.run();
}
Expand Down Expand Up @@ -63,10 +63,10 @@ fn setup(
.spawn(ParticleSpawnerBundle::from_settings(
ParticleSpawnerSettings {
one_shot: false,
rate: 80.0,
rate: 150.0,
emission_shape: EmissionShape::Circle {
normal: Vec3::Y,
radius: 2.0,
radius: 3.5,
},
lifetime: RandF32::constant(5.),
inherit_parent_velocity: true,
Expand All @@ -75,7 +75,7 @@ fn setup(
scale_curve: ParamCurve::linear_uniform(vec![1., 2.]),
color: Gradient::linear(vec![
(0., LinearRgba::new(0.6, 0.3, 0., 0.)),
(0.1, LinearRgba::new(0.6, 0.3, 0., 0.5)),
(0.1, LinearRgba::new(0.6, 0.3, 0., 0.35)),
(1., LinearRgba::new(0.6, 0.3, 0., 0.0)),
]),
blend_mode: BlendMode::Blend,
Expand Down
4 changes: 2 additions & 2 deletions examples/sparks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn main() {
.add_systems(Startup, setup)
.add_systems(Update, adjust_time_scale);

#[cfg(feature = "physics_xpbd")]
app.add_plugins(bevy_xpbd_3d::prelude::PhysicsPlugins::default());
#[cfg(feature = "physics_avian")]
app.add_plugins(avian3d::prelude::PhysicsPlugins::default());

app.run();
}
Expand Down
3 changes: 2 additions & 1 deletion examples/stress_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use avian3d::PhysicsPlugins;
// use avian3d::plugins::PhysicsPlugins;
use bevy::{
core_pipeline::bloom::BloomSettings,
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
Expand All @@ -9,7 +11,6 @@ use bevy_firework::{
plugin::ParticleSystemPlugin,
};
use bevy_utilitarian::prelude::*;
use bevy_xpbd_3d::plugins::PhysicsPlugins;
use std::f32::consts::PI;

fn main() {
Expand Down
4 changes: 1 addition & 3 deletions examples/stress_test_collision.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use avian3d::{collision::Collider, spatial_query::SpatialQueryFilter, PhysicsPlugins};
use bevy::{
core_pipeline::bloom::BloomSettings,
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
Expand All @@ -12,9 +13,6 @@ use bevy_firework::{
plugin::ParticleSystemPlugin,
};
use bevy_utilitarian::prelude::*;
use bevy_xpbd_3d::plugins::{
collision::Collider, spatial_query::SpatialQueryFilter, PhysicsPlugins,
};
use std::f32::consts::PI;

fn main() {
Expand Down
24 changes: 12 additions & 12 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use bevy_utilitarian::prelude::*;
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[cfg(feature = "physics_xpbd")]
use bevy_xpbd_3d::prelude::*;
#[cfg(feature = "physics_avian")]
use avian3d::prelude::*;

pub const DEFAULT_MESH: Handle<Mesh> =
Handle::weak_from_u128(164408926256276437310893021157813788765);
Expand Down Expand Up @@ -81,7 +81,7 @@ pub struct ParticleSpawnerSettings {
pub blend_mode: BlendMode,
/// Whether to use the PBR pipeline for the particle
pub pbr: bool,
#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
/// If Some, particles will collide with the scene according to the provided parameters
/// If None, no particle collision will occur.
pub collision_settings: Option<ParticleCollisionSettings>,
Expand All @@ -106,7 +106,7 @@ impl Default for ParticleSpawnerSettings {
blend_mode: BlendMode::Blend,
linear_drag: 0.,
pbr: false,
#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
collision_settings: None,
fade_edge: 0.7,
fade_scene: 1.,
Expand All @@ -115,7 +115,7 @@ impl Default for ParticleSpawnerSettings {
}
}

#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
#[derive(Reflect, Clone, Serialize, Deserialize)]
pub struct ParticleCollisionSettings {
pub restitution: f32,
Expand All @@ -124,7 +124,7 @@ pub struct ParticleCollisionSettings {
pub filter: SpatialQueryFilter,
}

#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
impl std::fmt::Debug for ParticleCollisionSettings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
Expand Down Expand Up @@ -294,7 +294,7 @@ pub fn spawn_particles(
pub fn update_particles(
mut particle_systems_query: Query<(&ParticleSpawnerSettings, &mut ParticleSpawnerData)>,
time: Res<Time>,
#[cfg(feature = "physics_xpbd")] spatial_query: SpatialQuery,
#[cfg(feature = "physics_avian")] spatial_query: SpatialQuery,
) {
particle_systems_query
.par_iter_mut()
Expand All @@ -315,7 +315,7 @@ pub fn update_particles(

particle.scale = particle.initial_scale * scale_factor;

#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
let (new_pos, new_vel) =
if let Some(collision_settigs) = &settings.collision_settings {
particle_collision(
Expand All @@ -331,7 +331,7 @@ pub fn update_particles(
particle.velocity,
)
};
#[cfg(not(feature = "physics_xpbd"))]
#[cfg(not(feature = "physics_avian"))]
let (new_pos, new_vel) = (
particle.position + particle.velocity * time.delta_seconds(),
particle.velocity,
Expand Down Expand Up @@ -372,7 +372,7 @@ pub fn setup_default_mesh(mut meshes: ResMut<Assets<Mesh>>) {
);
}

#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
pub fn sync_parent_velocity(
velocity: Query<(
Entity,
Expand Down Expand Up @@ -405,13 +405,13 @@ pub fn sync_parent_velocity(
}
}

#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
/// All quantities are in world-space
fn linear_velocity_at_point(linvel: Vec3, angvel: Vec3, point: Vec3, center_of_mass: Vec3) -> Vec3 {
linvel + angvel.cross(point - center_of_mass)
}

#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
fn particle_collision(
mut pos: Vec3,
mut vel: Vec3,
Expand Down
4 changes: 2 additions & 2 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
};
use bevy::{asset::load_internal_asset, prelude::*, transform::TransformSystem};

#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
use super::core::sync_parent_velocity;

pub const PARTICLE_SHADER_HANDLE: Handle<Shader> =
Expand Down Expand Up @@ -37,7 +37,7 @@ impl Plugin for ParticleSystemPlugin {
(create_spawner_data, propagate_particle_spawner_modifier),
apply_deferred,
sync_spawner_data,
#[cfg(feature = "physics_xpbd")]
#[cfg(feature = "physics_avian")]
sync_parent_velocity,
)
.chain(),
Expand Down

0 comments on commit 53e9371

Please sign in to comment.