dimforge / dimforge/bevy_rapier

Despawn events missed when using FixedUpdate

Open
#635 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
1.6k
Forks
282
PR merge metrics
No merged PRs in 30d

Description

I followed the [custom setup example](https://github.com/dimforge/bevy_rapier/blob/master/bevy_rapier3d/examples/custom_system_setup3.rs), but I used `FixedUpdate` instead of `PostUpdate`.
I noticed that, sometimes, the bodies/colliders of despawned entities would remain active in the world, when they should have been removed.
After looking into the [plugin's setup code](https://github.com/dimforge/bevy_rapier/blob/a4e9343cd75ce2be788280de2c21ed7efb9039b7/src/plugin/plugin.rs#L288C1-L295C10), I found
```rust
// These *must* be in the main schedule currently so that they do not miss events.
// See test `test_sync_removal` for an example of this.
if self.schedule != PostUpdate.intern() {
app.add_systems(
PostUpdate,
(systems::sync_removals,).before(TransformSystem::TransformPropagate),
);
}
```
So, despite using `with_default_system_setup(false)`, `self.schedule` is still `PostUpdate`, which means this will not be called and `sync_removals` will not be placed in the correct schedule. In the example I mentioned, the custom schedule used is `PostUpdate`, so this issue does not happen.
To fix this, I used `with_default_system_setup(false).in_schedule(FixedUpdate)`, but this feels like a hack since I will still have to register the systems again later.
My use case was I wanted to be able to pause the simulation, so I needed to add the systems and only make them run if the game is in a certain state:
```rust
.configure_sets(
FixedUpdate,
(
PhysicsSet::SyncBackend,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
)
.chain()
.before(TransformSystem::TransformPropagate),
)
.add_systems(
FixedUpdate,
(
RapierPhysicsPlugin::::get_systems(PhysicsSet::SyncBackend)
.in_set(PhysicsSet::SyncBackend),
RapierPhysicsPlugin::::get_systems(PhysicsSet::StepSimulation)
.in_set(PhysicsSet::StepSimulation),
RapierPhysicsPlugin::::get_systems(PhysicsSet::Writeback)
.in_set(PhysicsSet::Writeback),
)
.run_if(in_state(GameState::Running)) // only run if game is running
)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.