@react-three/rapier 0.1.2 → 0.2.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/CHANGELOG.md +12 -0
- package/dist/declarations/src/Physics.d.ts +10 -5
- package/dist/declarations/src/api.d.ts +33 -0
- package/dist/declarations/src/components.d.ts +11 -2
- package/dist/declarations/src/hooks.d.ts +172 -24
- package/dist/declarations/src/index.d.ts +1 -1
- package/dist/declarations/src/types.d.ts +7 -1
- package/dist/declarations/src/utils.d.ts +4 -4
- package/dist/react-three-rapier.cjs.dev.js +284 -128
- package/dist/react-three-rapier.cjs.prod.js +284 -128
- package/dist/react-three-rapier.esm.js +285 -129
- package/package.json +1 -1
@@ -1,9 +1,9 @@
|
|
1
|
-
import React, { useRef, useMemo, createContext, useContext, useEffect,
|
1
|
+
import React, { useRef, useState, useLayoutEffect, useMemo, createContext, useContext, useEffect, forwardRef, useImperativeHandle, memo } from 'react';
|
2
2
|
import { useAsset } from 'use-asset';
|
3
3
|
import { useFrame } from '@react-three/fiber';
|
4
|
+
import { Vector3, Quaternion, Object3D, Euler, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry } from 'three';
|
4
5
|
import { ColliderDesc, CoefficientCombineRule, ShapeType } from '@dimforge/rapier3d-compat';
|
5
6
|
export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
|
6
|
-
import { Quaternion, Vector3, Object3D, Euler, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry } from 'three';
|
7
7
|
|
8
8
|
const vectorArrayToObject = arr => {
|
9
9
|
const [x, y, z] = arr;
|
@@ -38,7 +38,7 @@ const scaleColliderArgs = (shape, args, scale) => {
|
|
38
38
|
const scaleArray = [scale.x, scale.y, scale.z];
|
39
39
|
return newArgs.map((arg, index) => scaleArray[index] * arg);
|
40
40
|
};
|
41
|
-
const createColliderFromOptions = (options, world,
|
41
|
+
const createColliderFromOptions = (options, world, rigidBodyHandle, scale = {
|
42
42
|
x: 1,
|
43
43
|
y: 1,
|
44
44
|
z: 1
|
@@ -80,12 +80,13 @@ const createColliderFromOptions = (options, world, body, scale = {
|
|
80
80
|
});
|
81
81
|
}
|
82
82
|
|
83
|
-
const collider = world.createCollider(colliderDesc,
|
83
|
+
const collider = world.createCollider(colliderDesc, rigidBodyHandle);
|
84
84
|
return collider;
|
85
85
|
};
|
86
86
|
const createCollidersFromChildren = (object, rigidBody, type, world) => {
|
87
87
|
const colliders = [];
|
88
88
|
let desc;
|
89
|
+
let offset = new Vector3();
|
89
90
|
object.traverse(child => {
|
90
91
|
if ("isMesh" in child) {
|
91
92
|
const {
|
@@ -112,6 +113,7 @@ const createCollidersFromChildren = (object, rigidBody, type, world) => {
|
|
112
113
|
boundingBox
|
113
114
|
} = geometry;
|
114
115
|
const size = boundingBox.getSize(new Vector3());
|
116
|
+
boundingBox.getCenter(offset);
|
115
117
|
desc = ColliderDesc.cuboid(size.x / 2 * scale.x, size.y / 2 * scale.y, size.z / 2 * scale.z);
|
116
118
|
}
|
117
119
|
break;
|
@@ -123,6 +125,7 @@ const createCollidersFromChildren = (object, rigidBody, type, world) => {
|
|
123
125
|
boundingSphere
|
124
126
|
} = geometry;
|
125
127
|
const radius = boundingSphere.radius * scale.x;
|
128
|
+
offset.copy(boundingSphere.center);
|
126
129
|
desc = ColliderDesc.ball(radius);
|
127
130
|
}
|
128
131
|
break;
|
@@ -147,7 +150,7 @@ const createCollidersFromChildren = (object, rigidBody, type, world) => {
|
|
147
150
|
|
148
151
|
|
149
152
|
const parentWorldScale = child.parent.getWorldScale(new Vector3());
|
150
|
-
desc.setTranslation(x * parentWorldScale.x, y * parentWorldScale.y, z * parentWorldScale.z).setRotation({
|
153
|
+
desc.setTranslation((x + offset.x) * parentWorldScale.x, (y + offset.y) * parentWorldScale.y, (z + offset.z) * parentWorldScale.z).setRotation({
|
151
154
|
x: rx,
|
152
155
|
y: ry,
|
153
156
|
z: rz,
|
@@ -171,6 +174,99 @@ const scaleVertices = (vertices, scale) => {
|
|
171
174
|
return scaledVerts;
|
172
175
|
};
|
173
176
|
|
177
|
+
// TODO: Flesh this out
|
178
|
+
const createRigidBodyApi = ref => {
|
179
|
+
return {
|
180
|
+
raw: () => ref.current(),
|
181
|
+
|
182
|
+
get handle() {
|
183
|
+
return ref.current().handle;
|
184
|
+
},
|
185
|
+
|
186
|
+
applyImpulse({
|
187
|
+
x,
|
188
|
+
y,
|
189
|
+
z
|
190
|
+
}) {
|
191
|
+
ref.current().applyImpulse({
|
192
|
+
x,
|
193
|
+
y,
|
194
|
+
z
|
195
|
+
}, true);
|
196
|
+
},
|
197
|
+
|
198
|
+
applyTorqueImpulse({
|
199
|
+
x,
|
200
|
+
y,
|
201
|
+
z
|
202
|
+
}) {
|
203
|
+
ref.current().applyTorqueImpulse({
|
204
|
+
x,
|
205
|
+
y,
|
206
|
+
z
|
207
|
+
}, true);
|
208
|
+
},
|
209
|
+
|
210
|
+
translation() {
|
211
|
+
const {
|
212
|
+
x,
|
213
|
+
y,
|
214
|
+
z
|
215
|
+
} = ref.current().translation();
|
216
|
+
return new Vector3(x, y, z);
|
217
|
+
},
|
218
|
+
|
219
|
+
rotation() {
|
220
|
+
const {
|
221
|
+
x,
|
222
|
+
y,
|
223
|
+
z,
|
224
|
+
w
|
225
|
+
} = ref.current().rotation();
|
226
|
+
return new Quaternion(x, y, z, w);
|
227
|
+
}
|
228
|
+
|
229
|
+
};
|
230
|
+
}; // TODO: Flesh this out
|
231
|
+
|
232
|
+
const createColliderApi = ref => {
|
233
|
+
return {
|
234
|
+
raw: () => ref.current(),
|
235
|
+
|
236
|
+
get handle() {
|
237
|
+
return ref.current().handle;
|
238
|
+
}
|
239
|
+
|
240
|
+
};
|
241
|
+
};
|
242
|
+
const createWorldApi = ref => {
|
243
|
+
return {
|
244
|
+
raw: () => ref.current(),
|
245
|
+
getCollider: handle => ref.current().getCollider(handle),
|
246
|
+
getRigidBody: handle => ref.current().getRigidBody(handle),
|
247
|
+
createRigidBody: desc => ref.current().createRigidBody(desc),
|
248
|
+
createCollider: (desc, rigidBodyHandle) => ref.current().createCollider(desc, rigidBodyHandle),
|
249
|
+
removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
|
250
|
+
removeCollider: collider => ref.current().removeCollider(collider, true),
|
251
|
+
createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB),
|
252
|
+
removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
|
253
|
+
forEachCollider: callback => ref.current().forEachCollider(callback)
|
254
|
+
};
|
255
|
+
}; // TODO: Broken currently, waiting for Rapier3D to fix
|
256
|
+
|
257
|
+
const createJointApi = ref => {
|
258
|
+
return {
|
259
|
+
raw: () => ref.current(),
|
260
|
+
|
261
|
+
get handle() {
|
262
|
+
return ref.current().handle;
|
263
|
+
},
|
264
|
+
|
265
|
+
configureMotorPosition: (targetPos, stiffness, damping) => ref.current().configureMotorPosition(targetPos, stiffness, damping),
|
266
|
+
configureMotorVelocity: (targetVel, damping) => ref.current().configureMotorVelocity(targetVel, damping)
|
267
|
+
};
|
268
|
+
};
|
269
|
+
|
174
270
|
const RapierContext = /*#__PURE__*/createContext(undefined);
|
175
271
|
|
176
272
|
const importRapier = async () => {
|
@@ -185,30 +281,81 @@ const Physics = ({
|
|
185
281
|
children
|
186
282
|
}) => {
|
187
283
|
const rapier = useAsset(importRapier);
|
188
|
-
const
|
189
|
-
const
|
190
|
-
if (!
|
191
|
-
|
192
|
-
|
193
|
-
|
284
|
+
const worldRef = useRef();
|
285
|
+
const getWorldRef = useRef(() => {
|
286
|
+
if (!worldRef.current) {
|
287
|
+
const world = new rapier.World(vectorArrayToObject(_gravity));
|
288
|
+
worldRef.current = world;
|
289
|
+
}
|
290
|
+
|
291
|
+
return worldRef.current;
|
292
|
+
});
|
293
|
+
const [colliderMeshes] = useState(() => new Map());
|
294
|
+
const [rigidBodyMeshes] = useState(() => new Map()); // Init world
|
295
|
+
|
296
|
+
useLayoutEffect(() => {
|
297
|
+
const world = getWorldRef.current();
|
298
|
+
return () => {
|
299
|
+
if (world) {
|
300
|
+
world.free();
|
301
|
+
worldRef.current = undefined;
|
302
|
+
}
|
303
|
+
};
|
304
|
+
}, []);
|
194
305
|
const time = useRef(performance.now());
|
195
306
|
useFrame(context => {
|
196
|
-
|
307
|
+
const world = worldRef.current;
|
308
|
+
if (!world) return; // Set timestep to current delta, to allow for variable frame rates
|
197
309
|
// We cap the delta at 100, so that the physics simulation doesn't get wild
|
310
|
+
|
198
311
|
const now = performance.now();
|
199
312
|
const delta = Math.min(100, now - time.current);
|
200
313
|
world.timestep = delta / 1000;
|
201
|
-
world.step(); //
|
314
|
+
world.step(); // Update meshes
|
202
315
|
|
203
|
-
|
316
|
+
rigidBodyMeshes.forEach((mesh, handle) => {
|
317
|
+
const rigidBody = world.getRigidBody(handle);
|
318
|
+
|
319
|
+
if (!rigidBody || rigidBody.isSleeping() || rigidBody.isFixed() || !mesh.parent) {
|
320
|
+
return;
|
321
|
+
}
|
322
|
+
|
323
|
+
const {
|
324
|
+
x,
|
325
|
+
y,
|
326
|
+
z
|
327
|
+
} = rigidBody.translation();
|
328
|
+
const {
|
329
|
+
x: rx,
|
330
|
+
y: ry,
|
331
|
+
z: rz,
|
332
|
+
w: rw
|
333
|
+
} = rigidBody.rotation();
|
334
|
+
const scale = mesh.getWorldScale(new Vector3()); // haha matrixes I have no idea what I'm doing :)
|
335
|
+
|
336
|
+
const o = new Object3D();
|
337
|
+
o.position.set(x, y, z);
|
338
|
+
o.rotation.setFromQuaternion(new Quaternion(rx, ry, rz, rw));
|
339
|
+
o.scale.set(scale.x, scale.y, scale.z);
|
340
|
+
o.updateMatrix();
|
341
|
+
o.applyMatrix4(mesh.parent.matrixWorld.clone().invert());
|
342
|
+
o.updateMatrix();
|
343
|
+
mesh.position.setFromMatrixPosition(o.matrix);
|
344
|
+
mesh.rotation.setFromRotationMatrix(o.matrix);
|
345
|
+
});
|
204
346
|
time.current = now;
|
205
347
|
});
|
348
|
+
const api = useMemo(() => createWorldApi(getWorldRef), []);
|
206
349
|
const context = useMemo(() => ({
|
207
|
-
|
208
|
-
world,
|
209
|
-
|
210
|
-
|
211
|
-
|
350
|
+
rapier,
|
351
|
+
world: api,
|
352
|
+
physicsOptions: {
|
353
|
+
colliders: _colliders,
|
354
|
+
gravity: _gravity
|
355
|
+
},
|
356
|
+
colliderMeshes,
|
357
|
+
rigidBodyMeshes
|
358
|
+
}), []);
|
212
359
|
return /*#__PURE__*/React.createElement(RapierContext.Provider, {
|
213
360
|
value: context
|
214
361
|
}, children);
|
@@ -257,66 +404,45 @@ function _objectSpread2(target) {
|
|
257
404
|
|
258
405
|
const useRapier = () => {
|
259
406
|
return useContext(RapierContext);
|
260
|
-
}; // Private hook for updating the simulations on objects
|
261
|
-
|
262
|
-
const useRapierStep = callback => {
|
263
|
-
const {
|
264
|
-
stepFuncs
|
265
|
-
} = useRapier();
|
266
|
-
useEffect(() => {
|
267
|
-
stepFuncs.push(callback);
|
268
|
-
return () => {
|
269
|
-
const index = stepFuncs.indexOf(callback);
|
270
|
-
stepFuncs.splice(index, 1);
|
271
|
-
};
|
272
|
-
}, [callback]);
|
273
|
-
};
|
274
|
-
const useCollider = (body, options = {}) => {
|
275
|
-
const {
|
276
|
-
RAPIER,
|
277
|
-
world
|
278
|
-
} = useRapier();
|
279
|
-
const collider = useMemo(() => {
|
280
|
-
return createColliderFromOptions(options, world, body);
|
281
|
-
}, []);
|
282
|
-
useEffect(() => {
|
283
|
-
return () => {
|
284
|
-
world.removeCollider(collider, false);
|
285
|
-
};
|
286
|
-
}, []);
|
287
|
-
return [collider];
|
288
407
|
};
|
289
|
-
const useRigidBody = options => {
|
408
|
+
const useRigidBody = (options = {}) => {
|
290
409
|
const {
|
291
|
-
|
410
|
+
rapier,
|
292
411
|
world,
|
293
|
-
|
412
|
+
rigidBodyMeshes,
|
413
|
+
physicsOptions
|
294
414
|
} = useRapier();
|
295
415
|
const ref = useRef(); // Create rigidbody
|
296
416
|
|
297
|
-
const
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
417
|
+
const rigidBodyRef = useRef();
|
418
|
+
const getRigidBodyRef = useRef(() => {
|
419
|
+
if (!rigidBodyRef.current) {
|
420
|
+
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd;
|
421
|
+
|
422
|
+
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
423
|
+
const [lvx, lvy, lvz] = (_options$linearVeloci = options === null || options === void 0 ? void 0 : options.linearVelocity) !== null && _options$linearVeloci !== void 0 ? _options$linearVeloci : [0, 0, 0];
|
424
|
+
const [avx, avy, avz] = (_options$angularVeloc = options === null || options === void 0 ? void 0 : options.angularVelocity) !== null && _options$angularVeloc !== void 0 ? _options$angularVeloc : [0, 0, 0];
|
425
|
+
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
426
|
+
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
427
|
+
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
428
|
+
const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
429
|
+
x: avx,
|
430
|
+
y: avy,
|
431
|
+
z: avz
|
432
|
+
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
|
433
|
+
const rigidBody = world.createRigidBody(desc);
|
434
|
+
rigidBodyRef.current = rigidBody;
|
435
|
+
}
|
436
|
+
|
437
|
+
return rigidBodyRef.current;
|
438
|
+
}); // Setup
|
316
439
|
|
317
440
|
useEffect(() => {
|
318
441
|
var _ref$current$parent, _ref, _options$colliders;
|
319
442
|
|
443
|
+
const rigidBody = getRigidBodyRef.current();
|
444
|
+
rigidBodyRef.current = rigidBody;
|
445
|
+
|
320
446
|
if (!ref.current) {
|
321
447
|
ref.current = new Object3D();
|
322
448
|
} // Get intitial world transforms
|
@@ -348,43 +474,42 @@ const useRigidBody = options => {
|
|
348
474
|
}, false);
|
349
475
|
rigidBody.resetForces(false);
|
350
476
|
rigidBody.resetTorques(false);
|
351
|
-
const colliderSetting = (_ref = (_options$colliders = options === null || options === void 0 ? void 0 : options.colliders) !== null && _options$colliders !== void 0 ? _options$colliders : colliders) !== null && _ref !== void 0 ? _ref : false;
|
477
|
+
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;
|
352
478
|
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, colliderSetting, world) : [];
|
479
|
+
rigidBody.wakeUp();
|
480
|
+
rigidBodyMeshes.set(rigidBody.handle, ref.current);
|
353
481
|
return () => {
|
482
|
+
autoColliders.forEach(collider => world.removeCollider(collider));
|
354
483
|
world.removeRigidBody(rigidBody);
|
355
|
-
|
484
|
+
rigidBodyRef.current = undefined;
|
485
|
+
rigidBodyMeshes.delete(rigidBody.handle);
|
356
486
|
};
|
357
487
|
}, []);
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
} = rigidBody.rotation();
|
371
|
-
const scale = ref.current.getWorldScale(new Vector3());
|
372
|
-
|
373
|
-
if (ref.current.parent) {
|
374
|
-
// haha matrixes I have no idea what I'm doing :)
|
375
|
-
const o = new Object3D();
|
376
|
-
o.position.set(x, y, z);
|
377
|
-
o.rotation.setFromQuaternion(new Quaternion(rx, ry, rz, rw));
|
378
|
-
o.scale.set(scale.x, scale.y, scale.z);
|
379
|
-
o.updateMatrix();
|
380
|
-
o.applyMatrix4(ref.current.parent.matrixWorld.clone().invert());
|
381
|
-
o.updateMatrix();
|
382
|
-
ref.current.position.setFromMatrixPosition(o.matrix);
|
383
|
-
ref.current.rotation.setFromRotationMatrix(o.matrix);
|
384
|
-
}
|
488
|
+
const api = useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
|
489
|
+
return [ref, api];
|
490
|
+
};
|
491
|
+
const useCollider = (body, options = {}) => {
|
492
|
+
const {
|
493
|
+
world
|
494
|
+
} = useRapier();
|
495
|
+
const colliderRef = useRef();
|
496
|
+
const objectRef = useRef();
|
497
|
+
const getColliderRef = useRef(() => {
|
498
|
+
if (!colliderRef.current) {
|
499
|
+
colliderRef.current = createColliderFromOptions(options, world, body.handle);
|
385
500
|
}
|
501
|
+
|
502
|
+
return colliderRef.current;
|
386
503
|
});
|
387
|
-
|
504
|
+
useEffect(() => {
|
505
|
+
const collider = getColliderRef.current();
|
506
|
+
return () => {
|
507
|
+
if (collider) world.removeCollider(collider);
|
508
|
+
colliderRef.current = undefined;
|
509
|
+
};
|
510
|
+
}, []);
|
511
|
+
const api = useMemo(() => createColliderApi(getColliderRef), []);
|
512
|
+
return [objectRef, api];
|
388
513
|
};
|
389
514
|
const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
|
390
515
|
const {
|
@@ -397,9 +522,9 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
|
|
397
522
|
}
|
398
523
|
|
399
524
|
const scale = ref.current.getWorldScale(new Vector3());
|
400
|
-
const collider = createColliderFromOptions(colliderOptions, world, rigidBody, scale);
|
525
|
+
const collider = createColliderFromOptions(colliderOptions, world, rigidBody.handle, scale);
|
401
526
|
return () => {
|
402
|
-
world.removeCollider(collider
|
527
|
+
world.removeCollider(collider);
|
403
528
|
};
|
404
529
|
}, []);
|
405
530
|
return [ref, rigidBody];
|
@@ -525,20 +650,42 @@ const useRoundConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
|
525
650
|
|
526
651
|
const useImpulseJoint = (body1, body2, params) => {
|
527
652
|
const {
|
528
|
-
world
|
529
|
-
RAPIER
|
653
|
+
world
|
530
654
|
} = useRapier();
|
531
|
-
|
532
|
-
|
655
|
+
const jointRef = useRef();
|
656
|
+
const getJointRef = useRef(() => {
|
657
|
+
if (!jointRef.current) {
|
658
|
+
let rb1;
|
659
|
+
let rb2;
|
660
|
+
|
661
|
+
if ('handle' in body1 && 'handle' in body2) {
|
662
|
+
rb1 = world.getRigidBody(body1.handle);
|
663
|
+
rb2 = world.getRigidBody(body2.handle);
|
664
|
+
jointRef.current = world.createImpulseJoint(params, rb1, rb2);
|
665
|
+
}
|
533
666
|
|
534
|
-
|
535
|
-
|
667
|
+
if ('current' in body1 && body1.current && 'current' in body2 && body2.current) {
|
668
|
+
rb1 = world.getRigidBody(body1.current.handle);
|
669
|
+
rb2 = world.getRigidBody(body2.current.handle);
|
670
|
+
const newJoint = world.createImpulseJoint(params, rb1, rb2);
|
671
|
+
jointRef.current = newJoint;
|
672
|
+
}
|
536
673
|
}
|
537
674
|
|
675
|
+
return jointRef.current;
|
676
|
+
});
|
677
|
+
useEffect(() => {
|
678
|
+
const joint = getJointRef.current();
|
538
679
|
return () => {
|
539
|
-
if (joint)
|
680
|
+
if (joint) {
|
681
|
+
console.log('remove joint', joint);
|
682
|
+
world.removeImpulseJoint(joint);
|
683
|
+
jointRef.current = undefined;
|
684
|
+
}
|
540
685
|
};
|
541
|
-
}, [
|
686
|
+
}, []);
|
687
|
+
const api = useMemo(() => createJointApi(getJointRef), []);
|
688
|
+
return api;
|
542
689
|
};
|
543
690
|
/**
|
544
691
|
*
|
@@ -549,9 +696,9 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
549
696
|
|
550
697
|
const useFixedJoint = (body1, body2, [body1Anchor, body1LocalFrame, body2Anchor, body2LocalFrame]) => {
|
551
698
|
const {
|
552
|
-
|
699
|
+
rapier
|
553
700
|
} = useRapier();
|
554
|
-
return useImpulseJoint(body1, body2,
|
701
|
+
return useImpulseJoint(body1, body2, rapier.JointData.fixed(vectorArrayToObject(body1Anchor), _objectSpread2(_objectSpread2({}, vectorArrayToObject(body1LocalFrame)), {}, {
|
555
702
|
w: 1
|
556
703
|
}), vectorArrayToObject(body2Anchor), _objectSpread2(_objectSpread2({}, vectorArrayToObject(body2LocalFrame)), {}, {
|
557
704
|
w: 1
|
@@ -566,9 +713,9 @@ const useFixedJoint = (body1, body2, [body1Anchor, body1LocalFrame, body2Anchor,
|
|
566
713
|
|
567
714
|
const useSphericalJoint = (body1, body2, [body1Anchor, body2Anchor]) => {
|
568
715
|
const {
|
569
|
-
|
716
|
+
rapier
|
570
717
|
} = useRapier();
|
571
|
-
return useImpulseJoint(body1, body2,
|
718
|
+
return useImpulseJoint(body1, body2, rapier.JointData.spherical(vectorArrayToObject(body1Anchor), vectorArrayToObject(body2Anchor)));
|
572
719
|
};
|
573
720
|
/**
|
574
721
|
* The revolute joint prevents any relative movement between two rigid-bodies, except for relative
|
@@ -578,9 +725,9 @@ const useSphericalJoint = (body1, body2, [body1Anchor, body2Anchor]) => {
|
|
578
725
|
|
579
726
|
const useRevoluteJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
|
580
727
|
const {
|
581
|
-
|
728
|
+
rapier
|
582
729
|
} = useRapier();
|
583
|
-
return useImpulseJoint(body1, body2,
|
730
|
+
return useImpulseJoint(body1, body2, rapier.JointData.revolute(vectorArrayToObject(body1Anchor), vectorArrayToObject(body2Anchor), vectorArrayToObject(axis)));
|
584
731
|
};
|
585
732
|
/**
|
586
733
|
* The prismatic joint prevents any relative movement between two rigid-bodies, except for relative translations along one axis.
|
@@ -590,9 +737,9 @@ const useRevoluteJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
|
|
590
737
|
|
591
738
|
const usePrismaticJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
|
592
739
|
const {
|
593
|
-
|
740
|
+
rapier
|
594
741
|
} = useRapier();
|
595
|
-
return useImpulseJoint(body1, body2,
|
742
|
+
return useImpulseJoint(body1, body2, rapier.JointData.prismatic(vectorArrayToObject(body1Anchor), vectorArrayToObject(body2Anchor), vectorArrayToObject(axis)));
|
596
743
|
};
|
597
744
|
|
598
745
|
function _extends() {
|
@@ -647,7 +794,8 @@ function _objectWithoutProperties(source, excluded) {
|
|
647
794
|
return target;
|
648
795
|
}
|
649
796
|
|
650
|
-
const _excluded = ["children"]
|
797
|
+
const _excluded = ["children"],
|
798
|
+
_excluded2 = ["children"];
|
651
799
|
const RigidBodyContext = /*#__PURE__*/createContext(undefined);
|
652
800
|
|
653
801
|
const useParentRigidBody = () => useContext(RigidBodyContext); // RigidBody
|
@@ -659,28 +807,36 @@ const RigidBody = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
659
807
|
} = _ref,
|
660
808
|
props = _objectWithoutProperties(_ref, _excluded);
|
661
809
|
|
662
|
-
const [
|
810
|
+
const [object, rigidBody] = useRigidBody(props);
|
663
811
|
useImperativeHandle(ref, () => rigidBody);
|
664
812
|
return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
|
665
|
-
value: [
|
666
|
-
}, /*#__PURE__*/React.createElement("
|
667
|
-
ref:
|
813
|
+
value: [object, rigidBody]
|
814
|
+
}, /*#__PURE__*/React.createElement("object3D", {
|
815
|
+
ref: object
|
668
816
|
}, children));
|
669
817
|
}); // Colliders
|
670
818
|
|
671
|
-
const AnyCollider =
|
819
|
+
const AnyCollider = _ref2 => {
|
820
|
+
let {
|
821
|
+
children
|
822
|
+
} = _ref2,
|
823
|
+
props = _objectWithoutProperties(_ref2, _excluded2);
|
824
|
+
|
672
825
|
const {
|
673
826
|
world
|
674
827
|
} = useRapier();
|
675
|
-
const [
|
828
|
+
const [, rigidBody] = useParentRigidBody();
|
829
|
+
const ref = useRef(null);
|
676
830
|
useEffect(() => {
|
677
|
-
const scale =
|
678
|
-
const collider = createColliderFromOptions(props, world, rigidBody, scale);
|
831
|
+
const scale = ref.current.getWorldScale(new Vector3());
|
832
|
+
const collider = createColliderFromOptions(props, world, rigidBody.handle, scale);
|
679
833
|
return () => {
|
680
|
-
world.removeCollider(collider
|
834
|
+
world.removeCollider(collider);
|
681
835
|
};
|
682
836
|
}, []);
|
683
|
-
return
|
837
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
838
|
+
ref: ref
|
839
|
+
}, children);
|
684
840
|
};
|
685
841
|
|
686
842
|
const CuboidCollider = props => {
|
@@ -829,7 +985,7 @@ const Debug = () => {
|
|
829
985
|
|
830
986
|
useFrame(() => {
|
831
987
|
const newColliders = [];
|
832
|
-
world.
|
988
|
+
world.forEachCollider(collider => {
|
833
989
|
newColliders.push(collider.handle);
|
834
990
|
});
|
835
991
|
setColliders(newColliders);
|