Skip to content

Commit d90dd95

Browse files
authored
Fix CCD fixed target cache invalidation (#1003)
1 parent 29616c7 commit d90dd95

3 files changed

Lines changed: 86 additions & 18 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//! The CCD fixed-target cache must not outlive the colliders it points at.
2+
//!
3+
//! The non-bullet continuous pass sweeps against a cached list of fixed targets, held as
4+
//! `ColliderHandle`s from one step to the next. The scene-change signal that invalidates it
5+
//! is rebuilt from the per-step user-change lists, which are drained whether or not the CCD
6+
//! pass runs — so a removal on a step where nothing moved fast enough used to be forgotten,
7+
//! leaving the cache holding a dead handle for the next fast body to resolve.
8+
9+
use rapier2d::prelude::*;
10+
11+
/// A fixed collider removed on a step without any CCD-active body must not linger in the
12+
/// cached fixed-target list.
13+
#[test]
14+
fn fixed_collider_removed_on_a_ccd_less_step_leaves_no_stale_target() {
15+
let mut world = PhysicsWorld::new();
16+
17+
// Two parentless (hence fixed-target) colliders. `stale` sits on the path `faller`
18+
// takes down to the ground, so a leftover entry is guaranteed to be resolved rather
19+
// than skipped by the swept-AABB test.
20+
world.insert_collider(
21+
ColliderBuilder::cuboid(100.0, 0.5).translation(Vector::new(0.0, 0.0)),
22+
None,
23+
);
24+
let stale = world.insert_collider(
25+
ColliderBuilder::cuboid(2.0, 2.0).translation(Vector::new(10.0, 25.0)),
26+
None,
27+
);
28+
29+
// Moving fast from the first step: forces a continuous pass, which is what populates
30+
// the fixed-target cache.
31+
let fast = world.insert_body(
32+
RigidBodyBuilder::dynamic()
33+
.translation(Vector::new(0.0, 30.0))
34+
.linvel(Vector::new(0.0, -400.0)),
35+
);
36+
world.insert_collider(ColliderBuilder::ball(0.2), Some(fast));
37+
38+
// Starts at rest and only becomes CCD-active later, from gravity alone — no user change
39+
// marks the step it wakes the continuous pass back up.
40+
let faller =
41+
world.insert_body(RigidBodyBuilder::dynamic().translation(Vector::new(10.0, 60.0)));
42+
world.insert_collider(ColliderBuilder::ball(0.05), Some(faller));
43+
44+
world.step();
45+
46+
// Take the fast body out so the next few steps skip the continuous pass entirely.
47+
world.remove_body(fast);
48+
world.step();
49+
50+
// The removal whose invalidation signal used to be dropped: `faller` is still slow, so
51+
// no CCD pass runs this step to notice it.
52+
world.remove_collider(stale);
53+
world.step();
54+
55+
// Nothing is touched from here on: `faller` accelerates until it is CCD-active, and the
56+
// pass it triggers resolves the cached fixed targets.
57+
for _ in 0..400 {
58+
world.step();
59+
}
60+
61+
// It fell all the way through where `stale` used to be and settled on the ground.
62+
let y = world.bodies[faller].translation().y;
63+
assert!(
64+
(y - 0.55).abs() < 0.1,
65+
"faller should rest on the ground at y ≈ 0.55, got {y}"
66+
);
67+
}

‎src/dynamics/ccd/ccd_solver.rs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ impl CCDSolver {
4343
Self::default()
4444
}
4545

46+
/// Discards the cached fixed-target list used by the non-bullet continuous pass.
47+
/// This must be called any time the fixed targets change. If using the physics pipeline,
48+
/// it is called for you. If not, it must be called manually.
49+
pub fn invalidate_fixed_targets_cache(&mut self) {
50+
self.fixed_targets_cache = None;
51+
}
52+
4653
/// Updates the set of bodies that needs CCD to be resolved.
4754
///
4855
/// Returns `true` if any rigid-body must have CCD resolved.
@@ -165,10 +172,6 @@ impl CCDSolver {
165172
narrow_phase: &NarrowPhase,
166173
hooks: &dyn PhysicsHooks,
167174
events: &dyn EventHandler,
168-
// `true` when colliders/bodies were added, removed or modified by the
169-
// user since the last step: the only ways a fixed target can appear,
170-
// vanish or move, hence the fixed-target cache invalidation signal.
171-
scene_changed: bool,
172175
) {
173176
let dt = params.dt;
174177
let linear_slop = params.allowed_linear_error();
@@ -194,14 +197,12 @@ impl CCDSolver {
194197
);
195198
let (bvh, dispatcher) = (query_pipeline.bvh, query_pipeline.dispatcher);
196199
// Non-bullet fast bodies only hit fixed targets: sweep against the (small) cached
197-
// fixed-collider list instead of the full BVH. Rebuilt — a full collider scan —
198-
// only on scene changes, since fixed targets can't move otherwise.
200+
// fixed-collider list instead of the full BVH.
199201
let prediction = params.prediction_distance();
200-
let cache_valid = !scene_changed
201-
&& self
202-
.fixed_targets_cache
203-
.as_ref()
204-
.is_some_and(|(p, _)| *p == prediction);
202+
let cache_valid = self
203+
.fixed_targets_cache
204+
.as_ref()
205+
.is_some_and(|(p, _)| *p == prediction);
205206
if !cache_valid {
206207
self.fixed_targets_cache = Some((
207208
prediction,

‎src/pipeline/physics_pipeline/substep.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ impl PhysicsPipeline {
6262
ccd_solver: &mut CCDSolver,
6363
hooks: &dyn PhysicsHooks,
6464
events: &dyn EventHandler,
65-
scene_changed: bool,
6665
) {
6766
self.counters.ccd.toi_computation_time.start();
6867
// Handle CCD: sweep the fast bodies and clamp their `next_position` to their
@@ -76,7 +75,6 @@ impl PhysicsPipeline {
7675
narrow_phase,
7776
hooks,
7877
events,
79-
scene_changed,
8078
);
8179
self.counters.ccd.toi_computation_time.pause();
8280
}
@@ -333,12 +331,15 @@ impl PhysicsPipeline {
333331
.filter(|h| colliders.get(*h).map(|c| !c.is_enabled()).unwrap_or(false)),
334332
);
335333

336-
// Whether any user change could have added, removed or moved a FIXED
337-
// collider this step — the CCD fixed-target cache invalidation signal
334+
// If a user change could have added, removed, or moved a FIXED
335+
// collider this step, invalidate the cache.
338336
// (internal motion never touches fixed colliders nor these lists).
339-
let ccd_scene_changed = !modified_colliders.is_empty()
337+
if !modified_colliders.is_empty()
340338
|| !removed_colliders.is_empty()
341-
|| !modified_bodies.is_empty();
339+
|| !modified_bodies.is_empty()
340+
{
341+
ccd_solver.invalidate_fixed_targets_cache();
342+
}
342343

343344
// Join islands based on new joints.
344345
#[cfg(feature = "enhanced-determinism")]
@@ -514,7 +515,6 @@ impl PhysicsPipeline {
514515
ccd_solver,
515516
hooks,
516517
events,
517-
ccd_scene_changed,
518518
);
519519
}
520520
}

0 commit comments

Comments
 (0)