|
| 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 | +} |
0 commit comments