Skip to content

Commit 7935428

Browse files
committed
feat: add useFilterContactPair SolverFlags.EMPTY test
1 parent 4cd93b7 commit 7935428

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

packages/react-three-rapier/tests/physics-hooks.test.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,72 @@ describe("physics hooks", () => {
155155
expect(onCollisionEnter).toHaveBeenCalled();
156156
});
157157

158+
it("should block collisions when hook returns SolverFlags.EMPTY", async () => {
159+
let ballRef: RapierRigidBody | null = null;
160+
let platformRef: RapierRigidBody | null = null;
161+
162+
const TestComponent = () => {
163+
const colliderRef = useRef<RapierCollider>(null);
164+
const platformBodyRef = useRef<RapierRigidBody>(null);
165+
166+
useEffect(() => {
167+
platformRef = platformBodyRef.current;
168+
}, []);
169+
170+
useFilterContactPair(
171+
useCallback(() => {
172+
return SolverFlags.EMPTY; // Block collision resolution
173+
}, [])
174+
);
175+
176+
useEffect(() => {
177+
if (colliderRef.current) {
178+
colliderRef.current.setActiveHooks(
179+
ActiveHooks.FILTER_CONTACT_PAIRS
180+
);
181+
}
182+
}, []);
183+
184+
return (
185+
<RigidBody ref={platformBodyRef} type="fixed" position={[0, 0, 0]}>
186+
<CuboidCollider ref={colliderRef} args={[5, 0.5, 5]} />
187+
</RigidBody>
188+
);
189+
};
190+
191+
const FallingBall = () => {
192+
const ref = useRef<RapierRigidBody>(null);
193+
194+
useEffect(() => {
195+
ballRef = ref.current;
196+
}, []);
197+
198+
return (
199+
<RigidBody ref={ref} position={[0, 5, 0]}>
200+
<CuboidCollider args={[0.5, 0.5, 0.5]} />
201+
</RigidBody>
202+
);
203+
};
204+
205+
const step = await awaitReady(
206+
<>
207+
<TestComponent />
208+
<FallingBall />
209+
</>
210+
);
211+
212+
await ReactThreeTestRenderer.act(async () => {
213+
// Step enough times for ball to fall through platform
214+
for (let i = 0; i < 120; i++) {
215+
step(1 / 60);
216+
}
217+
});
218+
219+
// Ball should have fallen through the platform (y position below platform)
220+
// If collision was resolved, ball would be resting on top of platform at y ~1
221+
expect(ballRef!.translation().y).toBeLessThan(-2);
222+
});
223+
158224
it("should work with cached body state from useBeforePhysicsStep", async () => {
159225
const filterHook = vi.fn(() => 1);
160226

0 commit comments

Comments
 (0)