@react-three/rapier 0.15.0 → 0.16.0-canary.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/dist/declarations/src/components/AnyCollider.d.ts +28 -9
- package/dist/declarations/src/components/Physics.d.ts +2 -2
- package/dist/declarations/src/hooks/use-imperative-instance.d.ts +2 -1
- package/dist/declarations/src/types.d.ts +33 -21
- package/dist/declarations/src/utils/api.d.ts +1 -2
- package/dist/declarations/src/utils/utils-collider.d.ts +5 -5
- package/dist/declarations/src/utils/utils-rigidbody.d.ts +4 -0
- package/dist/declarations/src/utils/utils.d.ts +2 -2
- package/dist/react-three-rapier.cjs.dev.js +162 -164
- package/dist/react-three-rapier.cjs.prod.js +162 -164
- package/dist/react-three-rapier.esm.js +162 -166
- package/package.json +4 -4
- package/readme.md +29 -22
@@ -1,7 +1,7 @@
|
|
1
1
|
import { ActiveEvents, ColliderDesc, EventQueue, RigidBodyDesc } from '@dimforge/rapier3d-compat';
|
2
2
|
export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
|
3
3
|
import { useFrame, useThree } from '@react-three/fiber';
|
4
|
-
import React, { useRef, useEffect, memo, useMemo, useContext, useState,
|
4
|
+
import React, { useRef, useEffect, memo, useMemo, useContext, useState, useCallback, createContext, forwardRef, useImperativeHandle, Fragment } from 'react';
|
5
5
|
import { Quaternion, Euler, Vector3, Object3D, Matrix4, BufferAttribute, MathUtils, DynamicDrawUsage } from 'three';
|
6
6
|
import { useAsset } from 'use-asset';
|
7
7
|
import { mergeVertices } from 'three-stdlib';
|
@@ -47,37 +47,37 @@ function _objectSpread2(target) {
|
|
47
47
|
return target;
|
48
48
|
}
|
49
49
|
|
50
|
-
const createWorldApi =
|
50
|
+
const createWorldApi = getWorld => {
|
51
51
|
return {
|
52
|
-
raw: () =>
|
53
|
-
getCollider: handle =>
|
54
|
-
getRigidBody: handle =>
|
55
|
-
createRigidBody: desc =>
|
56
|
-
createCollider: (desc, rigidBody) =>
|
52
|
+
raw: () => getWorld(),
|
53
|
+
getCollider: handle => getWorld().getCollider(handle),
|
54
|
+
getRigidBody: handle => getWorld().getRigidBody(handle),
|
55
|
+
createRigidBody: desc => getWorld().createRigidBody(desc),
|
56
|
+
createCollider: (desc, rigidBody) => getWorld().createCollider(desc, rigidBody),
|
57
57
|
removeRigidBody: rigidBody => {
|
58
|
-
if (!
|
59
|
-
|
58
|
+
if (!getWorld().bodies.contains(rigidBody.handle)) return;
|
59
|
+
getWorld().removeRigidBody(rigidBody);
|
60
60
|
},
|
61
61
|
removeCollider: (collider, wakeUp = true) => {
|
62
|
-
if (!
|
63
|
-
|
62
|
+
if (!getWorld().colliders.contains(collider.handle)) return;
|
63
|
+
getWorld().removeCollider(collider, wakeUp);
|
64
64
|
},
|
65
|
-
createImpulseJoint: (params, rigidBodyA, rigidBodyB, wakeUp = true) =>
|
65
|
+
createImpulseJoint: (params, rigidBodyA, rigidBodyB, wakeUp = true) => getWorld().createImpulseJoint(params, rigidBodyA, rigidBodyB, wakeUp),
|
66
66
|
removeImpulseJoint: (joint, wakeUp = true) => {
|
67
|
-
if (!
|
68
|
-
|
67
|
+
if (!getWorld().impulseJoints.contains(joint.handle)) return;
|
68
|
+
getWorld().removeImpulseJoint(joint, wakeUp);
|
69
69
|
},
|
70
|
-
forEachCollider: callback =>
|
70
|
+
forEachCollider: callback => getWorld().forEachCollider(callback),
|
71
71
|
setGravity: ({
|
72
72
|
x,
|
73
73
|
y,
|
74
74
|
z
|
75
|
-
}) =>
|
75
|
+
}) => getWorld().gravity = {
|
76
76
|
x,
|
77
77
|
y,
|
78
78
|
z
|
79
79
|
},
|
80
|
-
debugRender: () =>
|
80
|
+
debugRender: () => getWorld().debugRender()
|
81
81
|
};
|
82
82
|
};
|
83
83
|
|
@@ -270,6 +270,7 @@ const createColliderFromOptions = (options, world, scale, getRigidBody) => {
|
|
270
270
|
const desc = ColliderDesc[options.shape](...scaledArgs);
|
271
271
|
return world.createCollider(desc, getRigidBody === null || getRigidBody === void 0 ? void 0 : getRigidBody());
|
272
272
|
};
|
273
|
+
const immutableColliderOptions = ["shape", "args"];
|
273
274
|
const massPropertiesConflictError = "Please pick ONLY ONE of the `density`, `mass` and `massProperties` options.";
|
274
275
|
|
275
276
|
const setColliderMassOptions = (collider, options) => {
|
@@ -318,7 +319,7 @@ const mutableColliderOptions = {
|
|
318
319
|
restitutionCombineRule: (collider, value) => {
|
319
320
|
collider.setRestitutionCombineRule(value);
|
320
321
|
},
|
321
|
-
// To make sure the options all
|
322
|
+
// To make sure the options all mutable options are listed
|
322
323
|
quaternion: () => {},
|
323
324
|
position: () => {},
|
324
325
|
rotation: () => {},
|
@@ -380,7 +381,7 @@ const useUpdateColliderOptions = (getCollider, props, states) => {
|
|
380
381
|
useEffect(() => {
|
381
382
|
const collider = getCollider();
|
382
383
|
setColliderOptions(collider, props, states);
|
383
|
-
}, mutablePropsAsFlatArray);
|
384
|
+
}, [...mutablePropsAsFlatArray, getCollider]);
|
384
385
|
};
|
385
386
|
|
386
387
|
const isChildOfMeshCollider = child => {
|
@@ -657,6 +658,33 @@ const Debug = /*#__PURE__*/memo(() => {
|
|
657
658
|
}), /*#__PURE__*/React.createElement("bufferGeometry", null)));
|
658
659
|
});
|
659
660
|
|
661
|
+
/**
|
662
|
+
* Initiate an instance and return a safe getter
|
663
|
+
*/
|
664
|
+
|
665
|
+
const useImperativeInstance = (createFn, destroyFn, dependencyList) => {
|
666
|
+
const ref = useRef();
|
667
|
+
const getInstance = useCallback(() => {
|
668
|
+
if (!ref.current) {
|
669
|
+
ref.current = createFn();
|
670
|
+
}
|
671
|
+
|
672
|
+
return ref.current;
|
673
|
+
}, dependencyList);
|
674
|
+
useEffect(() => {
|
675
|
+
// Save the destroy function and instance
|
676
|
+
const instance = getInstance();
|
677
|
+
|
678
|
+
const destroy = () => destroyFn(instance);
|
679
|
+
|
680
|
+
return () => {
|
681
|
+
destroy();
|
682
|
+
ref.current = undefined;
|
683
|
+
};
|
684
|
+
}, [getInstance]);
|
685
|
+
return getInstance;
|
686
|
+
};
|
687
|
+
|
660
688
|
const rapierContext = /*#__PURE__*/createContext(undefined);
|
661
689
|
|
662
690
|
const getCollisionPayloadFromSource = (target, other) => {
|
@@ -706,77 +734,60 @@ const Physics = ({
|
|
706
734
|
const rapier = useAsset(importRapier);
|
707
735
|
const {
|
708
736
|
invalidate
|
709
|
-
} = useThree();
|
710
|
-
const worldRef = useRef();
|
711
|
-
const getWorldRef = useRef(() => {
|
712
|
-
if (!worldRef.current) {
|
713
|
-
const world = new rapier.World(vectorArrayToVector3(_gravity));
|
714
|
-
worldRef.current = world;
|
715
|
-
}
|
737
|
+
} = useThree(); // Init World
|
716
738
|
|
717
|
-
|
718
|
-
|
739
|
+
const getWorld = useImperativeInstance(() => {
|
740
|
+
return new rapier.World(vectorArrayToVector3(_gravity));
|
741
|
+
}, world => {
|
742
|
+
world.free();
|
743
|
+
}, []);
|
719
744
|
const rigidBodyStates = useConst(() => new Map());
|
720
745
|
const colliderStates = useConst(() => new Map());
|
721
746
|
const rigidBodyEvents = useConst(() => new Map());
|
722
747
|
const colliderEvents = useConst(() => new Map());
|
723
748
|
const eventQueue = useConst(() => new EventQueue(false));
|
724
749
|
const beforeStepCallbacks = useConst(() => new Set());
|
725
|
-
const afterStepCallbacks = useConst(() => new Set()); //
|
750
|
+
const afterStepCallbacks = useConst(() => new Set()); // Update gravity
|
726
751
|
|
727
752
|
useEffect(() => {
|
728
|
-
const world =
|
729
|
-
return () => {
|
730
|
-
if (world) {
|
731
|
-
world.free();
|
732
|
-
worldRef.current = undefined;
|
733
|
-
}
|
734
|
-
};
|
735
|
-
}, []); // Update gravity
|
736
|
-
|
737
|
-
useEffect(() => {
|
738
|
-
const world = worldRef.current;
|
753
|
+
const world = getWorld();
|
739
754
|
|
740
755
|
if (world) {
|
741
756
|
world.gravity = vectorArrayToVector3(_gravity);
|
742
757
|
}
|
743
758
|
}, [_gravity]);
|
744
|
-
const api = useMemo(() => createWorldApi(
|
759
|
+
const api = useMemo(() => createWorldApi(getWorld), []);
|
745
760
|
const getSourceFromColliderHandle = useCallback(handle => {
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
};
|
770
|
-
return source;
|
771
|
-
}
|
761
|
+
var _collider$parent;
|
762
|
+
|
763
|
+
const world = getWorld();
|
764
|
+
const collider = world.getCollider(handle);
|
765
|
+
const colEvents = colliderEvents.get(handle);
|
766
|
+
const colliderState = colliderStates.get(handle);
|
767
|
+
const rigidBodyHandle = collider === null || collider === void 0 ? void 0 : (_collider$parent = collider.parent()) === null || _collider$parent === void 0 ? void 0 : _collider$parent.handle;
|
768
|
+
const rigidBody = rigidBodyHandle !== undefined ? world.getRigidBody(rigidBodyHandle) : undefined;
|
769
|
+
const rbEvents = rigidBody && rigidBodyHandle !== undefined ? rigidBodyEvents.get(rigidBodyHandle) : undefined;
|
770
|
+
const rigidBodyState = rigidBodyHandle !== undefined ? rigidBodyStates.get(rigidBodyHandle) : undefined;
|
771
|
+
const source = {
|
772
|
+
collider: {
|
773
|
+
object: collider,
|
774
|
+
events: colEvents,
|
775
|
+
state: colliderState
|
776
|
+
},
|
777
|
+
rigidBody: {
|
778
|
+
object: rigidBody,
|
779
|
+
events: rbEvents,
|
780
|
+
state: rigidBodyState
|
781
|
+
}
|
782
|
+
};
|
783
|
+
return source;
|
772
784
|
}, []);
|
773
785
|
const [steppingState] = useState({
|
774
786
|
previousState: {},
|
775
787
|
accumulator: 0
|
776
788
|
});
|
777
789
|
const step = useCallback(dt => {
|
778
|
-
const world =
|
779
|
-
if (!world) return;
|
790
|
+
const world = getWorld();
|
780
791
|
/* Check if the timestep is supposed to be variable. We'll do this here
|
781
792
|
once so we don't have to string-check every frame. */
|
782
793
|
|
@@ -1032,29 +1043,6 @@ function _extends() {
|
|
1032
1043
|
return _extends.apply(this, arguments);
|
1033
1044
|
}
|
1034
1045
|
|
1035
|
-
/**
|
1036
|
-
* Initiate an instance and return a safe getter
|
1037
|
-
*/
|
1038
|
-
|
1039
|
-
const useImperativeInstance = (createFn, destroyFn) => {
|
1040
|
-
const ref = useRef();
|
1041
|
-
const refGetter = useMemo(() => () => {
|
1042
|
-
if (!ref.current) {
|
1043
|
-
ref.current = createFn();
|
1044
|
-
}
|
1045
|
-
|
1046
|
-
return ref.current;
|
1047
|
-
}, []);
|
1048
|
-
useEffect(() => {
|
1049
|
-
const instance = refGetter();
|
1050
|
-
return () => {
|
1051
|
-
destroyFn(instance);
|
1052
|
-
ref.current = undefined;
|
1053
|
-
};
|
1054
|
-
}, []);
|
1055
|
-
return refGetter;
|
1056
|
-
};
|
1057
|
-
|
1058
1046
|
/**
|
1059
1047
|
* Takes an object resembling a Vector3 and returs a Three.Vector3
|
1060
1048
|
* @category Math helpers
|
@@ -1106,8 +1094,6 @@ const euler = ({
|
|
1106
1094
|
return new Euler(x, y, z);
|
1107
1095
|
};
|
1108
1096
|
|
1109
|
-
// Colliders
|
1110
|
-
|
1111
1097
|
/**
|
1112
1098
|
* A collider is a shape that can be attached to a rigid body to define its physical properties.
|
1113
1099
|
* @internal
|
@@ -1127,22 +1113,24 @@ const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, forwarded
|
|
1127
1113
|
colliderStates
|
1128
1114
|
} = useRapier();
|
1129
1115
|
const rigidBodyContext = useRigidBodyContext();
|
1130
|
-
const ref = useRef(null);
|
1116
|
+
const ref = useRef(null); // We spread the props out here to make sure that the ref is updated when the props change.
|
1117
|
+
|
1118
|
+
const immutablePropArray = immutableColliderOptions.flatMap(key => Array.isArray(props[key]) ? [...props[key]] : props[key]);
|
1131
1119
|
const getInstance = useImperativeInstance(() => {
|
1132
1120
|
const worldScale = ref.current.getWorldScale(vec3());
|
1133
1121
|
const collider = createColliderFromOptions(props, world, worldScale, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.getRigidBody);
|
1134
1122
|
return collider;
|
1135
1123
|
}, collider => {
|
1136
1124
|
world.removeCollider(collider);
|
1137
|
-
});
|
1125
|
+
}, [...immutablePropArray, rigidBodyContext]);
|
1138
1126
|
useEffect(() => {
|
1139
1127
|
const collider = getInstance();
|
1140
1128
|
colliderStates.set(collider.handle, createColliderState(collider, ref.current, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.ref.current));
|
1141
1129
|
return () => {
|
1142
1130
|
colliderStates.delete(collider.handle);
|
1143
1131
|
};
|
1144
|
-
}, []);
|
1145
|
-
useImperativeHandle(forwardedRef, () => getInstance());
|
1132
|
+
}, [getInstance]);
|
1133
|
+
useImperativeHandle(forwardedRef, () => getInstance(), [getInstance]);
|
1146
1134
|
const mergedProps = useMemo(() => {
|
1147
1135
|
return _objectSpread2(_objectSpread2({}, cleanRigidBodyPropsForCollider(rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options)), props);
|
1148
1136
|
}, [props, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options]);
|
@@ -1168,102 +1156,106 @@ const CuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
|
1168
1156
|
ref: ref
|
1169
1157
|
}));
|
1170
1158
|
});
|
1159
|
+
CuboidCollider.displayName = "CuboidCollider";
|
1160
|
+
|
1171
1161
|
/**
|
1172
1162
|
* A round cuboid collider shape
|
1173
1163
|
* @category Colliders
|
1174
1164
|
*/
|
1165
|
+
const RoundCuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1166
|
+
shape: "roundCuboid",
|
1167
|
+
ref: ref
|
1168
|
+
})));
|
1169
|
+
RoundCuboidCollider.displayName = "RoundCuboidCollider";
|
1175
1170
|
|
1176
|
-
const RoundCuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1177
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1178
|
-
shape: "roundCuboid",
|
1179
|
-
ref: ref
|
1180
|
-
}));
|
1181
|
-
});
|
1182
1171
|
/**
|
1183
1172
|
* A ball collider shape
|
1184
1173
|
* @category Colliders
|
1185
1174
|
*/
|
1175
|
+
const BallCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1176
|
+
shape: "ball",
|
1177
|
+
ref: ref
|
1178
|
+
})));
|
1179
|
+
BallCollider.displayName = "BallCollider";
|
1186
1180
|
|
1187
|
-
const BallCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1188
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1189
|
-
shape: "ball",
|
1190
|
-
ref: ref
|
1191
|
-
}));
|
1192
|
-
});
|
1193
1181
|
/**
|
1194
1182
|
* A capsule collider shape
|
1195
1183
|
* @category Colliders
|
1196
1184
|
*/
|
1185
|
+
const CapsuleCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1186
|
+
shape: "capsule",
|
1187
|
+
ref: ref
|
1188
|
+
})));
|
1189
|
+
CapsuleCollider.displayName = "CapsuleCollider";
|
1197
1190
|
|
1198
|
-
const CapsuleCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1199
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1200
|
-
shape: "capsule",
|
1201
|
-
ref: ref
|
1202
|
-
}));
|
1203
|
-
});
|
1204
1191
|
/**
|
1205
1192
|
* A heightfield collider shape
|
1206
1193
|
* @category Colliders
|
1207
1194
|
*/
|
1195
|
+
const HeightfieldCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1196
|
+
shape: "heightfield",
|
1197
|
+
ref: ref
|
1198
|
+
})));
|
1199
|
+
HeightfieldCollider.displayName = "HeightfieldCollider";
|
1208
1200
|
|
1209
|
-
const HeightfieldCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1210
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1211
|
-
shape: "heightfield",
|
1212
|
-
ref: ref
|
1213
|
-
}));
|
1214
|
-
});
|
1215
1201
|
/**
|
1216
1202
|
* A trimesh collider shape
|
1217
1203
|
* @category Colliders
|
1218
1204
|
*/
|
1205
|
+
const TrimeshCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1206
|
+
shape: "trimesh",
|
1207
|
+
ref: ref
|
1208
|
+
})));
|
1209
|
+
TrimeshCollider.displayName = "TrimeshCollider";
|
1219
1210
|
|
1220
|
-
const TrimeshCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1221
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1222
|
-
shape: "trimesh",
|
1223
|
-
ref: ref
|
1224
|
-
}));
|
1225
|
-
});
|
1226
1211
|
/**
|
1227
1212
|
* A cone collider shape
|
1228
1213
|
* @category Colliders
|
1229
1214
|
*/
|
1215
|
+
const ConeCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1216
|
+
shape: "cone",
|
1217
|
+
ref: ref
|
1218
|
+
})));
|
1219
|
+
ConeCollider.displayName = "ConeCollider";
|
1220
|
+
|
1221
|
+
/**
|
1222
|
+
* A round cylinder collider shape
|
1223
|
+
* @category Colliders
|
1224
|
+
*/
|
1225
|
+
const RoundConeCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1226
|
+
shape: "roundCone",
|
1227
|
+
ref: ref
|
1228
|
+
})));
|
1229
|
+
RoundConeCollider.displayName = "RoundConeCollider";
|
1230
1230
|
|
1231
|
-
const ConeCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1232
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1233
|
-
shape: "cone",
|
1234
|
-
ref: ref
|
1235
|
-
}));
|
1236
|
-
});
|
1237
1231
|
/**
|
1238
1232
|
* A cylinder collider shape
|
1239
1233
|
* @category Colliders
|
1240
1234
|
*/
|
1235
|
+
const CylinderCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1236
|
+
shape: "cylinder",
|
1237
|
+
ref: ref
|
1238
|
+
})));
|
1239
|
+
CylinderCollider.displayName = "CylinderCollider";
|
1241
1240
|
|
1242
|
-
const CylinderCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1243
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1244
|
-
shape: "cylinder",
|
1245
|
-
ref: ref
|
1246
|
-
}));
|
1247
|
-
});
|
1248
1241
|
/**
|
1249
|
-
* A
|
1242
|
+
* A round cylinder collider shape
|
1250
1243
|
* @category Colliders
|
1251
1244
|
*/
|
1245
|
+
const RoundCylinderCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1246
|
+
shape: "roundCylinder",
|
1247
|
+
ref: ref
|
1248
|
+
})));
|
1249
|
+
CylinderCollider.displayName = "RoundCylinderCollider";
|
1252
1250
|
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
BallCollider.displayName = "BallCollider";
|
1262
|
-
CapsuleCollider.displayName = "CapsuleCollider";
|
1263
|
-
HeightfieldCollider.displayName = "HeightfieldCollider";
|
1264
|
-
TrimeshCollider.displayName = "TrimeshCollider";
|
1265
|
-
ConeCollider.displayName = "ConeCollider";
|
1266
|
-
CylinderCollider.displayName = "CylinderCollider";
|
1251
|
+
/**
|
1252
|
+
* A convex hull collider shape
|
1253
|
+
* @category Colliders
|
1254
|
+
*/
|
1255
|
+
const ConvexHullCollider = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1256
|
+
shape: "convexHull",
|
1257
|
+
ref: ref
|
1258
|
+
})));
|
1267
1259
|
ConvexHullCollider.displayName = "ConvexHullCollider";
|
1268
1260
|
|
1269
1261
|
const rigidBodyDescFromOptions = options => {
|
@@ -1298,6 +1290,7 @@ const createRigidBodyState = ({
|
|
1298
1290
|
meshType: _meshType
|
1299
1291
|
};
|
1300
1292
|
};
|
1293
|
+
const immutableRigidBodyOptions = ["args", "colliders", "canSleep"];
|
1301
1294
|
const mutableRigidBodyOptions = {
|
1302
1295
|
gravityScale: (rb, value) => {
|
1303
1296
|
rb.setGravityScale(value, true);
|
@@ -1448,18 +1441,21 @@ const RigidBody = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, forwardedRe
|
|
1448
1441
|
children: undefined
|
1449
1442
|
});
|
1450
1443
|
}, [physicsOptions, props]);
|
1444
|
+
const immutablePropArray = immutableRigidBodyOptions.flatMap(key => {
|
1445
|
+
return Array.isArray(mergedOptions[key]) ? [...mergedOptions[key]] : mergedOptions[key];
|
1446
|
+
});
|
1451
1447
|
const childColliderProps = useChildColliderProps(ref, mergedOptions); // Provide a way to eagerly create rigidbody
|
1452
1448
|
|
1453
|
-
const
|
1449
|
+
const getRigidBody = useImperativeInstance(() => {
|
1454
1450
|
const desc = rigidBodyDescFromOptions(mergedOptions);
|
1455
1451
|
const rigidBody = world.createRigidBody(desc);
|
1456
1452
|
return rigidBody;
|
1457
1453
|
}, rigidBody => {
|
1458
1454
|
world.removeRigidBody(rigidBody);
|
1459
|
-
}); // Only provide a object state after the ref has been set
|
1455
|
+
}, immutablePropArray); // Only provide a object state after the ref has been set
|
1460
1456
|
|
1461
1457
|
useEffect(() => {
|
1462
|
-
const rigidBody =
|
1458
|
+
const rigidBody = getRigidBody();
|
1463
1459
|
const state = createRigidBodyState({
|
1464
1460
|
rigidBody,
|
1465
1461
|
object: ref.current
|
@@ -1468,17 +1464,17 @@ const RigidBody = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, forwardedRe
|
|
1468
1464
|
return () => {
|
1469
1465
|
rigidBodyStates.delete(rigidBody.handle);
|
1470
1466
|
};
|
1471
|
-
}, []);
|
1472
|
-
useUpdateRigidBodyOptions(
|
1473
|
-
useRigidBodyEvents(
|
1474
|
-
useImperativeHandle(forwardedRef, () =>
|
1467
|
+
}, [getRigidBody]);
|
1468
|
+
useUpdateRigidBodyOptions(getRigidBody, mergedOptions, rigidBodyStates);
|
1469
|
+
useRigidBodyEvents(getRigidBody, mergedOptions, rigidBodyEvents);
|
1470
|
+
useImperativeHandle(forwardedRef, () => getRigidBody(), [getRigidBody]);
|
1475
1471
|
const contextValue = useMemo(() => {
|
1476
1472
|
return {
|
1477
1473
|
ref,
|
1478
|
-
getRigidBody:
|
1474
|
+
getRigidBody: getRigidBody,
|
1479
1475
|
options: mergedOptions
|
1480
1476
|
};
|
1481
|
-
}, [
|
1477
|
+
}, [getRigidBody]);
|
1482
1478
|
return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
|
1483
1479
|
value: contextValue
|
1484
1480
|
}, /*#__PURE__*/React.createElement("object3D", _extends({
|
@@ -1632,7 +1628,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
1632
1628
|
jointRef.current = undefined;
|
1633
1629
|
world.removeImpulseJoint(joint);
|
1634
1630
|
}
|
1635
|
-
});
|
1631
|
+
}, []);
|
1636
1632
|
return jointRef;
|
1637
1633
|
};
|
1638
1634
|
/**
|
@@ -1743,4 +1739,4 @@ const interactionGroups = (memberships, filters) => (bitmask(memberships) << 16)
|
|
1743
1739
|
|
1744
1740
|
const bitmask = groups => [groups].flat().reduce((acc, layer) => acc | 1 << layer, 0);
|
1745
1741
|
|
1746
|
-
export { AnyCollider, BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, HeightfieldCollider, InstancedRigidBodies, MeshCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider, euler, interactionGroups, quat, useAfterPhysicsStep, useBeforePhysicsStep, useFixedJoint, useImpulseJoint, usePrismaticJoint, useRapier, useRevoluteJoint, useSphericalJoint, vec3 };
|
1742
|
+
export { AnyCollider, BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, HeightfieldCollider, InstancedRigidBodies, MeshCollider, Physics, RigidBody, RoundConeCollider, RoundCuboidCollider, RoundCylinderCollider, TrimeshCollider, euler, interactionGroups, quat, useAfterPhysicsStep, useBeforePhysicsStep, useFixedJoint, useImpulseJoint, usePrismaticJoint, useRapier, useRevoluteJoint, useSphericalJoint, vec3 };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@react-three/rapier",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.16.0-canary.0",
|
4
4
|
"source": "src/index.ts",
|
5
5
|
"main": "dist/react-three-rapier.cjs.js",
|
6
6
|
"module": "dist/react-three-rapier.esm.js",
|
@@ -26,9 +26,9 @@
|
|
26
26
|
"vitest": "0.29.7"
|
27
27
|
},
|
28
28
|
"peerDependencies": {
|
29
|
-
"@react-three/fiber": "8.9.
|
30
|
-
"react": "18.
|
31
|
-
"three": ">=0.139.
|
29
|
+
"@react-three/fiber": ">=8.9.0",
|
30
|
+
"react": ">=18.0.0",
|
31
|
+
"three": ">=0.139.0"
|
32
32
|
},
|
33
33
|
"dependencies": {
|
34
34
|
"@dimforge/rapier3d-compat": "0.11.2",
|
package/readme.md
CHANGED
@@ -27,7 +27,7 @@ The goal of this library to is to provide a fast physics engine with minimal fri
|
|
27
27
|
```tsx
|
28
28
|
import { Box, Torus } from "@react-three/drei";
|
29
29
|
import { Canvas } from "@react-three/fiber";
|
30
|
-
import { Physics, RigidBody, Debug } from "@react-three/rapier";
|
30
|
+
import { Physics, RigidBody, Debug, CuboidCollider } from "@react-three/rapier";
|
31
31
|
|
32
32
|
const App = () => {
|
33
33
|
return (
|
@@ -254,7 +254,7 @@ const Scene = () => {
|
|
254
254
|
|
255
255
|
// You can access individual instanced by their index
|
256
256
|
rigidBodies.current[40].applyImpulse({ x: 0, y: 10, z: 0 }, true);
|
257
|
-
rigidBodies.at(100).applyImpulse({ x: 0, y: 10, z: 0 }, true);
|
257
|
+
rigidBodies.current.at(100).applyImpulse({ x: 0, y: 10, z: 0 }, true);
|
258
258
|
|
259
259
|
// Or update all instances
|
260
260
|
rigidBodies.current.forEach((api) => {
|
@@ -281,7 +281,7 @@ const Scene = () => {
|
|
281
281
|
|
282
282
|
return (
|
283
283
|
<InstancedRigidBodies
|
284
|
-
ref={
|
284
|
+
ref={rigidBodies}
|
285
285
|
instances={instances}
|
286
286
|
colliders="ball"
|
287
287
|
>
|
@@ -314,7 +314,6 @@ const Scene = () => {
|
|
314
314
|
|
315
315
|
return (
|
316
316
|
<InstancedRigidBodies
|
317
|
-
ref={instancedApi}
|
318
317
|
instances={instances}
|
319
318
|
colliders="ball"
|
320
319
|
colliderNodes={[
|
@@ -654,10 +653,14 @@ const JointedThing = () => {
|
|
654
653
|
bodyA,
|
655
654
|
bodyB,
|
656
655
|
[
|
657
|
-
|
658
|
-
[0, 0, 0
|
659
|
-
|
660
|
-
[0, 0, 0, 1],
|
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],
|
661
664
|
]);
|
662
665
|
|
663
666
|
return (
|
@@ -684,8 +687,10 @@ const JointedThing = () => {
|
|
684
687
|
bodyA,
|
685
688
|
bodyB,
|
686
689
|
[
|
687
|
-
|
688
|
-
[0, 0, 0],
|
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],
|
689
694
|
]);
|
690
695
|
|
691
696
|
return (
|
@@ -712,17 +717,15 @@ const JointedThing = () => {
|
|
712
717
|
bodyA,
|
713
718
|
bodyB,
|
714
719
|
[
|
715
|
-
|
716
|
-
[0, 0, 0],
|
717
|
-
|
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],
|
718
727
|
]);
|
719
728
|
|
720
|
-
useEffect(() => {
|
721
|
-
if (joint.current) {
|
722
|
-
|
723
|
-
}
|
724
|
-
}, [])
|
725
|
-
|
726
729
|
return (
|
727
730
|
<group>
|
728
731
|
<RigidBody ref={bodyA}>
|
@@ -747,9 +750,13 @@ const JointedThing = () => {
|
|
747
750
|
bodyA,
|
748
751
|
bodyB,
|
749
752
|
[
|
750
|
-
|
751
|
-
[0, 0, 0],
|
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],
|
753
760
|
]);
|
754
761
|
|
755
762
|
return (
|