Skip to content

Commit 169dbb3

Browse files
committed
Merge remote-tracking branch 'dimforge/master' into custom-shape
2 parents a7cf839 + c539f29 commit 169dbb3

File tree

9 files changed

+116
-16
lines changed

9 files changed

+116
-16
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
## Unreleased
44

5+
### Modified
6+
7+
- Update from rapier `0.21` to rapier `0.22`,
8+
see [rapier's changelog](https://github.com/dimforge/rapier/blob/master/CHANGELOG.md).
9+
10+
### Fix
11+
12+
- Fix a crash when using `TimestepMode::Interpolated` and removing colliders
13+
during a frame which would not run a simulation step.
14+
515
### Added
616

717
- Added a `TriMeshFlags` parameter for `ComputedColliderShape`,

bevy_rapier_benches3d/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ edition = "2021"
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12-
rapier3d = { features = ["profiler"], version = "0.21" }
13-
bevy_rapier3d = { version = "0.27.0-rc.1", path = "../bevy_rapier3d" }
14-
bevy = { version = "0.14.0-rc.3", default-features = false }
12+
rapier3d = { features = ["profiler"], version = "0.22" }
13+
bevy_rapier3d = { version = "0.27", path = "../bevy_rapier3d" }
14+
bevy = { version = "0.14", default-features = false }

src/geometry/collider.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ impl Default for Friction {
171171
}
172172

173173
impl Friction {
174-
/// Creates a `Friction` component from the given friction coefficient, and using the default
175-
/// `CoefficientCombineRule::Average` coefficient combine rule.
174+
/// Creates a [`Friction`] component from the given friction coefficient, and using the default
175+
/// [`CoefficientCombineRule::Average`] coefficient combine rule.
176176
pub const fn new(coefficient: f32) -> Self {
177177
Self {
178178
coefficient,
179179
combine_rule: CoefficientCombineRule::Average,
180180
}
181181
}
182182

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

206206
impl Restitution {
207-
/// Creates a `Restitution` component from the given restitution coefficient, and using the default
208-
/// `CoefficientCombineRule::Average` coefficient combine rule.
207+
/// Creates a [`Restitution`] component from the given restitution coefficient, and using the default
208+
/// [`CoefficientCombineRule::Average`] coefficient combine rule.
209209
pub const fn new(coefficient: f32) -> Self {
210210
Self {
211211
coefficient,
212212
combine_rule: CoefficientCombineRule::Average,
213213
}
214214
}
215215

216-
/// Creates a `Restitution` component from the given restitution coefficient, and using the default
217-
/// `CoefficientCombineRule::Average` coefficient combine rule.
216+
/// Creates a [`Restitution`] component from the given restitution coefficient, and using the default
217+
/// [`CoefficientCombineRule::Average`] coefficient combine rule.
218218
pub const fn coefficient(coefficient: f32) -> Self {
219219
Self {
220220
coefficient,

src/geometry/shape_views/triangle.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ macro_rules! impl_ref_methods(
3737
///
3838
/// The normal points such that it is collinear to `AB × AC` (where `×` denotes the cross
3939
/// product).
40+
#[cfg(feature = "dim3")]
4041
#[inline]
4142
pub fn normal(&self) -> Option<Vect> {
4243
self.raw.normal().map(|n| (*n).into())
@@ -46,6 +47,7 @@ macro_rules! impl_ref_methods(
4647
///
4748
/// The vector points such that it is collinear to `AB × AC` (where `×` denotes the cross
4849
/// product).
50+
#[cfg(feature = "dim3")]
4951
#[inline]
5052
pub fn scaled_normal(&self) -> Vect {
5153
self.raw.scaled_normal().into()

src/pipeline/events.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ impl<'a> EventHandler for EventQueue<'a> {
125125

126126
#[cfg(test)]
127127
mod test {
128-
use bevy::time::{TimePlugin, TimeUpdateStrategy};
128+
use bevy::{
129+
app::{App, Startup, Update},
130+
prelude::{Commands, Component, Entity, Query, With},
131+
time::{TimePlugin, TimeUpdateStrategy},
132+
transform::{bundles::TransformBundle, components::Transform, TransformPlugin},
133+
MinimalPlugins,
134+
};
129135
use systems::tests::HeadlessRenderPlugin;
130136

131137
use crate::{plugin::*, prelude::*};
@@ -231,4 +237,76 @@ mod test {
231237
));
232238
}
233239
}
240+
241+
#[test]
242+
pub fn spam_remove_rapier_entity_interpolated() {
243+
let mut app = App::new();
244+
app.add_plugins((
245+
HeadlessRenderPlugin,
246+
MinimalPlugins,
247+
TransformPlugin,
248+
RapierPhysicsPlugin::<NoUserData>::default(),
249+
))
250+
.insert_resource(RapierConfiguration {
251+
timestep_mode: TimestepMode::Interpolated {
252+
dt: 1.0 / 30.0,
253+
time_scale: 1.0,
254+
substeps: 2,
255+
},
256+
..RapierConfiguration::new(1f32)
257+
})
258+
.add_systems(Startup, setup_physics)
259+
.add_systems(Update, remove_collider);
260+
// Simulates 60 updates per seconds
261+
app.insert_resource(TimeUpdateStrategy::ManualDuration(
262+
std::time::Duration::from_secs_f32(1f32 / 60f32),
263+
));
264+
265+
for i in 0..100 {
266+
dbg!(i);
267+
app.update();
268+
}
269+
return;
270+
271+
#[derive(Component)]
272+
pub struct ToRemove;
273+
274+
#[cfg(feature = "dim3")]
275+
fn cuboid(hx: Real, hy: Real, hz: Real) -> Collider {
276+
Collider::cuboid(hx, hy, hz)
277+
}
278+
#[cfg(feature = "dim2")]
279+
fn cuboid(hx: Real, hy: Real, _hz: Real) -> Collider {
280+
Collider::cuboid(hx, hy)
281+
}
282+
pub fn setup_physics(mut commands: Commands) {
283+
for _i in 0..100 {
284+
commands.spawn((
285+
TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0)),
286+
RigidBody::Dynamic,
287+
cuboid(0.5, 0.5, 0.5),
288+
ActiveEvents::all(),
289+
ToRemove,
290+
));
291+
}
292+
/*
293+
* Ground
294+
*/
295+
let ground_size = 5.1;
296+
let ground_height = 0.1;
297+
let starting_y = -0.5 - ground_height;
298+
299+
commands.spawn((
300+
TransformBundle::from(Transform::from_xyz(0.0, starting_y, 0.0)),
301+
cuboid(ground_size, ground_height, ground_size),
302+
));
303+
}
304+
305+
fn remove_collider(mut commands: Commands, query: Query<Entity, With<ToRemove>>) {
306+
let Some(entity) = query.iter().next() else {
307+
return;
308+
};
309+
commands.entity(entity).despawn();
310+
}
311+
}
234312
}

src/plugin/configuration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::math::{Real, Vect};
44
use crate::plugin::RapierContext;
55

66
#[cfg(doc)]
7-
use rapier::dynamics::IntegrationParameters;
7+
use {crate::prelude::TransformInterpolation, rapier::dynamics::IntegrationParameters};
88

99
/// Difference between simulation and rendering time
1010
#[derive(Resource, Default)]
@@ -39,7 +39,7 @@ pub enum TimestepMode {
3939
},
4040
/// Use a fixed timestep equal to `IntegrationParameters::dt`, but don't step if the
4141
/// physics simulation advanced by a time greater than the real-world elapsed time multiplied by `time_scale`.
42-
/// Rigid-bodies with a component `InterpolatedTransform` attached will use interpolation to
42+
/// Rigid-bodies with a component [`TransformInterpolation`] attached will use interpolation to
4343
/// estimate the rigid-bodies position in-between steps.
4444
Interpolated {
4545
/// The physics simulation will be advanced by this total amount at each Bevy tick, unless

src/plugin/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl RapierContext {
225225
.or_else(|| event_queue.as_ref().map(|q| q as &dyn EventHandler))
226226
.unwrap_or(&() as &dyn EventHandler);
227227

228+
let mut executed_steps = 0;
228229
match timestep_mode {
229230
TimestepMode::Interpolated {
230231
dt,
@@ -271,6 +272,7 @@ impl RapierContext {
271272
hooks,
272273
events,
273274
);
275+
executed_steps += 1;
274276
}
275277

276278
sim_to_render_time.diff -= dt;
@@ -302,6 +304,7 @@ impl RapierContext {
302304
hooks,
303305
events,
304306
);
307+
executed_steps += 1;
305308
}
306309
}
307310
TimestepMode::Fixed { dt, substeps } => {
@@ -326,9 +329,14 @@ impl RapierContext {
326329
hooks,
327330
events,
328331
);
332+
executed_steps += 1;
329333
}
330334
}
331335
}
336+
337+
if executed_steps > 0 {
338+
self.deleted_colliders.clear();
339+
}
332340
}
333341

334342
/// This method makes sure tha the rigid-body positions have been propagated to

src/plugin/systems/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub fn step_simulation<Hooks>(
5050
&mut sim_to_render_time,
5151
Some(interpolation_query),
5252
);
53-
context.deleted_colliders.clear();
5453
} else {
5554
context.propagate_modified_body_positions_to_colliders();
5655
}

src/render/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ use rapier::pipeline::{DebugRenderBackend, DebugRenderObject, DebugRenderPipelin
66
pub use rapier::pipeline::{DebugRenderMode, DebugRenderStyle};
77
use std::fmt::Debug;
88

9+
#[cfg(doc)]
10+
use crate::prelude::Collider;
11+
912
/// The color of a collider when using the debug-renderer.
1013
///
11-
/// Insert this component alongside the collider component to
14+
/// Insert this component alongside the [`Collider`] component to
1215
/// force to a specific value the color used to render the
1316
/// collider.
1417
#[derive(Copy, Clone, Component, PartialEq, Debug)]

0 commit comments

Comments
 (0)