@react-three/rapier 0.4.0 → 0.4.3
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/CHANGELOG.md +19 -0
- package/dist/declarations/src/api.d.ts +114 -20
- package/dist/declarations/src/components.d.ts +1 -11
- package/dist/declarations/src/hooks.d.ts +21 -201
- package/dist/declarations/src/types.d.ts +10 -0
- package/dist/declarations/src/utils.d.ts +2 -2
- package/dist/react-three-rapier.cjs.dev.js +91 -94
- package/dist/react-three-rapier.cjs.prod.js +91 -94
- package/dist/react-three-rapier.esm.js +91 -94
- package/package.json +1 -1
- package/readme.md +12 -6
@@ -88,7 +88,8 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
|
|
88
88
|
const collider = world.createCollider(colliderDesc, rigidBody);
|
89
89
|
return collider;
|
90
90
|
};
|
91
|
-
const createCollidersFromChildren = (object, rigidBody,
|
91
|
+
const createCollidersFromChildren = (object, rigidBody, options, world) => {
|
92
|
+
const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
|
92
93
|
const colliders = [];
|
93
94
|
let desc;
|
94
95
|
let offset = new Vector3();
|
@@ -110,7 +111,7 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
|
|
110
111
|
} = new Quaternion().setFromEuler(child.rotation);
|
111
112
|
const scale = child.getWorldScale(new Vector3());
|
112
113
|
|
113
|
-
switch (
|
114
|
+
switch (options.colliders) {
|
114
115
|
case "cuboid":
|
115
116
|
{
|
116
117
|
geometry.computeBoundingBox();
|
@@ -161,11 +162,9 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
|
|
161
162
|
z: rz,
|
162
163
|
w: rw
|
163
164
|
});
|
164
|
-
|
165
|
-
if (
|
166
|
-
|
167
|
-
}
|
168
|
-
|
165
|
+
if (hasCollisionEvents) desc.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
|
166
|
+
if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
|
167
|
+
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
169
168
|
const collider = world.createCollider(desc, rigidBody);
|
170
169
|
colliders.push(collider);
|
171
170
|
}
|
@@ -184,7 +183,47 @@ const scaleVertices = (vertices, scale) => {
|
|
184
183
|
return scaledVerts;
|
185
184
|
};
|
186
185
|
|
187
|
-
|
186
|
+
function _defineProperty(obj, key, value) {
|
187
|
+
if (key in obj) {
|
188
|
+
Object.defineProperty(obj, key, {
|
189
|
+
value: value,
|
190
|
+
enumerable: true,
|
191
|
+
configurable: true,
|
192
|
+
writable: true
|
193
|
+
});
|
194
|
+
} else {
|
195
|
+
obj[key] = value;
|
196
|
+
}
|
197
|
+
|
198
|
+
return obj;
|
199
|
+
}
|
200
|
+
|
201
|
+
function ownKeys(object, enumerableOnly) {
|
202
|
+
var keys = Object.keys(object);
|
203
|
+
|
204
|
+
if (Object.getOwnPropertySymbols) {
|
205
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
206
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
207
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
208
|
+
})), keys.push.apply(keys, symbols);
|
209
|
+
}
|
210
|
+
|
211
|
+
return keys;
|
212
|
+
}
|
213
|
+
|
214
|
+
function _objectSpread2(target) {
|
215
|
+
for (var i = 1; i < arguments.length; i++) {
|
216
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
217
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
218
|
+
_defineProperty(target, key, source[key]);
|
219
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
220
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
221
|
+
});
|
222
|
+
}
|
223
|
+
|
224
|
+
return target;
|
225
|
+
}
|
226
|
+
|
188
227
|
const createRigidBodyApi = ref => {
|
189
228
|
return {
|
190
229
|
raw: () => ref.current(),
|
@@ -193,30 +232,21 @@ const createRigidBodyApi = ref => {
|
|
193
232
|
return ref.current().handle;
|
194
233
|
},
|
195
234
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
}) {
|
201
|
-
ref.current().applyImpulse({
|
202
|
-
x,
|
203
|
-
y,
|
204
|
-
z
|
205
|
-
}, true);
|
235
|
+
mass: () => ref.current().mass(),
|
236
|
+
|
237
|
+
applyImpulse(impulseVector) {
|
238
|
+
ref.current().applyImpulse(impulseVector, true);
|
206
239
|
},
|
207
240
|
|
208
|
-
applyTorqueImpulse({
|
209
|
-
|
210
|
-
y,
|
211
|
-
z
|
212
|
-
}) {
|
213
|
-
ref.current().applyTorqueImpulse({
|
214
|
-
x,
|
215
|
-
y,
|
216
|
-
z
|
217
|
-
}, true);
|
241
|
+
applyTorqueImpulse(torqueVector) {
|
242
|
+
ref.current().applyTorqueImpulse(torqueVector, true);
|
218
243
|
},
|
219
244
|
|
245
|
+
applyImpulseAtPoint: (impulseVector, impulsePoint) => ref.current().applyImpulseAtPoint(impulseVector, impulsePoint, true),
|
246
|
+
addForce: force => ref.current().addForce(force, true),
|
247
|
+
addForceAtPoint: (force, point) => ref.current().addForceAtPoint(force, point, true),
|
248
|
+
addTorque: torque => ref.current().addTorque(torque, true),
|
249
|
+
|
220
250
|
translation() {
|
221
251
|
const {
|
222
252
|
x,
|
@@ -226,6 +256,8 @@ const createRigidBodyApi = ref => {
|
|
226
256
|
return new Vector3(x, y, z);
|
227
257
|
},
|
228
258
|
|
259
|
+
setTranslation: translation => ref.current().setTranslation(translation, true),
|
260
|
+
|
229
261
|
rotation() {
|
230
262
|
const {
|
231
263
|
x,
|
@@ -236,31 +268,37 @@ const createRigidBodyApi = ref => {
|
|
236
268
|
return new Quaternion(x, y, z, w);
|
237
269
|
},
|
238
270
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
271
|
+
setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
|
272
|
+
w: 1
|
273
|
+
}), true),
|
274
|
+
|
275
|
+
linvel() {
|
276
|
+
const {
|
245
277
|
x,
|
246
278
|
y,
|
247
|
-
z
|
248
|
-
|
249
|
-
|
279
|
+
z
|
280
|
+
} = ref.current().linvel();
|
281
|
+
return new Vector3(x, y, z);
|
250
282
|
},
|
251
283
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
}) {
|
257
|
-
ref.current().setNextKinematicTranslation({
|
284
|
+
setLinvel: velocity => ref.current().setLinvel(velocity, true),
|
285
|
+
|
286
|
+
angvel() {
|
287
|
+
const {
|
258
288
|
x,
|
259
289
|
y,
|
260
290
|
z
|
261
|
-
});
|
262
|
-
|
291
|
+
} = ref.current().angvel();
|
292
|
+
return new Vector3(x, y, z);
|
293
|
+
},
|
263
294
|
|
295
|
+
setAngvel: velocity => ref.current().setAngvel(velocity, true),
|
296
|
+
setNextKinematicRotation: rotation => ref.current().setNextKinematicRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
|
297
|
+
w: 1
|
298
|
+
})),
|
299
|
+
setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
|
300
|
+
resetForces: () => ref.current().resetForces(true),
|
301
|
+
resetTorques: () => ref.current().resetTorques(true)
|
264
302
|
};
|
265
303
|
}; // TODO: Flesh this out
|
266
304
|
|
@@ -460,47 +498,6 @@ const Physics = ({
|
|
460
498
|
}, children);
|
461
499
|
};
|
462
500
|
|
463
|
-
function _defineProperty(obj, key, value) {
|
464
|
-
if (key in obj) {
|
465
|
-
Object.defineProperty(obj, key, {
|
466
|
-
value: value,
|
467
|
-
enumerable: true,
|
468
|
-
configurable: true,
|
469
|
-
writable: true
|
470
|
-
});
|
471
|
-
} else {
|
472
|
-
obj[key] = value;
|
473
|
-
}
|
474
|
-
|
475
|
-
return obj;
|
476
|
-
}
|
477
|
-
|
478
|
-
function ownKeys(object, enumerableOnly) {
|
479
|
-
var keys = Object.keys(object);
|
480
|
-
|
481
|
-
if (Object.getOwnPropertySymbols) {
|
482
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
483
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
484
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
485
|
-
})), keys.push.apply(keys, symbols);
|
486
|
-
}
|
487
|
-
|
488
|
-
return keys;
|
489
|
-
}
|
490
|
-
|
491
|
-
function _objectSpread2(target) {
|
492
|
-
for (var i = 1; i < arguments.length; i++) {
|
493
|
-
var source = null != arguments[i] ? arguments[i] : {};
|
494
|
-
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
495
|
-
_defineProperty(target, key, source[key]);
|
496
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
497
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
498
|
-
});
|
499
|
-
}
|
500
|
-
|
501
|
-
return target;
|
502
|
-
}
|
503
|
-
|
504
501
|
const useRapier = () => {
|
505
502
|
return useContext(RapierContext);
|
506
503
|
};
|
@@ -566,7 +563,6 @@ const useRigidBody = (options = {}) => {
|
|
566
563
|
y: worldPosition.y + y * scale.y,
|
567
564
|
z: worldPosition.z + z * scale.z
|
568
565
|
}, false);
|
569
|
-
console.log(rigidBody.isKinematic());
|
570
566
|
const eulerAngles = new Euler(rx, ry, rz, 'XYZ');
|
571
567
|
const rotation = new Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
|
572
568
|
rigidBody.setRotation({
|
@@ -578,13 +574,12 @@ const useRigidBody = (options = {}) => {
|
|
578
574
|
rigidBody.resetForces(false);
|
579
575
|
rigidBody.resetTorques(false);
|
580
576
|
const colliderSetting = (_ref = (_options$colliders = options === null || options === void 0 ? void 0 : options.colliders) !== null && _options$colliders !== void 0 ? _options$colliders : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
|
581
|
-
const
|
582
|
-
|
583
|
-
|
577
|
+
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
|
578
|
+
colliders: colliderSetting
|
579
|
+
}), world) : [];
|
584
580
|
rigidBodyMeshes.set(rigidBody.handle, ref.current);
|
585
581
|
return () => {
|
586
|
-
|
587
|
-
world.removeRigidBody(actualBody);
|
582
|
+
world.removeRigidBody(rigidBody);
|
588
583
|
autoColliders.forEach(collider => world.removeCollider(collider));
|
589
584
|
rigidBodyRef.current = undefined;
|
590
585
|
rigidBodyMeshes.delete(rigidBody.handle);
|
@@ -633,7 +628,9 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
|
|
633
628
|
const {
|
634
629
|
world
|
635
630
|
} = useRapier();
|
636
|
-
const [ref, rigidBody] = useRigidBody(rigidBodyOptions)
|
631
|
+
const [ref, rigidBody] = useRigidBody(_objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
|
632
|
+
colliders: false
|
633
|
+
}));
|
637
634
|
useEffect(() => {
|
638
635
|
if (!colliderOptions) {
|
639
636
|
return;
|
@@ -946,7 +943,7 @@ const AnyCollider = _ref2 => {
|
|
946
943
|
const ref = useRef(null);
|
947
944
|
useEffect(() => {
|
948
945
|
const scale = ref.current.getWorldScale(new Vector3());
|
949
|
-
const collider = createColliderFromOptions(props, world,
|
946
|
+
const collider = createColliderFromOptions(props, world, rigidBody.raw(), scale, hasCollisionEvents);
|
950
947
|
return () => {
|
951
948
|
world.removeCollider(collider);
|
952
949
|
};
|
package/package.json
CHANGED
package/readme.md
CHANGED
@@ -5,18 +5,24 @@
|
|
5
5
|
## Usage
|
6
6
|
|
7
7
|
```tsx
|
8
|
-
import { Box } from "@react-three/drei";
|
8
|
+
import { Box, Torus } from "@react-three/drei";
|
9
9
|
import { Canvas } from "@react-three/fiber";
|
10
10
|
import { Physics, RigidBody } from "@react-three/rapier";
|
11
11
|
|
12
12
|
const App = () => {
|
13
13
|
return (
|
14
14
|
<Canvas>
|
15
|
-
<
|
16
|
-
<
|
17
|
-
<
|
18
|
-
|
19
|
-
|
15
|
+
<Suspense>
|
16
|
+
<Physics>
|
17
|
+
<RigidBody colliders={"hull"} restitution={2}>
|
18
|
+
<Torus />
|
19
|
+
</RigidBody>
|
20
|
+
|
21
|
+
<RigidBody position={[0, -2, 0]} type="kinematicPosition">
|
22
|
+
<Box args={[20, 0.5, 20]} />
|
23
|
+
</RigidBody>
|
24
|
+
</Physics>
|
25
|
+
</Suspense>
|
20
26
|
</Canvas>
|
21
27
|
);
|
22
28
|
};
|