react-three-rapier icon indicating copy to clipboard operation
react-three-rapier copied to clipboard

character going through physics

Open pierroo opened this issue 9 months ago • 7 comments

There is this random bug bothering me for months:

I am using r3f and rapier. I have this as my Experience component

...

 useEffect(() => {
    if (!didInitRef.current) {
      didInitRef.current = true;
      preloadAllAssets(); // function to preload all my models and textures
    }
  }, []);

...
<Physics debug={false} colliders={false}>
        <ListPlayers />
         <WholeScene />
      </Physics>

I manage my collider from the WholeScene and Player component, so I have colliders={false} on my main Physics parent.

At random during launch (refreshing the page, sometimes it works, sometimes it does not), my player will fall through my scene and get stuck in it.

I purposedly give it a high y position, so that when it loads during its fall I hope the Physics has enough time to compute and "welcome" my player. but it feels cheap / like a hack.

is there a way to make sure my scene is ready when physics starts? (if that's the issue) or to know when physics is done so I can load my player?

I have tried everything and remains lost.

pierroo avatar Mar 20 '25 15:03 pierroo

Related: https://github.com/pmndrs/react-three-rapier/discussions/522

isaac-mason avatar Mar 20 '25 22:03 isaac-mason

Thank you @isaac-mason , I had seen that link, tried to implement it as well, but the same keeps happening at random. I was looking for a more robust solution somehown if there was something native to rapier to tackle this issue.

pierroo avatar Mar 21 '25 08:03 pierroo

@pierroo if you can create a minimal codesandbox I'd be happy to take a look 🙂 it's hard for me to suggest anything concretely with just the excerpt there not knowing how your asset loading is set up. The fundamental though may be that your ground may be added later than your character. This is generally either handled by not progressing physics until the desired state is achieved, or something similar.

isaac-mason avatar Mar 21 '25 12:03 isaac-mason

Yes I completely understand without a link it might be hard to help. Unfortunately the project is so big and with so many inter-dependencies between components that it would be a struggle to isolate the issue into a sandbox, however here is my findings:

both my ground and my character are added at the same time as the physics parent. I have tried all kind of delay (adding my character 10 seconds after everything else), but when physics decided to break on launch, it will regardless.

However, I then removed all complex meshes, and went to my blender to add some super basic planes and cubes to handle every single environment collider. Now I am no longer facing any issue, obviously because the geometry of those rigidbody is 100x times easier now. But doesn't that say that the issue was indeed with rapier handling physics and not about any kind of orders of display? The whole scene / player are appearing at the same time as before, yet by making the rigidbodies simpler now I no longer go through the floor.

thank you for being willing to help!

pierroo avatar Mar 21 '25 12:03 pierroo

A few things you could look at:

  • are you using a trimesh for your ground? These are very susceptible to issues with tunnelling. Convex shapes are more forgiving when de-penetrating bodies. You could try enabling ccd for your character if you are using a dynamic body.
  • are you using a fixed time step? or a variable / delta time step? If you're using a variable time step and your first step's delta time is large, that could also result in your character moving down too far in one step and getting stuck.

isaac-mason avatar Mar 22 '25 02:03 isaac-mason

Yes I am indeed using a trimesh; can you clarify what is "ccd" on the other hand? :/ I am using whatever is the default regarding timestep, I am not changing anything on that.

And as you mentioned trimesh is complex, I created a fake plane floor specifically for my "spawn" point, and it did the trick (not a fix then, more of a hack for now)

pierroo avatar Mar 22 '25 18:03 pierroo

See here for the ccd (continuous collision detection) RigidBody component prop: https://pmndrs.github.io/react-three-rapier/interfaces/RigidBodyProps.html#ccd

isaac-mason avatar Mar 23 '25 01:03 isaac-mason