Skip to content

Commit

Permalink
Merge remote-tracking branch 'dimforge/master' into custom-shape
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Zhixing committed Jul 27, 2024
2 parents a7cf839 + c539f29 commit 169dbb3
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 16 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## Unreleased

### Modified

- Update from rapier `0.21` to rapier `0.22`,
see [rapier's changelog](https://github.com/dimforge/rapier/blob/master/CHANGELOG.md).

### Fix

- Fix a crash when using `TimestepMode::Interpolated` and removing colliders
during a frame which would not run a simulation step.

### Added

- Added a `TriMeshFlags` parameter for `ComputedColliderShape`,
Expand Down
6 changes: 3 additions & 3 deletions bevy_rapier_benches3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rapier3d = { features = ["profiler"], version = "0.21" }
bevy_rapier3d = { version = "0.27.0-rc.1", path = "../bevy_rapier3d" }
bevy = { version = "0.14.0-rc.3", default-features = false }
rapier3d = { features = ["profiler"], version = "0.22" }
bevy_rapier3d = { version = "0.27", path = "../bevy_rapier3d" }
bevy = { version = "0.14", default-features = false }
16 changes: 8 additions & 8 deletions src/geometry/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,17 @@ impl Default for Friction {
}

impl Friction {
/// Creates a `Friction` component from the given friction coefficient, and using the default
/// `CoefficientCombineRule::Average` coefficient combine rule.
/// Creates a [`Friction`] component from the given friction coefficient, and using the default
/// [`CoefficientCombineRule::Average`] coefficient combine rule.
pub const fn new(coefficient: f32) -> Self {
Self {
coefficient,
combine_rule: CoefficientCombineRule::Average,
}
}

/// Creates a `Friction` component from the given friction coefficient, and using the default
/// `CoefficientCombineRule::Average` coefficient combine rule.
/// Creates a [`Friction`] component from the given friction coefficient, and using the default
/// [`CoefficientCombineRule::Average`] coefficient combine rule.
pub const fn coefficient(coefficient: f32) -> Self {
Self {
coefficient,
Expand All @@ -204,17 +204,17 @@ pub struct Restitution {
}

impl Restitution {
/// Creates a `Restitution` component from the given restitution coefficient, and using the default
/// `CoefficientCombineRule::Average` coefficient combine rule.
/// Creates a [`Restitution`] component from the given restitution coefficient, and using the default
/// [`CoefficientCombineRule::Average`] coefficient combine rule.
pub const fn new(coefficient: f32) -> Self {
Self {
coefficient,
combine_rule: CoefficientCombineRule::Average,
}
}

/// Creates a `Restitution` component from the given restitution coefficient, and using the default
/// `CoefficientCombineRule::Average` coefficient combine rule.
/// Creates a [`Restitution`] component from the given restitution coefficient, and using the default
/// [`CoefficientCombineRule::Average`] coefficient combine rule.
pub const fn coefficient(coefficient: f32) -> Self {
Self {
coefficient,
Expand Down
2 changes: 2 additions & 0 deletions src/geometry/shape_views/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ macro_rules! impl_ref_methods(
///
/// The normal points such that it is collinear to `AB × AC` (where `×` denotes the cross
/// product).
#[cfg(feature = "dim3")]
#[inline]
pub fn normal(&self) -> Option<Vect> {
self.raw.normal().map(|n| (*n).into())
Expand All @@ -46,6 +47,7 @@ macro_rules! impl_ref_methods(
///
/// The vector points such that it is collinear to `AB × AC` (where `×` denotes the cross
/// product).
#[cfg(feature = "dim3")]
#[inline]
pub fn scaled_normal(&self) -> Vect {
self.raw.scaled_normal().into()
Expand Down
80 changes: 79 additions & 1 deletion src/pipeline/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ impl<'a> EventHandler for EventQueue<'a> {

#[cfg(test)]
mod test {
use bevy::time::{TimePlugin, TimeUpdateStrategy};
use bevy::{
app::{App, Startup, Update},
prelude::{Commands, Component, Entity, Query, With},
time::{TimePlugin, TimeUpdateStrategy},
transform::{bundles::TransformBundle, components::Transform, TransformPlugin},
MinimalPlugins,
};
use systems::tests::HeadlessRenderPlugin;

use crate::{plugin::*, prelude::*};
Expand Down Expand Up @@ -231,4 +237,76 @@ mod test {
));
}
}

#[test]
pub fn spam_remove_rapier_entity_interpolated() {
let mut app = App::new();
app.add_plugins((
HeadlessRenderPlugin,
MinimalPlugins,
TransformPlugin,
RapierPhysicsPlugin::<NoUserData>::default(),
))
.insert_resource(RapierConfiguration {
timestep_mode: TimestepMode::Interpolated {
dt: 1.0 / 30.0,
time_scale: 1.0,
substeps: 2,
},
..RapierConfiguration::new(1f32)
})
.add_systems(Startup, setup_physics)
.add_systems(Update, remove_collider);
// Simulates 60 updates per seconds
app.insert_resource(TimeUpdateStrategy::ManualDuration(
std::time::Duration::from_secs_f32(1f32 / 60f32),
));

for i in 0..100 {
dbg!(i);
app.update();
}
return;

#[derive(Component)]
pub struct ToRemove;

#[cfg(feature = "dim3")]
fn cuboid(hx: Real, hy: Real, hz: Real) -> Collider {
Collider::cuboid(hx, hy, hz)
}
#[cfg(feature = "dim2")]
fn cuboid(hx: Real, hy: Real, _hz: Real) -> Collider {
Collider::cuboid(hx, hy)
}
pub fn setup_physics(mut commands: Commands) {
for _i in 0..100 {
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0)),
RigidBody::Dynamic,
cuboid(0.5, 0.5, 0.5),
ActiveEvents::all(),
ToRemove,
));
}
/*
* Ground
*/
let ground_size = 5.1;
let ground_height = 0.1;
let starting_y = -0.5 - ground_height;

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, starting_y, 0.0)),
cuboid(ground_size, ground_height, ground_size),
));
}

fn remove_collider(mut commands: Commands, query: Query<Entity, With<ToRemove>>) {
let Some(entity) = query.iter().next() else {
return;
};
commands.entity(entity).despawn();
}
}
}
4 changes: 2 additions & 2 deletions src/plugin/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::math::{Real, Vect};
use crate::plugin::RapierContext;

#[cfg(doc)]
use rapier::dynamics::IntegrationParameters;
use {crate::prelude::TransformInterpolation, rapier::dynamics::IntegrationParameters};

/// Difference between simulation and rendering time
#[derive(Resource, Default)]
Expand Down Expand Up @@ -39,7 +39,7 @@ pub enum TimestepMode {
},
/// Use a fixed timestep equal to `IntegrationParameters::dt`, but don't step if the
/// physics simulation advanced by a time greater than the real-world elapsed time multiplied by `time_scale`.
/// Rigid-bodies with a component `InterpolatedTransform` attached will use interpolation to
/// Rigid-bodies with a component [`TransformInterpolation`] attached will use interpolation to
/// estimate the rigid-bodies position in-between steps.
Interpolated {
/// The physics simulation will be advanced by this total amount at each Bevy tick, unless
Expand Down
8 changes: 8 additions & 0 deletions src/plugin/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ impl RapierContext {
.or_else(|| event_queue.as_ref().map(|q| q as &dyn EventHandler))
.unwrap_or(&() as &dyn EventHandler);

let mut executed_steps = 0;
match timestep_mode {
TimestepMode::Interpolated {
dt,
Expand Down Expand Up @@ -271,6 +272,7 @@ impl RapierContext {
hooks,
events,
);
executed_steps += 1;
}

sim_to_render_time.diff -= dt;
Expand Down Expand Up @@ -302,6 +304,7 @@ impl RapierContext {
hooks,
events,
);
executed_steps += 1;
}
}
TimestepMode::Fixed { dt, substeps } => {
Expand All @@ -326,9 +329,14 @@ impl RapierContext {
hooks,
events,
);
executed_steps += 1;
}
}
}

if executed_steps > 0 {
self.deleted_colliders.clear();
}
}

/// This method makes sure tha the rigid-body positions have been propagated to
Expand Down
1 change: 0 additions & 1 deletion src/plugin/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub fn step_simulation<Hooks>(
&mut sim_to_render_time,
Some(interpolation_query),
);
context.deleted_colliders.clear();
} else {
context.propagate_modified_body_positions_to_colliders();
}
Expand Down
5 changes: 4 additions & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use rapier::pipeline::{DebugRenderBackend, DebugRenderObject, DebugRenderPipelin
pub use rapier::pipeline::{DebugRenderMode, DebugRenderStyle};
use std::fmt::Debug;

#[cfg(doc)]
use crate::prelude::Collider;

/// The color of a collider when using the debug-renderer.
///
/// Insert this component alongside the collider component to
/// Insert this component alongside the [`Collider`] component to
/// force to a specific value the color used to render the
/// collider.
#[derive(Copy, Clone, Component, PartialEq, Debug)]
Expand Down

0 comments on commit 169dbb3

Please sign in to comment.