@react-three/rapier 0.7.6 → 0.7.7

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.
@@ -101,12 +101,12 @@ export interface RigidBodyApi {
101
101
  */
102
102
  setNextKinematicRotation(rotation: Quaternion): void;
103
103
  /**
104
- * If this rigid body is kinematic, sets its future rotation after the next timestep integration.
104
+ * If this rigid body is kinematic, sets its future translation after the next timestep integration.
105
105
  *
106
- * This should be used instead of rigidBody.setRotation to make the dynamic object interacting with
107
- * this kinematic body behave as expected. Internally, Rapier will compute an artificial velocity
108
- * for this rigid-body from its current position and its next kinematic position. This velocity
109
- * will be used to compute forces on dynamic bodies interacting with this body.
106
+ * This should be used instead of rigidBody.setTranslation to make the dynamic object interacting with
107
+ * this kinematic body behave as expected. Internally, Rapier will compute an artificial velocity
108
+ * for this rigid-body from its current position and its next kinematic position. This velocity
109
+ * will be used to compute forces on dynamic bodies interacting with this body.
110
110
  */
111
111
  setNextKinematicTranslation(translation: Vector3Object): void;
112
112
  /**
@@ -31,4 +31,4 @@ import { InteractionGroups } from "@dimforge/rapier3d-compat";
31
31
  * @param filters Groups the interaction group should filter against. (Values can range from 0 to 15.)
32
32
  * @returns An InteractionGroup bitmask.
33
33
  */
34
- export declare const interactionGroups: (memberships: number | number[], filters?: number | number[] | undefined) => InteractionGroups;
34
+ export declare const interactionGroups: (memberships: number | number[], filters?: number | number[]) => InteractionGroups;
@@ -19,7 +19,7 @@ export declare type CuboidArgs = [
19
19
  halfDepth: number
20
20
  ];
21
21
  export declare type BallArgs = [radius: number];
22
- export declare type CapsuleArgs = [radius: number, height: number];
22
+ export declare type CapsuleArgs = [halfHeight: number, radius: number];
23
23
  export declare type ConvexHullArgs = [vertices: ArrayLike<number>];
24
24
  export declare type HeightfieldArgs = [
25
25
  width: number,
@@ -42,16 +42,16 @@ export declare type RoundCuboidArgs = [
42
42
  halfDepth: number,
43
43
  borderRadius: number
44
44
  ];
45
- export declare type CylinderArgs = [radius: number, height: number];
45
+ export declare type CylinderArgs = [halfHeight: number, radius: number];
46
46
  export declare type RoundCylinderArgs = [
47
+ halfHeight: number,
47
48
  radius: number,
48
- height: number,
49
49
  borderRadius: number
50
50
  ];
51
- export declare type ConeArgs = [radius: number, height: number];
51
+ export declare type ConeArgs = [halfHeight: number, radius: number];
52
52
  export declare type RoundConeArgs = [
53
+ halfHeight: number,
53
54
  radius: number,
54
- height: number,
55
55
  borderRadius: number
56
56
  ];
57
57
  export declare type ConvexMeshArgs = [
@@ -14,12 +14,12 @@ export declare const scaleColliderArgs: (shape: ColliderShape, args: (number | A
14
14
  y: number;
15
15
  z: number;
16
16
  })[];
17
- export declare const createColliderFromOptions: (options: ColliderProps, world: WorldApi, scale: Vector3, rigidBody?: RigidBody | undefined) => Collider;
17
+ export declare const createColliderFromOptions: (options: ColliderProps, world: WorldApi, scale: Vector3, rigidBody?: RigidBody) => Collider;
18
18
  declare type ImmutableColliderOptions = (keyof ColliderProps)[];
19
19
  export declare const immutableColliderOptions: ImmutableColliderOptions;
20
20
  export declare const setColliderOptions: (collider: Collider, options: ColliderProps, states: ColliderStateMap) => void;
21
21
  export declare const useUpdateColliderOptions: (collidersRef: MutableRefObject<Collider[]>, props: ColliderProps, states: ColliderStateMap) => void;
22
- export declare const createColliderState: (collider: Collider, object: Object3D, rigidBodyObject?: Object3D<import("three").Event> | null | undefined) => ColliderState;
22
+ export declare const createColliderState: (collider: Collider, object: Object3D, rigidBodyObject?: Object3D | null) => ColliderState;
23
23
  interface CreateColliderPropsFromChildren {
24
24
  (options: {
25
25
  object: Object3D;
@@ -198,10 +198,19 @@ const createWorldApi = ref => {
198
198
  getRigidBody: handle => ref.current().getRigidBody(handle),
199
199
  createRigidBody: desc => ref.current().createRigidBody(desc),
200
200
  createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
201
- removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
202
- removeCollider: (collider, wakeUp = true) => ref.current().removeCollider(collider, wakeUp),
201
+ removeRigidBody: rigidBody => {
202
+ if (!ref.current().bodies.contains(rigidBody.handle)) return;
203
+ ref.current().removeRigidBody(rigidBody);
204
+ },
205
+ removeCollider: (collider, wakeUp = true) => {
206
+ if (!ref.current().colliders.contains(collider.handle)) return;
207
+ ref.current().removeCollider(collider, wakeUp);
208
+ },
203
209
  createImpulseJoint: (params, rigidBodyA, rigidBodyB, wakeUp = true) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, wakeUp),
204
- removeImpulseJoint: (joint, wakeUp = true) => ref.current().removeImpulseJoint(joint, wakeUp),
210
+ removeImpulseJoint: (joint, wakeUp = true) => {
211
+ if (!ref.current().impulseJoints.contains(joint.handle)) return;
212
+ ref.current().removeImpulseJoint(joint, wakeUp);
213
+ },
205
214
  forEachCollider: callback => ref.current().forEachCollider(callback),
206
215
  setGravity: ({
207
216
  x,
@@ -245,10 +254,6 @@ const Physics = ({
245
254
  updatePriority
246
255
  }) => {
247
256
  const rapier = useAsset.useAsset(importRapier);
248
- const [isPaused, setIsPaused] = React.useState(_paused);
249
- React.useEffect(() => {
250
- setIsPaused(_paused);
251
- }, [_paused]);
252
257
  const worldRef = React.useRef();
253
258
  const getWorldRef = React.useRef(() => {
254
259
  if (!worldRef.current) {
@@ -269,6 +274,7 @@ const Physics = ({
269
274
  return () => {
270
275
  if (world) {
271
276
  world.free();
277
+ worldRef.current = undefined;
272
278
  }
273
279
  };
274
280
  }, []); // Update gravity
@@ -502,15 +508,15 @@ const Physics = ({
502
508
  colliderStates,
503
509
  rigidBodyEvents,
504
510
  colliderEvents,
505
- isPaused
506
- }), [isPaused]);
511
+ isPaused: _paused
512
+ }), [_paused]);
507
513
  return /*#__PURE__*/React__default["default"].createElement(RapierContext.Provider, {
508
514
  value: context
509
515
  }, children);
510
516
  };
511
517
 
512
518
  function _extends() {
513
- _extends = Object.assign || function (target) {
519
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
514
520
  for (var i = 1; i < arguments.length; i++) {
515
521
  var source = arguments[i];
516
522
 
@@ -523,7 +529,6 @@ function _extends() {
523
529
 
524
530
  return target;
525
531
  };
526
-
527
532
  return _extends.apply(this, arguments);
528
533
  }
529
534
 
@@ -697,11 +702,11 @@ const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslatio
697
702
  return vectorToTuple(props[key]);
698
703
  }), [props]);
699
704
  React.useEffect(() => {
700
- if ("length" in rigidBodyRef.current) {
701
- rigidBodyRef.current.forEach(rigidBody => {
705
+ if (Array.isArray(rigidBodyRef.current)) {
706
+ for (const rigidBody of rigidBodyRef.current) {
702
707
  setRigidBodyOptions(rigidBody, props, states, updateTranslations);
703
- });
704
- } else {
708
+ }
709
+ } else if (rigidBodyRef.current) {
705
710
  setRigidBodyOptions(rigidBodyRef.current, props, states, updateTranslations);
706
711
  }
707
712
  }, mutablePropsAsFlatArray);
@@ -724,20 +729,20 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
724
729
  onIntersectionExit
725
730
  };
726
731
  React.useEffect(() => {
727
- if ("length" in rigidBodyRef.current) {
728
- rigidBodyRef.current.forEach(rigidBody => {
732
+ if (Array.isArray(rigidBodyRef.current)) {
733
+ for (const rigidBody of rigidBodyRef.current) {
729
734
  events.set(rigidBody.handle, eventHandlers);
730
- });
731
- } else {
735
+ }
736
+ } else if (rigidBodyRef.current) {
732
737
  events.set(rigidBodyRef.current.handle, eventHandlers);
733
738
  }
734
739
 
735
740
  return () => {
736
- if ("length" in rigidBodyRef.current) {
737
- rigidBodyRef.current.forEach(rigidBody => {
741
+ if (Array.isArray(rigidBodyRef.current)) {
742
+ for (const rigidBody of rigidBodyRef.current) {
738
743
  events.delete(rigidBody.handle);
739
- });
740
- } else {
744
+ }
745
+ } else if (rigidBodyRef.current) {
741
746
  events.delete(rigidBodyRef.current.handle);
742
747
  }
743
748
  };
@@ -1078,6 +1083,7 @@ const useRigidBody = (options = {}) => {
1078
1083
  return () => {
1079
1084
  world.removeRigidBody(rigidBody);
1080
1085
  rigidBodyStates.delete(rigidBody.handle);
1086
+ rigidBodyRef.current = undefined;
1081
1087
  };
1082
1088
  }, []);
1083
1089
  useUpdateRigidBodyOptions(rigidBodyRef, mergedOptions, rigidBodyStates);
@@ -198,10 +198,19 @@ const createWorldApi = ref => {
198
198
  getRigidBody: handle => ref.current().getRigidBody(handle),
199
199
  createRigidBody: desc => ref.current().createRigidBody(desc),
200
200
  createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
201
- removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
202
- removeCollider: (collider, wakeUp = true) => ref.current().removeCollider(collider, wakeUp),
201
+ removeRigidBody: rigidBody => {
202
+ if (!ref.current().bodies.contains(rigidBody.handle)) return;
203
+ ref.current().removeRigidBody(rigidBody);
204
+ },
205
+ removeCollider: (collider, wakeUp = true) => {
206
+ if (!ref.current().colliders.contains(collider.handle)) return;
207
+ ref.current().removeCollider(collider, wakeUp);
208
+ },
203
209
  createImpulseJoint: (params, rigidBodyA, rigidBodyB, wakeUp = true) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, wakeUp),
204
- removeImpulseJoint: (joint, wakeUp = true) => ref.current().removeImpulseJoint(joint, wakeUp),
210
+ removeImpulseJoint: (joint, wakeUp = true) => {
211
+ if (!ref.current().impulseJoints.contains(joint.handle)) return;
212
+ ref.current().removeImpulseJoint(joint, wakeUp);
213
+ },
205
214
  forEachCollider: callback => ref.current().forEachCollider(callback),
206
215
  setGravity: ({
207
216
  x,
@@ -245,10 +254,6 @@ const Physics = ({
245
254
  updatePriority
246
255
  }) => {
247
256
  const rapier = useAsset.useAsset(importRapier);
248
- const [isPaused, setIsPaused] = React.useState(_paused);
249
- React.useEffect(() => {
250
- setIsPaused(_paused);
251
- }, [_paused]);
252
257
  const worldRef = React.useRef();
253
258
  const getWorldRef = React.useRef(() => {
254
259
  if (!worldRef.current) {
@@ -269,6 +274,7 @@ const Physics = ({
269
274
  return () => {
270
275
  if (world) {
271
276
  world.free();
277
+ worldRef.current = undefined;
272
278
  }
273
279
  };
274
280
  }, []); // Update gravity
@@ -502,15 +508,15 @@ const Physics = ({
502
508
  colliderStates,
503
509
  rigidBodyEvents,
504
510
  colliderEvents,
505
- isPaused
506
- }), [isPaused]);
511
+ isPaused: _paused
512
+ }), [_paused]);
507
513
  return /*#__PURE__*/React__default["default"].createElement(RapierContext.Provider, {
508
514
  value: context
509
515
  }, children);
510
516
  };
511
517
 
512
518
  function _extends() {
513
- _extends = Object.assign || function (target) {
519
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
514
520
  for (var i = 1; i < arguments.length; i++) {
515
521
  var source = arguments[i];
516
522
 
@@ -523,7 +529,6 @@ function _extends() {
523
529
 
524
530
  return target;
525
531
  };
526
-
527
532
  return _extends.apply(this, arguments);
528
533
  }
529
534
 
@@ -697,11 +702,11 @@ const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslatio
697
702
  return vectorToTuple(props[key]);
698
703
  }), [props]);
699
704
  React.useEffect(() => {
700
- if ("length" in rigidBodyRef.current) {
701
- rigidBodyRef.current.forEach(rigidBody => {
705
+ if (Array.isArray(rigidBodyRef.current)) {
706
+ for (const rigidBody of rigidBodyRef.current) {
702
707
  setRigidBodyOptions(rigidBody, props, states, updateTranslations);
703
- });
704
- } else {
708
+ }
709
+ } else if (rigidBodyRef.current) {
705
710
  setRigidBodyOptions(rigidBodyRef.current, props, states, updateTranslations);
706
711
  }
707
712
  }, mutablePropsAsFlatArray);
@@ -724,20 +729,20 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
724
729
  onIntersectionExit
725
730
  };
726
731
  React.useEffect(() => {
727
- if ("length" in rigidBodyRef.current) {
728
- rigidBodyRef.current.forEach(rigidBody => {
732
+ if (Array.isArray(rigidBodyRef.current)) {
733
+ for (const rigidBody of rigidBodyRef.current) {
729
734
  events.set(rigidBody.handle, eventHandlers);
730
- });
731
- } else {
735
+ }
736
+ } else if (rigidBodyRef.current) {
732
737
  events.set(rigidBodyRef.current.handle, eventHandlers);
733
738
  }
734
739
 
735
740
  return () => {
736
- if ("length" in rigidBodyRef.current) {
737
- rigidBodyRef.current.forEach(rigidBody => {
741
+ if (Array.isArray(rigidBodyRef.current)) {
742
+ for (const rigidBody of rigidBodyRef.current) {
738
743
  events.delete(rigidBody.handle);
739
- });
740
- } else {
744
+ }
745
+ } else if (rigidBodyRef.current) {
741
746
  events.delete(rigidBodyRef.current.handle);
742
747
  }
743
748
  };
@@ -1078,6 +1083,7 @@ const useRigidBody = (options = {}) => {
1078
1083
  return () => {
1079
1084
  world.removeRigidBody(rigidBody);
1080
1085
  rigidBodyStates.delete(rigidBody.handle);
1086
+ rigidBodyRef.current = undefined;
1081
1087
  };
1082
1088
  }, []);
1083
1089
  useUpdateRigidBodyOptions(rigidBodyRef, mergedOptions, rigidBodyStates);
@@ -1,6 +1,6 @@
1
1
  import { EventQueue, RigidBodyDesc, ColliderDesc, ActiveEvents, ShapeType } from '@dimforge/rapier3d-compat';
2
2
  export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
3
- import React, { useState, useEffect, useRef, useMemo, createContext, useContext, memo, forwardRef, useImperativeHandle, useLayoutEffect } from 'react';
3
+ import React, { useRef, useState, useEffect, useMemo, createContext, useContext, memo, forwardRef, useImperativeHandle, useLayoutEffect } from 'react';
4
4
  import { useAsset } from 'use-asset';
5
5
  import { useFrame } from '@react-three/fiber';
6
6
  import { Quaternion, Euler, Vector3, Object3D, Matrix4, MathUtils, InstancedMesh, MeshBasicMaterial, Color, PlaneGeometry, ConeGeometry, CapsuleGeometry, CylinderGeometry, BufferGeometry, BufferAttribute, SphereGeometry, BoxGeometry, DynamicDrawUsage } from 'three';
@@ -173,10 +173,19 @@ const createWorldApi = ref => {
173
173
  getRigidBody: handle => ref.current().getRigidBody(handle),
174
174
  createRigidBody: desc => ref.current().createRigidBody(desc),
175
175
  createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
176
- removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
177
- removeCollider: (collider, wakeUp = true) => ref.current().removeCollider(collider, wakeUp),
176
+ removeRigidBody: rigidBody => {
177
+ if (!ref.current().bodies.contains(rigidBody.handle)) return;
178
+ ref.current().removeRigidBody(rigidBody);
179
+ },
180
+ removeCollider: (collider, wakeUp = true) => {
181
+ if (!ref.current().colliders.contains(collider.handle)) return;
182
+ ref.current().removeCollider(collider, wakeUp);
183
+ },
178
184
  createImpulseJoint: (params, rigidBodyA, rigidBodyB, wakeUp = true) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, wakeUp),
179
- removeImpulseJoint: (joint, wakeUp = true) => ref.current().removeImpulseJoint(joint, wakeUp),
185
+ removeImpulseJoint: (joint, wakeUp = true) => {
186
+ if (!ref.current().impulseJoints.contains(joint.handle)) return;
187
+ ref.current().removeImpulseJoint(joint, wakeUp);
188
+ },
180
189
  forEachCollider: callback => ref.current().forEachCollider(callback),
181
190
  setGravity: ({
182
191
  x,
@@ -220,10 +229,6 @@ const Physics = ({
220
229
  updatePriority
221
230
  }) => {
222
231
  const rapier = useAsset(importRapier);
223
- const [isPaused, setIsPaused] = useState(_paused);
224
- useEffect(() => {
225
- setIsPaused(_paused);
226
- }, [_paused]);
227
232
  const worldRef = useRef();
228
233
  const getWorldRef = useRef(() => {
229
234
  if (!worldRef.current) {
@@ -244,6 +249,7 @@ const Physics = ({
244
249
  return () => {
245
250
  if (world) {
246
251
  world.free();
252
+ worldRef.current = undefined;
247
253
  }
248
254
  };
249
255
  }, []); // Update gravity
@@ -477,15 +483,15 @@ const Physics = ({
477
483
  colliderStates,
478
484
  rigidBodyEvents,
479
485
  colliderEvents,
480
- isPaused
481
- }), [isPaused]);
486
+ isPaused: _paused
487
+ }), [_paused]);
482
488
  return /*#__PURE__*/React.createElement(RapierContext.Provider, {
483
489
  value: context
484
490
  }, children);
485
491
  };
486
492
 
487
493
  function _extends() {
488
- _extends = Object.assign || function (target) {
494
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
489
495
  for (var i = 1; i < arguments.length; i++) {
490
496
  var source = arguments[i];
491
497
 
@@ -498,7 +504,6 @@ function _extends() {
498
504
 
499
505
  return target;
500
506
  };
501
-
502
507
  return _extends.apply(this, arguments);
503
508
  }
504
509
 
@@ -672,11 +677,11 @@ const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslatio
672
677
  return vectorToTuple(props[key]);
673
678
  }), [props]);
674
679
  useEffect(() => {
675
- if ("length" in rigidBodyRef.current) {
676
- rigidBodyRef.current.forEach(rigidBody => {
680
+ if (Array.isArray(rigidBodyRef.current)) {
681
+ for (const rigidBody of rigidBodyRef.current) {
677
682
  setRigidBodyOptions(rigidBody, props, states, updateTranslations);
678
- });
679
- } else {
683
+ }
684
+ } else if (rigidBodyRef.current) {
680
685
  setRigidBodyOptions(rigidBodyRef.current, props, states, updateTranslations);
681
686
  }
682
687
  }, mutablePropsAsFlatArray);
@@ -699,20 +704,20 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
699
704
  onIntersectionExit
700
705
  };
701
706
  useEffect(() => {
702
- if ("length" in rigidBodyRef.current) {
703
- rigidBodyRef.current.forEach(rigidBody => {
707
+ if (Array.isArray(rigidBodyRef.current)) {
708
+ for (const rigidBody of rigidBodyRef.current) {
704
709
  events.set(rigidBody.handle, eventHandlers);
705
- });
706
- } else {
710
+ }
711
+ } else if (rigidBodyRef.current) {
707
712
  events.set(rigidBodyRef.current.handle, eventHandlers);
708
713
  }
709
714
 
710
715
  return () => {
711
- if ("length" in rigidBodyRef.current) {
712
- rigidBodyRef.current.forEach(rigidBody => {
716
+ if (Array.isArray(rigidBodyRef.current)) {
717
+ for (const rigidBody of rigidBodyRef.current) {
713
718
  events.delete(rigidBody.handle);
714
- });
715
- } else {
719
+ }
720
+ } else if (rigidBodyRef.current) {
716
721
  events.delete(rigidBodyRef.current.handle);
717
722
  }
718
723
  };
@@ -1053,6 +1058,7 @@ const useRigidBody = (options = {}) => {
1053
1058
  return () => {
1054
1059
  world.removeRigidBody(rigidBody);
1055
1060
  rigidBodyStates.delete(rigidBody.handle);
1061
+ rigidBodyRef.current = undefined;
1056
1062
  };
1057
1063
  }, []);
1058
1064
  useUpdateRigidBodyOptions(rigidBodyRef, mergedOptions, rigidBodyStates);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.7.6",
3
+ "version": "0.7.7",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
@@ -9,18 +9,18 @@
9
9
  "dist"
10
10
  ],
11
11
  "devDependencies": {
12
- "@react-three/drei": "^9.6.2",
13
- "@react-three/fiber": "^8.0.12",
12
+ "@react-three/drei": "^9.34.1",
13
+ "@react-three/fiber": "^8.8.9",
14
14
  "@react-three/test-renderer": "^8.0.17",
15
15
  "@types/react-dom": "^18.0.2",
16
16
  "@types/three": "^0.139.0",
17
17
  "@vitejs/plugin-react": "^2.1.0",
18
- "@vitest/ui": "^0.23.4",
19
- "happy-dom": "^6.0.4",
18
+ "@vitest/ui": "^0.24.1",
19
+ "happy-dom": "^7.5.5",
20
20
  "react": "^18.1.0",
21
21
  "react-dom": "^18.1.0",
22
22
  "three": "^0.139.2",
23
- "vitest": "^0.23.4"
23
+ "vitest": "^0.24.1"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "@react-three/fiber": "^8.0.12",