@react-three/rapier 0.7.0 → 0.7.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.
@@ -14,7 +14,7 @@ interface CreateRigidBodyStateOptions {
14
14
  export declare const createRigidBodyState: ({ rigidBody, object, setMatrix, getMatrix, worldScale }: CreateRigidBodyStateOptions) => RigidBodyState;
15
15
  declare type ImmutableRigidBodyOptions = (keyof RigidBodyProps)[];
16
16
  export declare const immutableRigidBodyOptions: ImmutableRigidBodyOptions;
17
- export declare const setRigidBodyOptions: (rigidBody: RigidBody, options: RigidBodyProps, states: RigidBodyStateMap) => void;
18
- export declare const useUpdateRigidBodyOptions: (rigidBodyRef: MutableRefObject<RigidBody | undefined>, props: RigidBodyProps, states: RigidBodyStateMap) => void;
19
- export declare const useRigidBodyEvents: (rigidBodyRef: MutableRefObject<RigidBody | undefined>, props: RigidBodyProps, events: EventMap) => void;
17
+ export declare const setRigidBodyOptions: (rigidBody: RigidBody, options: RigidBodyProps, states: RigidBodyStateMap, updateTranslations?: boolean) => void;
18
+ export declare const useUpdateRigidBodyOptions: (rigidBodyRef: MutableRefObject<RigidBody | RigidBody[] | undefined>, props: RigidBodyProps, states: RigidBodyStateMap, updateTranslations?: boolean) => void;
19
+ export declare const useRigidBodyEvents: (rigidBodyRef: MutableRefObject<RigidBody | RigidBody[] | undefined>, props: RigidBodyProps, events: EventMap) => void;
20
20
  export {};
@@ -636,7 +636,7 @@ const mutableRigidBodyOptions = {
636
636
  }
637
637
  };
638
638
  const mutableRigidBodyOptionKeys = Object.keys(mutableRigidBodyOptions);
639
- const setRigidBodyOptions = (rigidBody, options, states) => {
639
+ const setRigidBodyOptions = (rigidBody, options, states, updateTranslations = true) => {
640
640
  if (!rigidBody) {
641
641
  return;
642
642
  }
@@ -644,12 +644,15 @@ const setRigidBodyOptions = (rigidBody, options, states) => {
644
644
  const state = states.get(rigidBody.handle);
645
645
 
646
646
  if (state) {
647
- state.object.updateWorldMatrix(true, false);
647
+ if (updateTranslations) {
648
+ state.object.updateWorldMatrix(true, false);
649
+
650
+ _matrix4.copy(state.object.matrixWorld).decompose(_position, _rotation, _scale);
648
651
 
649
- _matrix4.copy(state.object.matrixWorld).decompose(_position, _rotation, _scale);
652
+ rigidBody.setTranslation(_position, false);
653
+ rigidBody.setRotation(_rotation, false);
654
+ }
650
655
 
651
- rigidBody.setTranslation(_position, false);
652
- rigidBody.setRotation(_rotation, false);
653
656
  mutableRigidBodyOptionKeys.forEach(key => {
654
657
  if (key in options) {
655
658
  mutableRigidBodyOptions[key](rigidBody, options[key]);
@@ -657,9 +660,15 @@ const setRigidBodyOptions = (rigidBody, options, states) => {
657
660
  });
658
661
  }
659
662
  };
660
- const useUpdateRigidBodyOptions = (rigidBodyRef, props, states) => {
663
+ const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslations = true) => {
661
664
  React.useEffect(() => {
662
- setRigidBodyOptions(rigidBodyRef.current, props, states);
665
+ if ("length" in rigidBodyRef.current) {
666
+ rigidBodyRef.current.forEach(rigidBody => {
667
+ setRigidBodyOptions(rigidBody, props, states, updateTranslations);
668
+ });
669
+ } else {
670
+ setRigidBodyOptions(rigidBodyRef.current, props, states, updateTranslations);
671
+ }
663
672
  }, [props]);
664
673
  };
665
674
  const useRigidBodyEvents = (rigidBodyRef, props, events) => {
@@ -671,17 +680,31 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
671
680
  onIntersectionEnter,
672
681
  onIntersectionExit
673
682
  } = props;
683
+ const eventHandlers = {
684
+ onWake,
685
+ onSleep,
686
+ onCollisionEnter,
687
+ onCollisionExit,
688
+ onIntersectionEnter,
689
+ onIntersectionExit
690
+ };
674
691
  React.useEffect(() => {
675
- events.set(rigidBodyRef.current.handle, {
676
- onWake,
677
- onSleep,
678
- onCollisionEnter,
679
- onCollisionExit,
680
- onIntersectionEnter,
681
- onIntersectionExit
682
- });
692
+ if ("length" in rigidBodyRef.current) {
693
+ rigidBodyRef.current.forEach(rigidBody => {
694
+ events.set(rigidBody.handle, eventHandlers);
695
+ });
696
+ } else {
697
+ events.set(rigidBodyRef.current.handle, eventHandlers);
698
+ }
699
+
683
700
  return () => {
684
- events.delete(rigidBodyRef.current.handle);
701
+ if ("length" in rigidBodyRef.current) {
702
+ rigidBodyRef.current.forEach(rigidBody => {
703
+ events.delete(rigidBody.handle);
704
+ });
705
+ } else {
706
+ events.delete(rigidBodyRef.current.handle);
707
+ }
685
708
  };
686
709
  }, [onWake, onSleep, onCollisionEnter, onCollisionExit]);
687
710
  };
@@ -1413,7 +1436,8 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1413
1436
  const {
1414
1437
  world,
1415
1438
  rigidBodyStates,
1416
- physicsOptions
1439
+ physicsOptions,
1440
+ rigidBodyEvents
1417
1441
  } = useRapier();
1418
1442
  const object = React.useRef(null);
1419
1443
 
@@ -1424,7 +1448,7 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1424
1448
  } = props,
1425
1449
  options = _objectWithoutProperties(props, _excluded);
1426
1450
 
1427
- const instancesRef = React.useRef();
1451
+ const instancesRef = React.useRef([]);
1428
1452
  const instancesRefGetter = React.useRef(() => {
1429
1453
  if (!instancesRef.current) {
1430
1454
  instancesRef.current = [];
@@ -1469,10 +1493,9 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1469
1493
 
1470
1494
  _object3d.rotation.set(rx, ry, rz);
1471
1495
 
1472
- _object3d.applyMatrix4(invertedWorld); // Set initial transforms based on world transforms
1473
- // will be replaced by the setRigidBodyOption below
1474
-
1496
+ _object3d.applyMatrix4(invertedWorld);
1475
1497
 
1498
+ mesh.setMatrixAt(index, _object3d.matrix);
1476
1499
  rigidBody.setTranslation(_object3d.position, false);
1477
1500
  rigidBody.setRotation(_object3d.quaternion, false);
1478
1501
  const api = createRigidBodyApi({
@@ -1493,11 +1516,21 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1493
1516
  world.removeRigidBody(rb.rigidBody);
1494
1517
  rigidBodyStates.delete(rb.rigidBody.handle);
1495
1518
  });
1496
- instancesRef.current = undefined;
1519
+ instancesRef.current = [];
1497
1520
  };
1498
1521
  }, []);
1499
1522
  const api = React.useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
1500
1523
  React.useImperativeHandle(ref, () => api);
1524
+ useUpdateRigidBodyOptions({
1525
+ current: instancesRef.current.map(({
1526
+ rigidBody
1527
+ }) => rigidBody)
1528
+ }, mergedOptions, rigidBodyStates, false);
1529
+ useRigidBodyEvents({
1530
+ current: instancesRef.current.map(({
1531
+ rigidBody
1532
+ }) => rigidBody)
1533
+ }, mergedOptions, rigidBodyEvents);
1501
1534
  return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
1502
1535
  value: {
1503
1536
  ref: object,
@@ -636,7 +636,7 @@ const mutableRigidBodyOptions = {
636
636
  }
637
637
  };
638
638
  const mutableRigidBodyOptionKeys = Object.keys(mutableRigidBodyOptions);
639
- const setRigidBodyOptions = (rigidBody, options, states) => {
639
+ const setRigidBodyOptions = (rigidBody, options, states, updateTranslations = true) => {
640
640
  if (!rigidBody) {
641
641
  return;
642
642
  }
@@ -644,12 +644,15 @@ const setRigidBodyOptions = (rigidBody, options, states) => {
644
644
  const state = states.get(rigidBody.handle);
645
645
 
646
646
  if (state) {
647
- state.object.updateWorldMatrix(true, false);
647
+ if (updateTranslations) {
648
+ state.object.updateWorldMatrix(true, false);
649
+
650
+ _matrix4.copy(state.object.matrixWorld).decompose(_position, _rotation, _scale);
648
651
 
649
- _matrix4.copy(state.object.matrixWorld).decompose(_position, _rotation, _scale);
652
+ rigidBody.setTranslation(_position, false);
653
+ rigidBody.setRotation(_rotation, false);
654
+ }
650
655
 
651
- rigidBody.setTranslation(_position, false);
652
- rigidBody.setRotation(_rotation, false);
653
656
  mutableRigidBodyOptionKeys.forEach(key => {
654
657
  if (key in options) {
655
658
  mutableRigidBodyOptions[key](rigidBody, options[key]);
@@ -657,9 +660,15 @@ const setRigidBodyOptions = (rigidBody, options, states) => {
657
660
  });
658
661
  }
659
662
  };
660
- const useUpdateRigidBodyOptions = (rigidBodyRef, props, states) => {
663
+ const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslations = true) => {
661
664
  React.useEffect(() => {
662
- setRigidBodyOptions(rigidBodyRef.current, props, states);
665
+ if ("length" in rigidBodyRef.current) {
666
+ rigidBodyRef.current.forEach(rigidBody => {
667
+ setRigidBodyOptions(rigidBody, props, states, updateTranslations);
668
+ });
669
+ } else {
670
+ setRigidBodyOptions(rigidBodyRef.current, props, states, updateTranslations);
671
+ }
663
672
  }, [props]);
664
673
  };
665
674
  const useRigidBodyEvents = (rigidBodyRef, props, events) => {
@@ -671,17 +680,31 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
671
680
  onIntersectionEnter,
672
681
  onIntersectionExit
673
682
  } = props;
683
+ const eventHandlers = {
684
+ onWake,
685
+ onSleep,
686
+ onCollisionEnter,
687
+ onCollisionExit,
688
+ onIntersectionEnter,
689
+ onIntersectionExit
690
+ };
674
691
  React.useEffect(() => {
675
- events.set(rigidBodyRef.current.handle, {
676
- onWake,
677
- onSleep,
678
- onCollisionEnter,
679
- onCollisionExit,
680
- onIntersectionEnter,
681
- onIntersectionExit
682
- });
692
+ if ("length" in rigidBodyRef.current) {
693
+ rigidBodyRef.current.forEach(rigidBody => {
694
+ events.set(rigidBody.handle, eventHandlers);
695
+ });
696
+ } else {
697
+ events.set(rigidBodyRef.current.handle, eventHandlers);
698
+ }
699
+
683
700
  return () => {
684
- events.delete(rigidBodyRef.current.handle);
701
+ if ("length" in rigidBodyRef.current) {
702
+ rigidBodyRef.current.forEach(rigidBody => {
703
+ events.delete(rigidBody.handle);
704
+ });
705
+ } else {
706
+ events.delete(rigidBodyRef.current.handle);
707
+ }
685
708
  };
686
709
  }, [onWake, onSleep, onCollisionEnter, onCollisionExit]);
687
710
  };
@@ -1413,7 +1436,8 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1413
1436
  const {
1414
1437
  world,
1415
1438
  rigidBodyStates,
1416
- physicsOptions
1439
+ physicsOptions,
1440
+ rigidBodyEvents
1417
1441
  } = useRapier();
1418
1442
  const object = React.useRef(null);
1419
1443
 
@@ -1424,7 +1448,7 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1424
1448
  } = props,
1425
1449
  options = _objectWithoutProperties(props, _excluded);
1426
1450
 
1427
- const instancesRef = React.useRef();
1451
+ const instancesRef = React.useRef([]);
1428
1452
  const instancesRefGetter = React.useRef(() => {
1429
1453
  if (!instancesRef.current) {
1430
1454
  instancesRef.current = [];
@@ -1469,10 +1493,9 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1469
1493
 
1470
1494
  _object3d.rotation.set(rx, ry, rz);
1471
1495
 
1472
- _object3d.applyMatrix4(invertedWorld); // Set initial transforms based on world transforms
1473
- // will be replaced by the setRigidBodyOption below
1474
-
1496
+ _object3d.applyMatrix4(invertedWorld);
1475
1497
 
1498
+ mesh.setMatrixAt(index, _object3d.matrix);
1476
1499
  rigidBody.setTranslation(_object3d.position, false);
1477
1500
  rigidBody.setRotation(_object3d.quaternion, false);
1478
1501
  const api = createRigidBodyApi({
@@ -1493,11 +1516,21 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1493
1516
  world.removeRigidBody(rb.rigidBody);
1494
1517
  rigidBodyStates.delete(rb.rigidBody.handle);
1495
1518
  });
1496
- instancesRef.current = undefined;
1519
+ instancesRef.current = [];
1497
1520
  };
1498
1521
  }, []);
1499
1522
  const api = React.useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
1500
1523
  React.useImperativeHandle(ref, () => api);
1524
+ useUpdateRigidBodyOptions({
1525
+ current: instancesRef.current.map(({
1526
+ rigidBody
1527
+ }) => rigidBody)
1528
+ }, mergedOptions, rigidBodyStates, false);
1529
+ useRigidBodyEvents({
1530
+ current: instancesRef.current.map(({
1531
+ rigidBody
1532
+ }) => rigidBody)
1533
+ }, mergedOptions, rigidBodyEvents);
1501
1534
  return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
1502
1535
  value: {
1503
1536
  ref: object,
@@ -611,7 +611,7 @@ const mutableRigidBodyOptions = {
611
611
  }
612
612
  };
613
613
  const mutableRigidBodyOptionKeys = Object.keys(mutableRigidBodyOptions);
614
- const setRigidBodyOptions = (rigidBody, options, states) => {
614
+ const setRigidBodyOptions = (rigidBody, options, states, updateTranslations = true) => {
615
615
  if (!rigidBody) {
616
616
  return;
617
617
  }
@@ -619,12 +619,15 @@ const setRigidBodyOptions = (rigidBody, options, states) => {
619
619
  const state = states.get(rigidBody.handle);
620
620
 
621
621
  if (state) {
622
- state.object.updateWorldMatrix(true, false);
622
+ if (updateTranslations) {
623
+ state.object.updateWorldMatrix(true, false);
624
+
625
+ _matrix4.copy(state.object.matrixWorld).decompose(_position, _rotation, _scale);
623
626
 
624
- _matrix4.copy(state.object.matrixWorld).decompose(_position, _rotation, _scale);
627
+ rigidBody.setTranslation(_position, false);
628
+ rigidBody.setRotation(_rotation, false);
629
+ }
625
630
 
626
- rigidBody.setTranslation(_position, false);
627
- rigidBody.setRotation(_rotation, false);
628
631
  mutableRigidBodyOptionKeys.forEach(key => {
629
632
  if (key in options) {
630
633
  mutableRigidBodyOptions[key](rigidBody, options[key]);
@@ -632,9 +635,15 @@ const setRigidBodyOptions = (rigidBody, options, states) => {
632
635
  });
633
636
  }
634
637
  };
635
- const useUpdateRigidBodyOptions = (rigidBodyRef, props, states) => {
638
+ const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslations = true) => {
636
639
  useEffect(() => {
637
- setRigidBodyOptions(rigidBodyRef.current, props, states);
640
+ if ("length" in rigidBodyRef.current) {
641
+ rigidBodyRef.current.forEach(rigidBody => {
642
+ setRigidBodyOptions(rigidBody, props, states, updateTranslations);
643
+ });
644
+ } else {
645
+ setRigidBodyOptions(rigidBodyRef.current, props, states, updateTranslations);
646
+ }
638
647
  }, [props]);
639
648
  };
640
649
  const useRigidBodyEvents = (rigidBodyRef, props, events) => {
@@ -646,17 +655,31 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
646
655
  onIntersectionEnter,
647
656
  onIntersectionExit
648
657
  } = props;
658
+ const eventHandlers = {
659
+ onWake,
660
+ onSleep,
661
+ onCollisionEnter,
662
+ onCollisionExit,
663
+ onIntersectionEnter,
664
+ onIntersectionExit
665
+ };
649
666
  useEffect(() => {
650
- events.set(rigidBodyRef.current.handle, {
651
- onWake,
652
- onSleep,
653
- onCollisionEnter,
654
- onCollisionExit,
655
- onIntersectionEnter,
656
- onIntersectionExit
657
- });
667
+ if ("length" in rigidBodyRef.current) {
668
+ rigidBodyRef.current.forEach(rigidBody => {
669
+ events.set(rigidBody.handle, eventHandlers);
670
+ });
671
+ } else {
672
+ events.set(rigidBodyRef.current.handle, eventHandlers);
673
+ }
674
+
658
675
  return () => {
659
- events.delete(rigidBodyRef.current.handle);
676
+ if ("length" in rigidBodyRef.current) {
677
+ rigidBodyRef.current.forEach(rigidBody => {
678
+ events.delete(rigidBody.handle);
679
+ });
680
+ } else {
681
+ events.delete(rigidBodyRef.current.handle);
682
+ }
660
683
  };
661
684
  }, [onWake, onSleep, onCollisionEnter, onCollisionExit]);
662
685
  };
@@ -1388,7 +1411,8 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1388
1411
  const {
1389
1412
  world,
1390
1413
  rigidBodyStates,
1391
- physicsOptions
1414
+ physicsOptions,
1415
+ rigidBodyEvents
1392
1416
  } = useRapier();
1393
1417
  const object = useRef(null);
1394
1418
 
@@ -1399,7 +1423,7 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1399
1423
  } = props,
1400
1424
  options = _objectWithoutProperties(props, _excluded);
1401
1425
 
1402
- const instancesRef = useRef();
1426
+ const instancesRef = useRef([]);
1403
1427
  const instancesRefGetter = useRef(() => {
1404
1428
  if (!instancesRef.current) {
1405
1429
  instancesRef.current = [];
@@ -1444,10 +1468,9 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1444
1468
 
1445
1469
  _object3d.rotation.set(rx, ry, rz);
1446
1470
 
1447
- _object3d.applyMatrix4(invertedWorld); // Set initial transforms based on world transforms
1448
- // will be replaced by the setRigidBodyOption below
1449
-
1471
+ _object3d.applyMatrix4(invertedWorld);
1450
1472
 
1473
+ mesh.setMatrixAt(index, _object3d.matrix);
1451
1474
  rigidBody.setTranslation(_object3d.position, false);
1452
1475
  rigidBody.setRotation(_object3d.quaternion, false);
1453
1476
  const api = createRigidBodyApi({
@@ -1468,11 +1491,21 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1468
1491
  world.removeRigidBody(rb.rigidBody);
1469
1492
  rigidBodyStates.delete(rb.rigidBody.handle);
1470
1493
  });
1471
- instancesRef.current = undefined;
1494
+ instancesRef.current = [];
1472
1495
  };
1473
1496
  }, []);
1474
1497
  const api = useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
1475
1498
  useImperativeHandle(ref, () => api);
1499
+ useUpdateRigidBodyOptions({
1500
+ current: instancesRef.current.map(({
1501
+ rigidBody
1502
+ }) => rigidBody)
1503
+ }, mergedOptions, rigidBodyStates, false);
1504
+ useRigidBodyEvents({
1505
+ current: instancesRef.current.map(({
1506
+ rigidBody
1507
+ }) => rigidBody)
1508
+ }, mergedOptions, rigidBodyEvents);
1476
1509
  return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
1477
1510
  value: {
1478
1511
  ref: object,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.7.0",
3
+ "version": "0.7.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",