Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions crates/rapier3d/tests/persistent_islands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,60 @@ fn type_change_bridges_and_unbridges() {
);
}

/// Bodies changed to fixed must leave both persistent and active islands;
/// changing them back to dynamic must register them exactly once again.
#[test]
fn fixed_bodies_leave_the_active_set_and_reactivate() {
let mut world = PhysicsWorld::new();
world.gravity = Vector::ZERO;
let handles: Vec<_> = (0..8)
.map(|i| {
world
.insert(
RigidBodyBuilder::dynamic()
.can_sleep(false)
.translation(Vector::new(i as Real * 2.0, 10.0, 0.0)),
ColliderBuilder::cuboid(0.5, 0.5, 0.5),
)
.0
})
.collect();
let frozen = &handles[..handles.len() - 1];
let survivor = *handles.last().unwrap();

world.step();
assert_eq!(world.islands.num_active_bodies(), handles.len());

for &handle in frozen {
world.bodies[handle].set_body_type(RigidBodyType::Fixed, false);
}
world.step();

assert_eq!(
world.islands.active_bodies().collect::<Vec<_>>(),
[survivor]
);
for &handle in frozen {
assert_eq!(island_of(&world, handle), None);
}

for &handle in frozen {
world.bodies[handle].set_body_type(RigidBodyType::Dynamic, true);
}
world.gravity = Vector::new(0.0, -9.81, 0.0);
for _ in 0..30 {
world.step();
}

assert_eq!(world.islands.num_active_bodies(), handles.len());
let active: std::collections::HashSet<_> = world.islands.active_bodies().collect();
assert_eq!(active.len(), handles.len());
for handle in handles {
assert!(active.contains(&handle));
assert!(world.bodies[handle].translation().y < 9.0);
}
}

/// Disabling the middle box must split the row; re-enabling must re-merge it.
#[test]
fn disable_enable_bridges_and_unbridges() {
Expand Down
8 changes: 5 additions & 3 deletions src/dynamics/island_manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ impl IslandManager {
};

if rb.is_fixed() {
// A body turned fixed leaves the persistent islands (fixed bodies
// are never island members).
self.persistent.remove_body(bodies, handle);
// Fixed bodies belong to neither persistent nor active/sleeping islands.
// Capture both sets of ids before removal clears them, just as when disabling
// a body. Leaving the active entry behind keeps fixed colliders in every step.
let ids = rb.ids;
self.rigid_body_removed_or_disabled(handle, &ids, bodies);
return;
}

Expand Down
20 changes: 4 additions & 16 deletions src/dynamics/island_manager/persistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,10 @@ impl PersistentIslands {
island.sleeping = rb.activation.sleeping;
}

/// Removes `handle` from its island (body removed, disabled, or turned fixed). Links
/// referencing the body are *not* eagerly removed: contact links die with their pairs, joint
/// links when joint edits drain; the split treats their endpoints as non-connecting meanwhile.
pub fn remove_body(&mut self, bodies: &mut RigidBodySet, handle: RigidBodyHandle) {
let Some(rb) = bodies.get_mut_internal(handle) else {
return;
};
let island_id = rb.ids.island_id;
let island_index = rb.ids.island_index;
rb.ids.island_id = INVALID_ISLAND;
rb.ids.island_index = u32::MAX;
self.remove_body_raw(bodies, island_id, island_index);
}

/// Like [`Self::remove_body`], for a body that is already gone from the
/// body set (its ids were captured before removal).
/// Removes a body from its persistent island using ids captured before the
/// body was removed, disabled, or made fixed. Links referencing the body are not eagerly
/// removed: contact links die with their pairs, joint links when joint edits drain;
/// the split treats their endpoints as non-connecting meanwhile.
pub fn remove_body_raw(
&mut self,
bodies: &mut RigidBodySet,
Expand Down
Loading