@react-three/rapier 1.0.1 → 1.1.1

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.
@@ -359,9 +359,8 @@ export declare type FixedJointParams = [
359
359
  ];
360
360
  export declare type PrismaticJointParams = [
361
361
  body1Anchor: Vector3Tuple,
362
- body1LocalFrame: Vector3Tuple,
363
362
  body2Anchor: Vector3Tuple,
364
- body2LocalFrame: Vector3Tuple,
363
+ axis: Vector3Tuple,
365
364
  limits?: [min: number, max: number]
366
365
  ];
367
366
  export declare type RevoluteJointParams = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
@@ -10,18 +10,18 @@
10
10
  ],
11
11
  "scripts": {},
12
12
  "devDependencies": {
13
- "@react-three/drei": "9.45.0",
13
+ "@react-three/drei": "9.74.14",
14
14
  "@react-three/fiber": "8.9.1",
15
15
  "@react-three/test-renderer": "8.0.17",
16
16
  "@types/react-dom": "18.0.2",
17
- "@types/three": "0.139.0",
17
+ "@types/three": "^0.152.1",
18
18
  "@vitejs/plugin-react": "^2.1.0",
19
- "@vitest/ui": "0.29.7",
19
+ "@vitest/ui": "0.32.0",
20
20
  "happy-dom": "9.19.2",
21
21
  "react": "18.2.0",
22
22
  "react-dom": "18.2.0",
23
23
  "three": "0.146.0",
24
- "vitest": "0.31.1"
24
+ "vitest": "0.32.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@react-three/fiber": ">=8.9.0",
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@dimforge/rapier3d-compat": "0.11.2",
33
- "three-stdlib": "2.21.6",
33
+ "three-stdlib": "2.23.9",
34
34
  "use-asset": "1.0.4"
35
35
  },
36
36
  "repository": "https://github.com/pmndrs/react-three-rapier/tree/master/packages/react-three-rapier"
package/readme.md CHANGED
@@ -38,7 +38,7 @@ const App = () => {
38
38
  <Torus />
39
39
  </RigidBody>
40
40
 
41
- <CuboidCollider position={[0, -2, 0]} args={[20, .5, 20]} />
41
+ <CuboidCollider position={[0, -2, 0]} args={[20, 0.5, 20]} />
42
42
  </Physics>
43
43
  </Suspense>
44
44
  </Canvas>
@@ -50,6 +50,7 @@ const App = () => {
50
50
 
51
51
  <!-- omit from toc -->
52
52
  ## 📝 Readme note
53
+
53
54
  Below follows a guide on core concepts for `react-three/rapier`.
54
55
  For full API outline and documentation, see 🧩 [API Docs](https://pmndrs.github.io/react-three-rapier/).
55
56
 
@@ -85,6 +86,7 @@ For full API outline and documentation, see 🧩 [API Docs](https://pmndrs.githu
85
86
  ---
86
87
 
87
88
  ## The Physics Component
89
+
88
90
  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 />`.
89
91
 
90
92
  🧩 See [PhysicsProps docs](https://pmndrs.github.io/react-three-rapier/interfaces/PhysicsProps.html) for available props.
@@ -94,20 +96,17 @@ const Scene = () => {
94
96
  return (
95
97
  <Canvas>
96
98
  <Suspense>
97
- <Physics
98
- gravity={[0,1,0]}
99
- interpolation={false}
100
- colliders={false}
101
- >
99
+ <Physics gravity={[0, 1, 0]} interpolation={false} colliders={false}>
102
100
  ...
103
101
  </Physics>
104
102
  </Suspense>
105
103
  </Canvas>
106
104
  );
107
- }
105
+ };
108
106
  ```
109
107
 
110
108
  ## The RigidBody Component
109
+
111
110
  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)).
112
111
 
113
112
  🧩 See [RigidBodyProps docs](https://pmndrs.github.io/react-three-rapier/interfaces/RigidBodyProps.html) for available props.
@@ -228,6 +227,7 @@ If part of our meshes are invisible and you want to include them in the collider
228
227
  ```
229
228
 
230
229
  ### 🖼 Collider Examples
230
+
231
231
  <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>
232
232
  <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>
233
233
 
@@ -249,7 +249,7 @@ const Scene = () => {
249
249
 
250
250
  useEffect(() => {
251
251
  if (!rigidBodies.current) {
252
- return
252
+ return;
253
253
  }
254
254
 
255
255
  // You can access individual instanced by their index
@@ -270,9 +270,9 @@ const Scene = () => {
270
270
 
271
271
  for (let i = 0; i < COUNT; i++) {
272
272
  instances.push({
273
- key: 'instance_' + Math.random(),
273
+ key: "instance_" + Math.random(),
274
274
  position: [Math.random() * 10, Math.random() * 10, Math.random() * 10],
275
- rotation: [Math.random(), Math.random(), Math.random()],
275
+ rotation: [Math.random(), Math.random(), Math.random()]
276
276
  });
277
277
  }
278
278
 
@@ -294,8 +294,12 @@ const Scene = () => {
294
294
  We can also create compound shapes for instanced meshes by providing an array of `Colliders` in the `colliderNodes` prop.
295
295
 
296
296
  ```tsx
297
- import { InstancedRigidBodies, BoxCollider, SphereCollider } from "@react-three/rapier";
298
- const COUNT = 500
297
+ import {
298
+ InstancedRigidBodies,
299
+ BoxCollider,
300
+ SphereCollider
301
+ } from "@react-three/rapier";
302
+ const COUNT = 500;
299
303
 
300
304
  const Scene = () => {
301
305
  const instances = useMemo(() => {
@@ -303,9 +307,9 @@ const Scene = () => {
303
307
 
304
308
  for (let i = 0; i < COUNT; i++) {
305
309
  instances.push({
306
- key: 'instance_' + Math.random(),
310
+ key: "instance_" + Math.random(),
307
311
  position: [Math.random() * 10, Math.random() * 10, Math.random() * 10],
308
- rotation: [Math.random(), Math.random(), Math.random()],
312
+ rotation: [Math.random(), Math.random(), Math.random()]
309
313
  });
310
314
  }
311
315
 
@@ -318,7 +322,7 @@ const Scene = () => {
318
322
  colliders="ball"
319
323
  colliderNodes={[
320
324
  <BoxCollider args={[0.5, 0.5, 0.5]} />,
321
- <SphereCollider args={[0.5]} />,
325
+ <SphereCollider args={[0.5]} />
322
326
  ]}
323
327
  >
324
328
  <instancedMesh args={[undefined, undefined, COUNT]} count={COUNT} />
@@ -350,6 +354,7 @@ const Scene = () => {
350
354
  ```
351
355
 
352
356
  ## Moving things around, and applying forces
357
+
353
358
  You can access the instance for a RigidBody by storing its `ref`. This allows you to perform any operation on the underlying physics object directly.
354
359
 
355
360
  `r3/rapier` exposes a `RapierRigidBody` and `RapierCollider` as aliases for `rapiers` underlying base objects.
@@ -357,10 +362,7 @@ You can access the instance for a RigidBody by storing its `ref`. This allows yo
357
362
  For all available methods, see the [Rapier docs](https://rapier.rs/javascript3d/classes/RigidBody.html).
358
363
 
359
364
  ```tsx
360
- import {
361
- RigidBody,
362
- RapierRigidBody
363
- } from "@react-three/rapier";
365
+ import { RigidBody, RapierRigidBody } from "@react-three/rapier";
364
366
 
365
367
  const Scene = () => {
366
368
  const rigidBody = useRef<RapierRigidBody>(null);
@@ -395,28 +397,25 @@ const Scene = () => {
395
397
  Rapier's API returns quaternions and vectors that are not compatible with Three.js, `r3/rapier` therefore exposes some helper functions (`vec3`, `quat`, `euler`) for quick type conversions. These helper functions can also be used as a shorthand for creating new objects.
396
398
 
397
399
  ```tsx
398
- import {
399
- RapierRigidBody,
400
- quat,
401
- vec3,
402
- euler
403
- } from "@react-three/rapier";
400
+ import { RapierRigidBody, quat, vec3, euler } from "@react-three/rapier";
404
401
 
405
402
  const Scene = () => {
406
- const rigidBody = useRef<RapierRigidBody>(null)
403
+ const rigidBody = useRef<RapierRigidBody>(null);
407
404
 
408
405
  useEffect(() => {
409
406
  if (rigidBody.current) {
410
- const position = vec3(rigidBody.current.translation())
411
- const quaternion = quat(rigidBody.current.rotation())
412
- const eulerRot = euler().setFromQuaternion(quat(rigidBody.current.rotation()))
407
+ const position = vec3(rigidBody.current.translation());
408
+ const quaternion = quat(rigidBody.current.rotation());
409
+ const eulerRot = euler().setFromQuaternion(
410
+ quat(rigidBody.current.rotation())
411
+ );
413
412
 
414
413
  // While Rapier's return types need conversion, setting values can be done directly with Three.js types
415
- rigidBody.current.setTranslation(position, true)
416
- rigidBody.current.setRotation(quaternion, true)
417
- rigidBody.current.setAngVel({x: 0, y: 2, z: 0}, true)
414
+ rigidBody.current.setTranslation(position, true);
415
+ rigidBody.current.setRotation(quaternion, true);
416
+ rigidBody.current.setAngVel({ x: 0, y: 2, z: 0 }, true);
418
417
  }
419
- }, [])
418
+ }, []);
420
419
 
421
420
  return (
422
421
  <RigidBody ref={rigidBody}>
@@ -426,8 +425,7 @@ const Scene = () => {
426
425
  </mesh>
427
426
  </RigidBody>
428
427
  );
429
- }
430
-
428
+ };
431
429
  ```
432
430
 
433
431
  ## Collision Events
@@ -541,9 +539,10 @@ Contact force events are triggered on `<RigidBody>` and any collider components
541
539
  colliders="ball"
542
540
  onContactForce={(payload) => {
543
541
  console.log(`The total force generated was: ${payload.totalForce}`);
544
- }}>
542
+ }}
543
+ >
545
544
  <Sphere>
546
- <meshPhysicalMaterial color={'grey'} />
545
+ <meshPhysicalMaterial color={"grey"} />
547
546
  </Sphere>
548
547
  </RigidBody>
549
548
  ```
@@ -596,6 +595,7 @@ To detect when a collider enters or leaves another collider, you can use the `on
596
595
  ```
597
596
 
598
597
  ### 🖼 Sensors Example
598
+
599
599
  <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>
600
600
 
601
601
  ## Configuring Time Step Size
@@ -615,53 +615,58 @@ The `timeStep` prop may also be set to `"vary"`, which will cause the simulation
615
615
  > **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!
616
616
 
617
617
  ## Joints
618
+
618
619
  Joints can be made between two `RigidBodies` to provide a way to restrict a motion of a body in relation to another.
620
+
619
621
  > Read more about joints in Rapier: https://rapier.rs/docs/user_guides/javascript/joints
620
622
 
621
623
  Joints are available in `r3/rapier` as hooks.
622
624
 
623
625
  There are 4 different joint types available:
626
+
624
627
  - Fixed (two bodies are fixed together)
625
628
  - Spherical (two bodies are connected by a ball and socket, for things like arms or chains)
626
629
  - Revolute (two bodies are connected by a hinge, for things like doors or wheels)
627
630
  - Prismatic (two bodies are connected by a sliding joint, for things like pistons or sliders)
628
631
 
629
- Each joint hook returns a RefObject containing the raw reference to the joint instance.
632
+ Each joint hook returns a RefObject containing the raw reference to the joint instance.
630
633
 
631
634
  ```tsx
632
- const WheelJoint = ({bodyA, bodyB}) => {
633
- const joint = useRevoluteJoint(bodyA, bodyB, [[0,0,0],[0,0,0],[0,0,0]])
635
+ const WheelJoint = ({ bodyA, bodyB }) => {
636
+ const joint = useRevoluteJoint(bodyA, bodyB, [
637
+ [0, 0, 0],
638
+ [0, 0, 0],
639
+ [0, 0, 0]
640
+ ]);
634
641
 
635
642
  useFrame(() => {
636
643
  if (joint.current) {
637
- joint.current.configureMotorVelocity(10, 2)
644
+ joint.current.configureMotorVelocity(10, 2);
638
645
  }
639
- }, [])
646
+ }, []);
640
647
 
641
- return null
642
- }
648
+ return null;
649
+ };
643
650
  ```
644
651
 
645
652
  ### Fixed Joint
653
+
646
654
  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.
647
655
 
648
656
  🧩 See [FixedJoint docs](https://pmndrs.github.io/react-three-rapier/functions/useFixedJoint.html) for available options.
649
657
 
650
658
  ```tsx
651
659
  const JointedThing = () => {
652
- const joint = useFixedJoint(
653
- bodyA,
654
- bodyB,
655
- [
656
- // Position of the joint in bodyA's local space
657
- [0, 0, 0],
658
- // Orientation of the joint in bodyA's local space
659
- [0, 0, 0, 1],
660
- // Position of the joint in bodyB's local space
661
- [0, 0, 0],
662
- // Orientation of the joint in bodyB's local space
663
- [0, 0, 0, 1],
664
- ]);
660
+ const joint = useFixedJoint(bodyA, bodyB, [
661
+ // Position of the joint in bodyA's local space
662
+ [0, 0, 0],
663
+ // Orientation of the joint in bodyA's local space
664
+ [0, 0, 0, 1],
665
+ // Position of the joint in bodyB's local space
666
+ [0, 0, 0],
667
+ // Orientation of the joint in bodyB's local space
668
+ [0, 0, 0, 1]
669
+ ]);
665
670
 
666
671
  return (
667
672
  <group>
@@ -673,25 +678,23 @@ const JointedThing = () => {
673
678
  </RigidBody>
674
679
  </group>
675
680
  );
676
- }
681
+ };
677
682
  ```
678
683
 
679
684
  ### Spherical Joint
685
+
680
686
  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).
681
687
 
682
688
  🧩 See [SphericalJoint docs](https://pmndrs.github.io/react-three-rapier/functions/useSphericalJoint.html) for available options.
683
689
 
684
690
  ```tsx
685
691
  const JointedThing = () => {
686
- const joint = useSphericalJoint(
687
- bodyA,
688
- bodyB,
689
- [
690
- // Position of the joint in bodyA's local space
691
- [0, 0, 0],
692
- // Position of the joint in bodyB's local space
693
- [0, 0, 0],
694
- ]);
692
+ const joint = useSphericalJoint(bodyA, bodyB, [
693
+ // Position of the joint in bodyA's local space
694
+ [0, 0, 0],
695
+ // Position of the joint in bodyB's local space
696
+ [0, 0, 0]
697
+ ]);
695
698
 
696
699
  return (
697
700
  <group>
@@ -703,28 +706,26 @@ const JointedThing = () => {
703
706
  </RigidBody>
704
707
  </group>
705
708
  );
706
- }
709
+ };
707
710
  ```
708
711
 
709
712
  ### Revolute Joint
713
+
710
714
  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.
711
715
 
712
716
  🧩 See [RevoluteJoint docs](https://pmndrs.github.io/react-three-rapier/functions/useRevoluteJoint.html) for available options.
713
717
 
714
718
  ```tsx
715
719
  const JointedThing = () => {
716
- const joint = useRevoluteJoint(
717
- bodyA,
718
- bodyB,
719
- [
720
- // Position of the joint in bodyA's local space
721
- [0, 0, 0],
722
- // Position of the joint in bodyB's local space
723
- [0, 0, 0],
724
- // Axis of the joint, expressed in the local-space of
725
- // the rigid-bodies it is attached to. Cannot be [0,0,0].
726
- [0, 1, 0],
727
- ]);
720
+ const joint = useRevoluteJoint(bodyA, bodyB, [
721
+ // Position of the joint in bodyA's local space
722
+ [0, 0, 0],
723
+ // Position of the joint in bodyB's local space
724
+ [0, 0, 0],
725
+ // Axis of the joint, expressed in the local-space of
726
+ // the rigid-bodies it is attached to. Cannot be [0,0,0].
727
+ [0, 1, 0]
728
+ ]);
728
729
 
729
730
  return (
730
731
  <group>
@@ -736,28 +737,26 @@ const JointedThing = () => {
736
737
  </RigidBody>
737
738
  </group>
738
739
  );
739
- }
740
+ };
740
741
  ```
741
742
 
742
743
  ### Prismatic Joint
744
+
743
745
  The prismatic joint prevents any relative movement between two rigid-bodies, except for relative translations along one axis.
744
746
 
745
747
  🧩 See [PrismaticJoint docs](https://pmndrs.github.io/react-three-rapier/functions/usePrismaticJoint.html) for available options.
746
748
 
747
749
  ```tsx
748
750
  const JointedThing = () => {
749
- const joint = usePrismaticJoint(
750
- bodyA,
751
- bodyB,
752
- [
753
- // Position of the joint in bodyA's local space
754
- [0, 0, 0],
755
- // Position of the joint in bodyB's local space
756
- [0, 0, 0],
757
- // Axis of the joint, expressed in the local-space of
758
- // the rigid-bodies it is attached to. Cannot be [0,0,0].
759
- [0, 1, 0],
760
- ]);
751
+ const joint = usePrismaticJoint(bodyA, bodyB, [
752
+ // Position of the joint in bodyA's local space
753
+ [0, 0, 0],
754
+ // Position of the joint in bodyB's local space
755
+ [0, 0, 0],
756
+ // Axis of the joint, expressed in the local-space of
757
+ // the rigid-bodies it is attached to. Cannot be [0,0,0].
758
+ [0, 1, 0]
759
+ ]);
761
760
 
762
761
  return (
763
762
  <group>
@@ -769,10 +768,11 @@ const JointedThing = () => {
769
768
  </RigidBody>
770
769
  </group>
771
770
  );
772
- }
771
+ };
773
772
  ```
774
773
 
775
774
  ### 🖼 Joints Example
775
+
776
776
  <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>
777
777
 
778
778
  ## Advanced hooks usage
@@ -800,6 +800,7 @@ step(1 / 60);
800
800
  ```
801
801
 
802
802
  ### On-demand rendering
803
+
803
804
  By default `@react-three/rapier` will update the physics simulation when a frame renders. This is fine for most cases, but if you want to only render the scene when things have changed, you need to run the physics simulation independently from the render loop.
804
805
 
805
806
  - See https://docs.pmnd.rs/react-three-fiber/advanced/scaling-performance#on-demand-rendering, for info on on-demand rendering in `@react-tree/fiber`.