Skip to content

Commit

Permalink
Fix a crash when removing colliders in TimestepMode::Interpolated (#…
Browse files Browse the repository at this point in the history
…563)

Co-authored-by: Sébastien Crozet <sebastien@crozet.re>
  • Loading branch information
Vrixyz and sebcrozet committed Jul 23, 2024
1 parent c6c9bbc commit 59477d9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### 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
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();
}
}
}
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

0 comments on commit 59477d9

Please sign in to comment.