pmndrs / react-three-rapier

🤺 Rapier physics in React
https://react-three-rapier.pmnd.rs
MIT License
1.03k stars 57 forks source link

One-way-platforms by adding PhysicsHooks to world.step issue #597

Open driescroons opened 7 months ago

driescroons commented 7 months ago

Hey 👋 I recently came across this example in rust for one way platforms in rapier and tried to recreate the effect.

I'm currently able to let the ball pass through the platform by adding a PhysicsHook

https://github.com/pmndrs/react-three-rapier/assets/27679518/8d8bf38f-2be6-4b09-a9af-fa193ba998bc

However, I'm only able to disable the collision check. I'm not able to access the RigidBody's linvel, or translation and it results in an recursive use of an object detected which would lead to unsafe aliasing error. I'd like to check if the ball is above the platform to enable the collision check again.

image

How does this currently work?

In order to get this to work we need to be able to pass an additional hooks param to world.step(eventQueue, hooks) of type PhysicsHooks.

In order for us to get access to this in components, I added filterContactPairHooks and filterIntersectionPairHooks to useRapier.

This allows us to register a hook in our OneWayPlatform.jsx demo that we can push to filterContactPairHooks. We also need to make sure we set setActiveHooks to one for either our ball or platform.

This works, but when I try to get access to body's linvel or translation in the hook, from either the ref, or the params I get the above error. How am I able to access the body in that hook?

Code: https://github.com/pmndrs/react-three-rapier/pull/603

Run locally