@react-three/rapier 0.11.3 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/readme.md CHANGED
@@ -7,9 +7,12 @@
7
7
  <a href="https://discord.gg/ZZjjNvJ"><img src="https://img.shields.io/discord/740090768164651008?style=for-the-badge&colorA=0099DA&colorB=ffffff&label=discord&logo=discord&logoColor=ffffff" /></a>
8
8
  </p>
9
9
 
10
- <p align="center">⚠️ Under heavy development. All APIs are subject to change. ⚠️
10
+ <p align="center">
11
+ ⚠️ This library is under development. All APIs are subject to change. ⚠️
11
12
  <br />
12
- For contributions, please read the <a href="https://github.com/pmndrs/react-three-rapier/blob/main/packages/react-three-rapier/CONTRIBUTING.md">Contribution Guide</a>.
13
+ For contributions, please read the <a href="https://github.com/pmndrs/react-three-rapier/blob/main/packages/react-three-rapier/CONTRIBUTING.md">🪧 Contribution Guide</a>.
14
+ <br/>
15
+ For available APIs, see <a href="https://pmndrs.github.io/react-three-rapier/>">🧩 API Docs</a>
13
16
  </p>
14
17
 
15
18
  ---
@@ -18,7 +21,6 @@ For contributions, please read the <a href="https://github.com/pmndrs/react-thre
18
21
 
19
22
  The goal of this library to is to provide a fast physics engine with minimal friction and small, straight forward API.
20
23
 
21
-
22
24
  ## Basic Usage
23
25
 
24
26
  ```tsx
@@ -47,59 +49,81 @@ const App = () => {
47
49
 
48
50
  ---
49
51
 
52
+ ## 📝 Readme note
53
+ Below follows a guide on core concepts for `react-three/rapier`.
54
+ For full API outline and documentation, see 🧩 [API Docs](https://pmndrs.github.io/react-three-rapier/).
55
+
56
+ ---
57
+
50
58
  ## Readme Topics
51
59
 
52
60
  - [Basic Usage](#basic-usage)
61
+ - [📝 Readme note](#-readme-note)
53
62
  - [Readme Topics](#readme-topics)
54
63
  - [The Physics Component](#the-physics-component)
55
- - [Automatic colliders](#automatic-colliders)
56
- - [Collider Examples](#collider-examples)
64
+ - [The RigidBody Component](#the-rigidbody-component)
65
+ - [Automatic Colliders](#automatic-colliders)
66
+ - [Collider Components](#collider-components)
67
+ - [🖼 Collider Examples](#-collider-examples)
57
68
  - [Instanced Meshes](#instanced-meshes)
58
69
  - [Debug](#debug)
59
70
  - [Collision Events](#collision-events)
60
71
  - [Configuring collision and solver groups](#configuring-collision-and-solver-groups)
61
72
  - [Contact force events](#contact-force-events)
62
73
  - [Sensors](#sensors)
63
- - [Sensors Example](#sensors-example)
74
+ - [🖼 Sensors Example](#-sensors-example)
64
75
  - [Attractors](#attractors)
65
- - [Attractors Example](#attractors-example)
76
+ - [🖼 Attractors Example](#-attractors-example)
66
77
  - [Configuring Time Step Size](#configuring-time-step-size)
67
- - [Manual stepping](#manual-stepping)
68
78
  - [Joints](#joints)
69
79
  - [Fixed Joint](#fixed-joint)
70
80
  - [Spherical Joint](#spherical-joint)
71
81
  - [Revolute Joint](#revolute-joint)
72
82
  - [Prismatic Joint](#prismatic-joint)
73
83
  - [Joint APIs](#joint-apis)
74
- - [Joints Example](#joints-example)
84
+ - [🖼 Joints Example](#-joints-example)
85
+ - [Advanced hooks usage](#advanced-hooks-usage)
86
+ - [Manual stepping](#manual-stepping)
75
87
 
76
88
  ---
77
89
 
78
90
  ## The Physics Component
79
91
  The `<Physics />` component is the root component of your physics world. It is responsible for creating the physics world and managing the simulation. It relies on lazily initiating `Rapier` and needs to be wrapped in `<Suspense />`.
80
92
 
81
- ```tsx
82
- // The gravity of the physics workd
83
- gravity?: Vector3Array; // default [0, -9.81, 0]
84
-
85
- // Which collider shape to generate for meshes by default
86
- colliders?: RigidBodyAutoCollider; // default "cuboid"
93
+ 🧩 See [PhysicsProps docs](https://pmndrs.github.io/react-three-rapier/interfaces/PhysicsProps.html) for available props.
87
94
 
88
- // The number of physics steps per second
89
- timeStep?: number | "vary"; // default 1/60
95
+ ```tsx
96
+ const Scene = () => {
97
+ return (
98
+ <Canvas>
99
+ <Suspense>
100
+ <Physics
101
+ gravity={[0,1,0]}
102
+ interpolation={false}
103
+ colliders={false}
104
+ >
105
+ ...
106
+ </Physics>
107
+ </Suspense>
108
+ </Canvas>
109
+ );
110
+ }
111
+ ```
90
112
 
91
- // Pause the physic simulation
92
- paused?: boolean; // default false
113
+ ## The RigidBody Component
114
+ The `<RigidBody />` component is used to add a `mesh` into the physics world. You use it by wrapping one or more `meshes` and setting desired props. By default, this will automatically generate `Colliders` based on the shape of the wrapped `meshes` (see [Automatic colliders](#automatic-colliders)).
93
115
 
94
- // Which order to run the physics simulation
95
- updatePriority?: number;
116
+ 🧩 See [RigidBodyProps docs](https://pmndrs.github.io/react-three-rapier/interfaces/RigidBodyProps.html) for available props.
96
117
 
97
- // If the physics updates slower than the monitor refreshes,
98
- // interpolation will smooth out the steps between frames
99
- interpolate?: boolean; // default true
118
+ ```tsx
119
+ const RigidBodyMesh = () => (
120
+ <RigidBody>
121
+ <mesh />
122
+ </RigidBody>
123
+ );
100
124
  ```
101
125
 
102
- ## Automatic colliders
126
+ ## Automatic Colliders
103
127
 
104
128
  RigidBodies generate automatic colliders by default for all meshes that it contains. You can control the default collider by setting the `colliders` prop on a `<RigidBody />`, or change it globally by setting `colliders` on `<Physics />`. Setting `colliders={false}` disables auto-generation.
105
129
 
@@ -140,35 +164,47 @@ const Scene = () => (
140
164
  <RigidBody position={[0, 10, 0]} colliders="ball">
141
165
  <Sphere />
142
166
  </RigidBody>
167
+ </Physics>
168
+ );
169
+ ```
143
170
 
144
- {/* Make a compound shape with two custom BallColliders */}
145
- <RigidBody position={[0, 10, 0]}>
146
- <Sphere />
147
- <BallCollider args={[0.5]} />
148
- <BallCollider args={[0.5]} position={[1, 0, 0]} />
149
- </RigidBody>
150
-
151
- {/* Make a compound shape with two custom BallColliders, an automatic BallCollider,
152
- Two automatic MeshColliders, based on two different shape strategies */}
153
- <RigidBody position={[0, 10, 0]} colliders='ball'>
154
- <MeshCollider type="trimesh">
155
- <mesh ... />
156
- </MeshCollider>
171
+ ## Collider Components
157
172
 
158
- <MeshCollider type="hull">
159
- <mesh ... />
160
- </MeshCollider>
173
+ You can also create `Colliders` by hand and add them to a `RigidBody` to create compound colliders. This is useful for creating more complex shapes, for creating simplified shapes for performance reasons, or for detecting collisions on specific parts of a mesh.
161
174
 
162
- <Sphere />
175
+ 🧩 See [ColliderProps docs](https://pmndrs.github.io/react-three-rapier/interfaces/ColliderProps.html) for available props.
163
176
 
164
- <BallCollider args={[0.5]} />
165
- <BallCollider args={[0.5]} position={[1, 0, 0]} />
166
- </RigidBody>
167
- </Physics>
168
- );
177
+ ```tsx
178
+ const Scene = () => (<>
179
+ {/* Make a compound shape with two custom BallColliders */}
180
+ <RigidBody position={[0, 10, 0]}>
181
+ <Sphere />
182
+ <BallCollider args={[0.5]} />
183
+ <BallCollider args={[0.5]} position={[1, 0, 0]} />
184
+ </RigidBody>
185
+
186
+ {/* Make a compound shape with two custom BallColliders, an automatic BallCollider,
187
+ Two automatic MeshColliders, based on two different shape types */}
188
+ <RigidBody position={[0, 10, 0]} colliders='ball'>
189
+ <MeshCollider type="trimesh">
190
+ <mesh ... />
191
+ </MeshCollider>
192
+
193
+ <MeshCollider type="hull">
194
+ <mesh ... />
195
+ </MeshCollider>
196
+
197
+ <Sphere />
198
+
199
+ <BallCollider args={[0.5]} />
200
+ <BallCollider args={[0.5]} position={[1, 0, 0]} />
201
+ </RigidBody>
202
+ <>)
169
203
  ```
170
204
 
171
- Objects work inside other transformed objects as well. Simulation runs in world space and is transformed to the objects local space, so that things act as you'd expect.
205
+ RigidBodies work inside other transformed objects as well. Simulation runs in world space and is transformed to the objects local space, so that things act as you'd expect.
206
+
207
+ > **Note** It's always best to create RigidBodies where the center of gravity is in the center of the object, otherwise you might get some unexpected behavior during simulation interpolation.
172
208
 
173
209
  ```tsx
174
210
  import { Box } from "@react-three/drei";
@@ -194,7 +230,7 @@ If part of our meshes are invisible and you want to include them in the collider
194
230
  </RigidBody>
195
231
  ```
196
232
 
197
- ### Collider Examples
233
+ ### 🖼 Collider Examples
198
234
  <a href="https://codesandbox.io/s/react-three-rapier-auto-colliders-b4coz1"><img src="https://raw.githubusercontent.com/pmndrs/react-three-rapier/HEAD/packages/react-three-rapier/misc/example-auto-colliders.jpg" width="240" /></a>
199
235
  <a href="https://codesandbox.io/s/react-three-rapier-compound-colliders-ol5ybn"><img src="https://raw.githubusercontent.com/pmndrs/react-three-rapier/HEAD/packages/react-three-rapier/misc/example-compound-shapes.jpg" width="240" /></a>
200
236
 
@@ -204,6 +240,8 @@ Instanced meshes can also be used and have automatic colliders generated from th
204
240
 
205
241
  By wrapping the `InstancedMesh` in `<InstancedRigidBodies />`, each instance will be attached to an individual `RigidBody`.
206
242
 
243
+ 🧩 See [InstancedRigidBodiesProps docs](https://pmndrs.github.io/react-three-rapier/interfaces/InstancedRigidBodiesProps.html) for available props.
244
+
207
245
  ```tsx
208
246
  import { InstancedRigidBodies } from "@react-three/rapier";
209
247
 
@@ -289,6 +327,8 @@ const Scene = () => {
289
327
 
290
328
  You can subscribe to collision and state events on a RigidBody:
291
329
 
330
+ 🧩 See [onCollisionEnter / onCollisionExit docs](https://pmndrs.github.io/react-three-rapier/interfaces/RigidBodyProps.html#onCollisionEnter) for more information.
331
+
292
332
  ```tsx
293
333
  const RigidBottle = () => {
294
334
  const [isAsleep, setIsAsleep] = useState(false);
@@ -434,6 +474,10 @@ A Collider can be set to be a sensor, which means that it will not generate any
434
474
 
435
475
  To detect when a collider enters or leaves another collider, you can use the `onIntersectionEnter` and `onIntersectionExit` events on the collider.
436
476
 
477
+ 🧩 See [onIntersectionEnter / onIntersectionExit docs](https://pmndrs.github.io/react-three-rapier/interfaces/RigidBodyProps.html#onIntersectionEnter) for more information.
478
+
479
+ ```tsx
480
+
437
481
  ```tsx
438
482
  <RigidBody>
439
483
  <GoalPosts />
@@ -446,7 +490,7 @@ To detect when a collider enters or leaves another collider, you can use the `on
446
490
  </RigidBody>
447
491
  ```
448
492
 
449
- ### Sensors Example
493
+ ### 🖼 Sensors Example
450
494
  <a href="https://codesandbox.io/s/react-three-rapier-sensors-byjmsk"><img src="https://raw.githubusercontent.com/pmndrs/react-three-rapier/HEAD/packages/react-three-rapier/misc/example-sensors.jpg" width="240" /></a>
451
495
 
452
496
  ## Attractors
@@ -456,6 +500,8 @@ Setting the `strength` to a negative value will cause the `RigidBody` to be _pus
456
500
 
457
501
  The force applied to rigid-bodies within range is calculated differently depending on the `type`.
458
502
 
503
+ 🧩 See [Attractor docs](https://pmndrs.github.io/react-three-rapier/interfaces/AttractorProps.html) for all available props.
504
+
459
505
  ```tsx
460
506
  // Standard attractor
461
507
  <Attractor range={10} strength={5} type="linear" position={[5, -5, 0]} />
@@ -483,7 +529,7 @@ Gravity types:
483
529
  - `mass2` is the mass of the rigid-body at the time of calculation. Note that rigid-bodies with colliders will use the mass provided to the collider. This is not a value you can control from the attractor, only from wherever you're creating rigid-body components in the scene.
484
530
  - `distance` is the distance between the attractor and rigid-body at the time of calculation
485
531
 
486
- ### Attractors Example
532
+ ### 🖼 Attractors Example
487
533
  <a href="https://codesandbox.io/s/react-three-rapier-attractors-oyj640"><img src="https://raw.githubusercontent.com/pmndrs/react-three-rapier/HEAD/packages/react-three-rapier/misc/example-attractors.jpg" width="240" /></a>
488
534
 
489
535
  ## Configuring Time Step Size
@@ -502,16 +548,6 @@ The `timeStep` prop may also be set to `"vary"`, which will cause the simulation
502
548
 
503
549
  > **Note** This is useful for games that run at variable frame rates, but may cause instability in the simulation. It also prevents the physics simulation from being fully deterministic. Please use with care!
504
550
 
505
- ## Manual stepping
506
-
507
- You can also manually step the physics simulation by calling the `step` method from the `useRapier` hook.
508
-
509
- ```tsx
510
- const { step } = useRapier();
511
-
512
- step(1 / 60);
513
- ```
514
-
515
551
  ## Joints
516
552
  Joints can be made between two `RigidBodies` to provide a way to restrict a motion of a body in relation to another.
517
553
  > Read more about joints in Rapier: https://rapier.rs/docs/user_guides/javascript/joints
@@ -527,6 +563,8 @@ There are 4 different joint types available:
527
563
  ### Fixed Joint
528
564
  A fixed joint ensures that two rigid-bodies don't move relative to each other. Fixed joints are characterized by one local frame (represented by an isometry) on each rigid-body. The fixed-joint makes these frames coincide in world-space.
529
565
 
566
+ 🧩 See [FixedJoint docs](https://pmndrs.github.io/react-three-rapier/functions/useFixedJoint.html) for available options.
567
+
530
568
  ```tsx
531
569
  const JointedThing = () => {
532
570
  const joint = useFixedJoint(
@@ -555,6 +593,10 @@ const JointedThing = () => {
555
593
  ### Spherical Joint
556
594
  The spherical joint ensures that two points on the local-spaces of two rigid-bodies always coincide (it prevents any relative translational motion at this points).
557
595
 
596
+ 🧩 See [SphericalJoint docs](https://pmndrs.github.io/react-three-rapier/functions/useSphericalJoint.html) for available options.
597
+
598
+ ```tsx
599
+
558
600
  ```tsx
559
601
  const JointedThing = () => {
560
602
  const joint = useSphericalJoint(
@@ -581,6 +623,8 @@ const JointedThing = () => {
581
623
  ### Revolute Joint
582
624
  The revolute joint prevents any relative movement between two rigid-bodies, except for relative rotations along one axis. This is typically used to simulate wheels, fans, etc.
583
625
 
626
+ 🧩 See [RevoluteJoint docs](https://pmndrs.github.io/react-three-rapier/functions/useRevoluteJoint.html) for available options.
627
+
584
628
  ```tsx
585
629
  const JointedThing = () => {
586
630
  const joint = useRevoluteJoint(
@@ -608,6 +652,10 @@ const JointedThing = () => {
608
652
  ### Prismatic Joint
609
653
  The prismatic joint prevents any relative movement between two rigid-bodies, except for relative translations along one axis.
610
654
 
655
+ 🧩 See [PrismaticJoint docs](https://pmndrs.github.io/react-three-rapier/functions/usePrismaticJoint.html) for available options.
656
+
657
+ ```tsx
658
+
611
659
  ```tsx
612
660
  const JointedThing = () => {
613
661
  const joint = usePrismaticJoint(
@@ -635,6 +683,8 @@ const JointedThing = () => {
635
683
  ### Joint APIs
636
684
  Joints can be controlled imperatively similarily to how `RigidBody` components can be controlled.
637
685
 
686
+ 🧩 See [Joint API docs](https://pmndrs.github.io/react-three-rapier/interfaces/JointApi.html) for more information.
687
+
638
688
  ```tsx
639
689
  const JointedThing = () => {
640
690
  const joint = useSphericalJoint(...)
@@ -651,5 +701,29 @@ const JointedThing = () => {
651
701
  ```
652
702
 
653
703
 
654
- ### Joints Example
655
- <a href="https://codesandbox.io/s/react-three-rapier-joints-mhhbd4"><img src="https://raw.githubusercontent.com/pmndrs/react-three-rapier/HEAD/packages/react-three-rapier/misc/example-joints.jpg" width="240" /></a>
704
+ ### 🖼 Joints Example
705
+ <a href="https://codesandbox.io/s/react-three-rapier-joints-mhhbd4"><img src="https://raw.githubusercontent.com/pmndrs/react-three-rapier/HEAD/packages/react-three-rapier/misc/example-joints.jpg" width="240" /></a>
706
+
707
+ ## Advanced hooks usage
708
+
709
+ Advanced users might need granular access to the physics loop and direct access to the `world` instance. This can be done by using the following hooks:
710
+
711
+ - `useRapier`
712
+ Gives you access to the `world`, direct access to `rapier`, and more.
713
+ 🧩 See [useRapier docs](https://pmndrs.github.io/react-three-rapier/interfaces/RapierContext.html) for more information.
714
+ - `useBeforePhysicsStep`
715
+ Allows you to run code before the physics simulation is stepped.
716
+ 🧩 See [useBeforePhysicsStep docs](https://pmndrs.github.io/react-three-rapier/functions/useBeforePhysicsStep.html) for more information.
717
+ - `useAfterPhysicsStep`
718
+ Allows you to run code after the physics simulation is stepped.
719
+ 🧩 See [useAfterPhysicsStep docs](https://pmndrs.github.io/react-three-rapier/functions/useAfterPhysicsStep.html) for more information.
720
+
721
+ ### Manual stepping
722
+
723
+ You can manually step the physics simulation by calling the `step` method from the `useRapier` hook.
724
+
725
+ ```tsx
726
+ const { step } = useRapier();
727
+
728
+ step(1 / 60);
729
+ ```