Skip to content

Commit ef5dcac

Browse files
authored
Fix user-change handling for colliders as well as disabled colliders (#900)
* feat: add debug-demo for disabling a collider * feat: add a simple debug-demo with two cubes * feat: rename RigidBodyChangnes::MODIFIED and ColliderChanges::MODIFIED to ::IN_MODIFIED_SET * feat: render debug-colliders with a different color with the debug-renderer * chore: wire up new examples to the testbed * fix colliders user-modification being ignored after the first step * fix broad-phase still taking into account disabled colliders with enabled dynamic rigid-bodies * chore: update changelog * fix cargo doc
1 parent a68d0c6 commit ef5dcac

File tree

11 files changed

+146
-26
lines changed

11 files changed

+146
-26
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
## Unreleased
22

3-
- `InteractionGroups` struct now contains `InteractionTestMode`. Continues [rapier/pull/170](https://github.com/dimforge/rapier/pull/170) for [rapier/issues/622](https://github.com/dimforge/rapier/issues/622)
4-
- `InteractionGroups` constructor now requires an `InteractionTestMode` parameter. If you want same behaviour as before, use `InteractionTestMode::And` (eg. `InteractionGroups::new(Group::GROUP_1, Group::GROUP_1, InteractionTestMode::And)`)
3+
- `InteractionGroups` struct now contains `InteractionTestMode`.
4+
Continues [rapier/pull/170](https://github.com/dimforge/rapier/pull/170)
5+
for [rapier/issues/622](https://github.com/dimforge/rapier/issues/622)
6+
- `InteractionGroups` constructor now requires an `InteractionTestMode` parameter. If you want same behaviour as before,
7+
use `InteractionTestMode::And` (eg.
8+
`InteractionGroups::new(Group::GROUP_1, Group::GROUP_1, InteractionTestMode::And)`)
59
- `CoefficientCombineRule::Min` - now makes sure it uses a non zero value as result by using `coeff1.min(coeff2).abs()`
610
- `InteractionTestMode`: Specifies which method should be used to test interactions. Supports `AND` and `OR`.
711
- `CoefficientCombineRule::ClampedSum` - Adds the two coefficients and does a clamp to have at most 1.
12+
- Rename `ColliderChanges::CHANGED` to `::IN_CHANGED_SET` to make its meaning more precise.
13+
- Rename `RigidBodyChanges::CHANGED` to `::IN_CHANGED_SET` to make its meaning more precise.
14+
- Fix colliders ignoring user-changes after the first simulation step.
15+
- Fix broad-phase incorrectly taking into account disabled colliders attached to an enabled dynamic rigid-body.
816

917
## v0.30.1 (17 Oct. 2025)
1018

examples3d/all_examples3.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ mod joints3;
4141
mod character_controller3;
4242
mod debug_chain_high_mass_ratio3;
4343
mod debug_cube_high_mass_ratio3;
44+
mod debug_disabled3;
4445
mod debug_internal_edges3;
4546
mod debug_long_chain3;
4647
mod debug_multibody_ang_motor_pos3;
4748
mod debug_sleeping_kinematic3;
49+
mod debug_two_cubes3;
4850
mod gyroscopic3;
4951
mod inverse_kinematics3;
5052
mod joint_motor_position3;
@@ -106,6 +108,8 @@ pub fn main() {
106108
("(Debug) big colliders", debug_big_colliders3::init_world),
107109
("(Debug) boxes", debug_boxes3::init_world),
108110
("(Debug) balls", debug_balls3::init_world),
111+
("(Debug) disabled", debug_disabled3::init_world),
112+
("(Debug) two cubes", debug_two_cubes3::init_world),
109113
("(Debug) pop", debug_pop3::init_world),
110114
(
111115
"(Debug) dyn. coll. add",

examples3d/debug_disabled3.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use rapier_testbed3d::Testbed;
2+
use rapier3d::prelude::*;
3+
4+
pub fn init_world(testbed: &mut Testbed) {
5+
let mut bodies = RigidBodySet::new();
6+
let mut colliders = ColliderSet::new();
7+
let impulse_joints = ImpulseJointSet::new();
8+
let multibody_joints = MultibodyJointSet::new();
9+
10+
let rad = 0.5;
11+
12+
/*
13+
* Ground
14+
*/
15+
let ground_size = 10.1;
16+
let ground_height = 2.1;
17+
18+
let rigid_body = RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height, 0.0]);
19+
let handle = bodies.insert(rigid_body);
20+
let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size);
21+
colliders.insert_with_parent(collider, handle, &mut bodies);
22+
23+
/*
24+
* Platform that will be enabled/disabled.
25+
*/
26+
let rigid_body = RigidBodyBuilder::dynamic().translation(vector![0.0, 5.0, 0.0]);
27+
let handle = bodies.insert(rigid_body);
28+
let collider = ColliderBuilder::cuboid(5.0, 1.0, 5.0);
29+
let handle_to_disable = colliders.insert_with_parent(collider, handle, &mut bodies);
30+
31+
// Callback that will be executed on the main loop to handle proximities.
32+
testbed.add_callback(move |mut graphics, physics, _, run_state| {
33+
if run_state.timestep_id % 250 == 0 {
34+
let co = &mut physics.colliders[handle_to_disable];
35+
let enabled = co.is_enabled();
36+
co.set_enabled(!enabled);
37+
println!("Platform is now enabled: {}", co.is_enabled());
38+
}
39+
40+
if run_state.timestep_id % 25 == 0 {
41+
let rigid_body = RigidBodyBuilder::dynamic().translation(vector![0.0, 20.0, 0.0]);
42+
let handle = physics.bodies.insert(rigid_body);
43+
let collider = ColliderBuilder::cuboid(rad, rad, rad);
44+
physics
45+
.colliders
46+
.insert_with_parent(collider, handle, &mut physics.bodies);
47+
48+
if let Some(graphics) = &mut graphics {
49+
graphics.add_body(handle, &physics.bodies, &physics.colliders);
50+
}
51+
}
52+
});
53+
54+
/*
55+
* Set up the testbed.
56+
*/
57+
testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
58+
testbed.look_at(point![-30.0, 4.0, -30.0], point![0.0, 1.0, 0.0]);
59+
}

examples3d/debug_two_cubes3.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rapier_testbed3d::Testbed;
2+
use rapier3d::prelude::*;
3+
4+
pub fn init_world(testbed: &mut Testbed) {
5+
/*
6+
* World
7+
*/
8+
let mut bodies = RigidBodySet::new();
9+
let mut colliders = ColliderSet::new();
10+
let impulse_joints = ImpulseJointSet::new();
11+
let multibody_joints = MultibodyJointSet::new();
12+
13+
// Dynamic box rigid body.
14+
let rigid_body = RigidBodyBuilder::dynamic().translation(vector![0.0, 2.0, 0.0]);
15+
let handle = bodies.insert(rigid_body);
16+
let collider = ColliderBuilder::cuboid(0.5, 0.5, 0.5);
17+
colliders.insert_with_parent(collider, handle, &mut bodies);
18+
19+
let rigid_body = RigidBodyBuilder::fixed();
20+
let handle = bodies.insert(rigid_body);
21+
let collider = ColliderBuilder::cuboid(0.5, 0.5, 0.5);
22+
colliders.insert_with_parent(collider, handle, &mut bodies);
23+
24+
/*
25+
* Set up the testbed.
26+
*/
27+
testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
28+
testbed.look_at(point![10.0, 10.0, 10.0], Point::origin());
29+
}

src/dynamics/rigid_body_components.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ bitflags::bitflags! {
111111
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
112112
/// Flags describing how the rigid-body has been modified by the user.
113113
pub struct RigidBodyChanges: u32 {
114-
/// Flag indicating that any component of this rigid-body has been modified.
115-
const MODIFIED = 1 << 0;
114+
/// Flag indicating that this rigid-body is in the modified rigid-body set.
115+
const IN_MODIFIED_SET = 1 << 0;
116116
/// Flag indicating that the `RigidBodyPosition` component of this rigid-body has been modified.
117117
const POSITION = 1 << 1;
118118
/// Flag indicating that the `RigidBodyActivation` component of this rigid-body has been modified.
@@ -1061,10 +1061,7 @@ impl RigidBodyColliders {
10611061
co_handle: ColliderHandle,
10621062
) {
10631063
if let Some(i) = self.0.iter().position(|e| *e == co_handle) {
1064-
rb_changes.set(
1065-
RigidBodyChanges::MODIFIED | RigidBodyChanges::COLLIDERS,
1066-
true,
1067-
);
1064+
rb_changes.set(RigidBodyChanges::COLLIDERS, true);
10681065
self.0.swap_remove(i);
10691066
}
10701067
}
@@ -1083,10 +1080,7 @@ impl RigidBodyColliders {
10831080
co_shape: &ColliderShape,
10841081
co_mprops: &ColliderMassProps,
10851082
) {
1086-
rb_changes.set(
1087-
RigidBodyChanges::MODIFIED | RigidBodyChanges::COLLIDERS,
1088-
true,
1089-
);
1083+
rb_changes.set(RigidBodyChanges::COLLIDERS, true);
10901084

10911085
co_pos.0 = rb_pos.position * co_parent.pos_wrt_parent;
10921086
rb_ccd.ccd_thickness = rb_ccd.ccd_thickness.min(co_shape.ccd_thickness());
@@ -1113,6 +1107,8 @@ impl RigidBodyColliders {
11131107
) {
11141108
for handle in &self.0 {
11151109
// NOTE: the ColliderParent component must exist if we enter this method.
1110+
// NOTE: currently, we are propagating the position even if the collider is disabled.
1111+
// Is that the best behavior?
11161112
let co = colliders.index_mut_internal(*handle);
11171113
let new_pos = parent_pos * co.parent.as_ref().unwrap().pos_wrt_parent;
11181114

src/dynamics/rigid_body_set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ pub(crate) type ModifiedRigidBodies = ModifiedObjects<RigidBodyHandle, RigidBody
3131
impl HasModifiedFlag for RigidBody {
3232
#[inline]
3333
fn has_modified_flag(&self) -> bool {
34-
self.changes.contains(RigidBodyChanges::MODIFIED)
34+
self.changes.contains(RigidBodyChanges::IN_MODIFIED_SET)
3535
}
3636

3737
#[inline]
3838
fn set_modified_flag(&mut self) {
39-
self.changes |= RigidBodyChanges::MODIFIED;
39+
self.changes |= RigidBodyChanges::IN_MODIFIED_SET;
4040
}
4141
}
4242

src/geometry/broad_phase_bvh.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ impl BroadPhaseBvh {
6969
/// sent previously and no `RemovePair` happened since then). Sending redundant events is allowed
7070
/// but can result in a slight computational overhead.
7171
///
72-
/// The `colliders` set is mutable only to provide access to
73-
/// [`collider.set_internal_broad_phase_proxy_index`]. Other properties of the collider should
74-
/// **not** be modified during the broad-phase update.
75-
///
7672
/// # Parameters
7773
/// - `params`: the integration parameters governing the simulation.
7874
/// - `colliders`: the set of colliders. Change detection with `collider.needs_broad_phase_update()`

src/geometry/collider_components.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ bitflags::bitflags! {
3535
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
3636
/// Flags describing how the collider has been modified by the user.
3737
pub struct ColliderChanges: u32 {
38-
/// Flag indicating that any component of the collider has been modified.
39-
const MODIFIED = 1 << 0;
38+
/// Flag indicating that the collider handle is in the changed collider set.
39+
const IN_MODIFIED_SET = 1 << 0;
4040
/// Flag indicating that the density or mass-properties of this collider was changed.
4141
const LOCAL_MASS_PROPERTIES = 1 << 1; // => RigidBody local mass-properties update.
4242
/// Flag indicating that the `ColliderParent` component of the collider has been modified.

src/geometry/collider_set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ pub type ModifiedColliders = ModifiedObjects<ColliderHandle, Collider>;
1111
impl HasModifiedFlag for Collider {
1212
#[inline]
1313
fn has_modified_flag(&self) -> bool {
14-
self.changes.contains(ColliderChanges::MODIFIED)
14+
self.changes.contains(ColliderChanges::IN_MODIFIED_SET)
1515
}
1616

1717
#[inline]
1818
fn set_modified_flag(&mut self) {
19-
self.changes |= ColliderChanges::MODIFIED;
19+
self.changes |= ColliderChanges::IN_MODIFIED_SET;
2020
}
2121
}
2222

src/pipeline/debug_render_pipeline/debug_render_pipeline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ impl DebugRenderPipeline {
327327
c[2] * coeff[2],
328328
c[3] * coeff[3],
329329
]
330+
} else if !co.is_enabled() {
331+
self.style.disabled_color_multiplier
330332
} else {
331333
self.style.collider_parentless_color
332334
};

0 commit comments

Comments
 (0)