Skip to content

Make Rapier instance available outside React/hooks #578

Open

Description

@trepidacious

It would be useful to be able to use the "raw" instance of the imported RAPIER library outside React (i.e. not within a Physics context, without use of hooks). My specific use case is to pre-run a simulation of dice rolling so I can play it back in react-three-fiber with meshes rotated to guarantee landing with a pre-determined face upwards). It seems much easier to do this as a plain function rather than dragging in Canvas etc. when I don't want to display anything during the pre-run.

Looking at this rapier PR, it seems to be critical to only call RAPIER.init() once. My understanding is this will already be done by react-three-rapier if it has been used, however I don't think I can rely on this in my case, since my code may run before react-three-rapier is used.

Would it be possible to use a similar system to the code in the rapier PR to make a rapierInit() method available to call from wherever it's needed? This would return a promise, completing immediately if rapier is already inited. Then this could be called from react-three-fiber instead of directly initing rapier, but also from any other code that wants to use it?

If the PR was merged in rapier this would obviously then be unnecessary, but it looks like it's not likely to be.

Activity

  1. wiledal commented on Jan 5, 2024

    @wiledal
    Member

    I've been thinking about a v2 where the rapier instance is completely controlled by the user initializing the instance.

    <Physics rapier={yourRapierInstance}>
      ...
    </Physics>
    

    This would also solve usage of the non-compat version of Rapier, since the user will provide the library themselves!

  2. clicktodev commented on May 28, 2025

    @clicktodev

    this would be nice, why wasn't this set for v2?

  3. isaac-mason commented on May 29, 2025

    @isaac-mason
    Member

    A v2 was released for compatibility with the new @react-three/fiber and react major versions.

    It would be fair to rename the current "v2" label to "v3" 🙂

  4. isaac-mason commented on May 29, 2025

    @isaac-mason
    Member

    I've renamed the "v2" label to "v3" now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions