@react-three/rapier 0.3.1 → 0.4.2
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 +22 -0
- package/dist/declarations/src/api.d.ts +114 -18
- package/dist/declarations/src/components.d.ts +1 -9
- package/dist/declarations/src/hooks.d.ts +21 -161
- package/dist/declarations/src/types.d.ts +10 -0
- package/dist/declarations/src/utils.d.ts +4 -4
- package/dist/react-three-rapier.cjs.dev.js +124 -99
- package/dist/react-three-rapier.cjs.prod.js +124 -99
- package/dist/react-three-rapier.esm.js +125 -100
- package/package.json +2 -2
- package/readme.md +182 -0
@@ -63,7 +63,7 @@ const scaleColliderArgs = (shape, args, scale) => {
|
|
63
63
|
const scaleArray = [scale.x, scale.y, scale.z];
|
64
64
|
return newArgs.map((arg, index) => scaleArray[index] * arg);
|
65
65
|
};
|
66
|
-
const createColliderFromOptions = (options, world,
|
66
|
+
const createColliderFromOptions = (options, world, rigidBody, scale = {
|
67
67
|
x: 1,
|
68
68
|
y: 1,
|
69
69
|
z: 1
|
@@ -110,10 +110,11 @@ const createColliderFromOptions = (options, world, rigidBodyHandle, scale = {
|
|
110
110
|
});
|
111
111
|
}
|
112
112
|
|
113
|
-
const collider = world.createCollider(colliderDesc,
|
113
|
+
const collider = world.createCollider(colliderDesc, rigidBody);
|
114
114
|
return collider;
|
115
115
|
};
|
116
|
-
const createCollidersFromChildren = (object, rigidBody,
|
116
|
+
const createCollidersFromChildren = (object, rigidBody, options, world) => {
|
117
|
+
const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
|
117
118
|
const colliders = [];
|
118
119
|
let desc;
|
119
120
|
let offset = new three.Vector3();
|
@@ -135,7 +136,7 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
|
|
135
136
|
} = new three.Quaternion().setFromEuler(child.rotation);
|
136
137
|
const scale = child.getWorldScale(new three.Vector3());
|
137
138
|
|
138
|
-
switch (
|
139
|
+
switch (options.colliders) {
|
139
140
|
case "cuboid":
|
140
141
|
{
|
141
142
|
geometry.computeBoundingBox();
|
@@ -186,12 +187,10 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
|
|
186
187
|
z: rz,
|
187
188
|
w: rw
|
188
189
|
});
|
189
|
-
|
190
|
-
if (
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
const collider = world.createCollider(desc, rigidBody.handle);
|
190
|
+
if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
|
191
|
+
if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
|
192
|
+
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
193
|
+
const collider = world.createCollider(desc, rigidBody);
|
195
194
|
colliders.push(collider);
|
196
195
|
}
|
197
196
|
});
|
@@ -209,7 +208,47 @@ const scaleVertices = (vertices, scale) => {
|
|
209
208
|
return scaledVerts;
|
210
209
|
};
|
211
210
|
|
212
|
-
|
211
|
+
function _defineProperty(obj, key, value) {
|
212
|
+
if (key in obj) {
|
213
|
+
Object.defineProperty(obj, key, {
|
214
|
+
value: value,
|
215
|
+
enumerable: true,
|
216
|
+
configurable: true,
|
217
|
+
writable: true
|
218
|
+
});
|
219
|
+
} else {
|
220
|
+
obj[key] = value;
|
221
|
+
}
|
222
|
+
|
223
|
+
return obj;
|
224
|
+
}
|
225
|
+
|
226
|
+
function ownKeys(object, enumerableOnly) {
|
227
|
+
var keys = Object.keys(object);
|
228
|
+
|
229
|
+
if (Object.getOwnPropertySymbols) {
|
230
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
231
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
232
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
233
|
+
})), keys.push.apply(keys, symbols);
|
234
|
+
}
|
235
|
+
|
236
|
+
return keys;
|
237
|
+
}
|
238
|
+
|
239
|
+
function _objectSpread2(target) {
|
240
|
+
for (var i = 1; i < arguments.length; i++) {
|
241
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
242
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
243
|
+
_defineProperty(target, key, source[key]);
|
244
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
245
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
246
|
+
});
|
247
|
+
}
|
248
|
+
|
249
|
+
return target;
|
250
|
+
}
|
251
|
+
|
213
252
|
const createRigidBodyApi = ref => {
|
214
253
|
return {
|
215
254
|
raw: () => ref.current(),
|
@@ -218,30 +257,21 @@ const createRigidBodyApi = ref => {
|
|
218
257
|
return ref.current().handle;
|
219
258
|
},
|
220
259
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
}) {
|
226
|
-
ref.current().applyImpulse({
|
227
|
-
x,
|
228
|
-
y,
|
229
|
-
z
|
230
|
-
}, true);
|
260
|
+
mass: () => ref.current().mass(),
|
261
|
+
|
262
|
+
applyImpulse(impulseVector) {
|
263
|
+
ref.current().applyImpulse(impulseVector, true);
|
231
264
|
},
|
232
265
|
|
233
|
-
applyTorqueImpulse({
|
234
|
-
|
235
|
-
y,
|
236
|
-
z
|
237
|
-
}) {
|
238
|
-
ref.current().applyTorqueImpulse({
|
239
|
-
x,
|
240
|
-
y,
|
241
|
-
z
|
242
|
-
}, true);
|
266
|
+
applyTorqueImpulse(torqueVector) {
|
267
|
+
ref.current().applyTorqueImpulse(torqueVector, true);
|
243
268
|
},
|
244
269
|
|
270
|
+
applyImpulseAtPoint: (impulseVector, impulsePoint) => ref.current().applyImpulseAtPoint(impulseVector, impulsePoint, true),
|
271
|
+
addForce: force => ref.current().addForce(force, true),
|
272
|
+
addForceAtPoint: (force, point) => ref.current().addForceAtPoint(force, point, true),
|
273
|
+
addTorque: torque => ref.current().addTorque(torque, true),
|
274
|
+
|
245
275
|
translation() {
|
246
276
|
const {
|
247
277
|
x,
|
@@ -251,6 +281,8 @@ const createRigidBodyApi = ref => {
|
|
251
281
|
return new three.Vector3(x, y, z);
|
252
282
|
},
|
253
283
|
|
284
|
+
setTranslation: translation => ref.current().setTranslation(translation, true),
|
285
|
+
|
254
286
|
rotation() {
|
255
287
|
const {
|
256
288
|
x,
|
@@ -259,8 +291,39 @@ const createRigidBodyApi = ref => {
|
|
259
291
|
w
|
260
292
|
} = ref.current().rotation();
|
261
293
|
return new three.Quaternion(x, y, z, w);
|
262
|
-
}
|
294
|
+
},
|
295
|
+
|
296
|
+
setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
|
297
|
+
w: 1
|
298
|
+
}), true),
|
299
|
+
|
300
|
+
linvel() {
|
301
|
+
const {
|
302
|
+
x,
|
303
|
+
y,
|
304
|
+
z
|
305
|
+
} = ref.current().linvel();
|
306
|
+
return new three.Vector3(x, y, z);
|
307
|
+
},
|
263
308
|
|
309
|
+
setLinvel: velocity => ref.current().setLinvel(velocity, true),
|
310
|
+
|
311
|
+
angvel() {
|
312
|
+
const {
|
313
|
+
x,
|
314
|
+
y,
|
315
|
+
z
|
316
|
+
} = ref.current().angvel();
|
317
|
+
return new three.Vector3(x, y, z);
|
318
|
+
},
|
319
|
+
|
320
|
+
setAngvel: velocity => ref.current().setAngvel(velocity, true),
|
321
|
+
setNextKinematicRotation: rotation => ref.current().setNextKinematicRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
|
322
|
+
w: 1
|
323
|
+
})),
|
324
|
+
setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
|
325
|
+
resetForces: () => ref.current().resetForces(true),
|
326
|
+
resetTorques: () => ref.current().resetTorques(true)
|
264
327
|
};
|
265
328
|
}; // TODO: Flesh this out
|
266
329
|
|
@@ -280,10 +343,10 @@ const createWorldApi = ref => {
|
|
280
343
|
getCollider: handle => ref.current().getCollider(handle),
|
281
344
|
getRigidBody: handle => ref.current().getRigidBody(handle),
|
282
345
|
createRigidBody: desc => ref.current().createRigidBody(desc),
|
283
|
-
createCollider: (desc,
|
346
|
+
createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
|
284
347
|
removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
|
285
348
|
removeCollider: collider => ref.current().removeCollider(collider, true),
|
286
|
-
createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB),
|
349
|
+
createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
|
287
350
|
removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
|
288
351
|
forEachCollider: callback => ref.current().forEachCollider(callback)
|
289
352
|
};
|
@@ -330,7 +393,7 @@ const Physics = ({
|
|
330
393
|
const [rigidBodyEvents] = React.useState(() => new Map());
|
331
394
|
const [eventQueue] = React.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
|
332
395
|
|
333
|
-
React.
|
396
|
+
React.useEffect(() => {
|
334
397
|
const world = getWorldRef.current();
|
335
398
|
return () => {
|
336
399
|
if (world) {
|
@@ -399,10 +462,12 @@ const Physics = ({
|
|
399
462
|
}); // Collision events
|
400
463
|
|
401
464
|
eventQueue.drainCollisionEvents((handle1, handle2, started) => {
|
465
|
+
var _collider1$parent, _collider2$parent;
|
466
|
+
|
402
467
|
const collider1 = world.getCollider(handle1);
|
403
468
|
const collider2 = world.getCollider(handle2);
|
404
|
-
const rigidBodyHandle1 = collider1.parent();
|
405
|
-
const rigidBodyHandle2 = collider2.parent();
|
469
|
+
const rigidBodyHandle1 = (_collider1$parent = collider1.parent()) === null || _collider1$parent === void 0 ? void 0 : _collider1$parent.handle;
|
470
|
+
const rigidBodyHandle2 = (_collider2$parent = collider2.parent()) === null || _collider2$parent === void 0 ? void 0 : _collider2$parent.handle;
|
406
471
|
|
407
472
|
if (!collider1 || !collider2 || !rigidBodyHandle1 || !rigidBodyHandle2) {
|
408
473
|
return;
|
@@ -414,7 +479,7 @@ const Physics = ({
|
|
414
479
|
const events2 = rigidBodyEvents.get(rigidBodyHandle2);
|
415
480
|
|
416
481
|
if (started) {
|
417
|
-
world.contactPair(
|
482
|
+
world.contactPair(collider1, collider2, (manifold, flipped) => {
|
418
483
|
var _events1$onCollisionE, _events2$onCollisionE;
|
419
484
|
|
420
485
|
events1 === null || events1 === void 0 ? void 0 : (_events1$onCollisionE = events1.onCollisionEnter) === null || _events1$onCollisionE === void 0 ? void 0 : _events1$onCollisionE.call(events1, {
|
@@ -458,47 +523,6 @@ const Physics = ({
|
|
458
523
|
}, children);
|
459
524
|
};
|
460
525
|
|
461
|
-
function _defineProperty(obj, key, value) {
|
462
|
-
if (key in obj) {
|
463
|
-
Object.defineProperty(obj, key, {
|
464
|
-
value: value,
|
465
|
-
enumerable: true,
|
466
|
-
configurable: true,
|
467
|
-
writable: true
|
468
|
-
});
|
469
|
-
} else {
|
470
|
-
obj[key] = value;
|
471
|
-
}
|
472
|
-
|
473
|
-
return obj;
|
474
|
-
}
|
475
|
-
|
476
|
-
function ownKeys(object, enumerableOnly) {
|
477
|
-
var keys = Object.keys(object);
|
478
|
-
|
479
|
-
if (Object.getOwnPropertySymbols) {
|
480
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
481
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
482
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
483
|
-
})), keys.push.apply(keys, symbols);
|
484
|
-
}
|
485
|
-
|
486
|
-
return keys;
|
487
|
-
}
|
488
|
-
|
489
|
-
function _objectSpread2(target) {
|
490
|
-
for (var i = 1; i < arguments.length; i++) {
|
491
|
-
var source = null != arguments[i] ? arguments[i] : {};
|
492
|
-
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
493
|
-
_defineProperty(target, key, source[key]);
|
494
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
495
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
496
|
-
});
|
497
|
-
}
|
498
|
-
|
499
|
-
return target;
|
500
|
-
}
|
501
|
-
|
502
526
|
const useRapier = () => {
|
503
527
|
return React.useContext(RapierContext);
|
504
528
|
};
|
@@ -529,7 +553,7 @@ const useRigidBody = (options = {}) => {
|
|
529
553
|
z: avz
|
530
554
|
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
|
531
555
|
const rigidBody = world.createRigidBody(desc);
|
532
|
-
rigidBodyRef.current = rigidBody;
|
556
|
+
rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
|
533
557
|
}
|
534
558
|
|
535
559
|
return rigidBodyRef.current;
|
@@ -575,13 +599,11 @@ const useRigidBody = (options = {}) => {
|
|
575
599
|
rigidBody.resetForces(false);
|
576
600
|
rigidBody.resetTorques(false);
|
577
601
|
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;
|
578
|
-
const
|
579
|
-
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, colliderSetting, world, hasCollisionEvents) : [];
|
580
|
-
rigidBody.wakeUp();
|
602
|
+
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, options, world) : [];
|
581
603
|
rigidBodyMeshes.set(rigidBody.handle, ref.current);
|
582
604
|
return () => {
|
583
|
-
autoColliders.forEach(collider => world.removeCollider(collider));
|
584
605
|
world.removeRigidBody(rigidBody);
|
606
|
+
autoColliders.forEach(collider => world.removeCollider(collider));
|
585
607
|
rigidBodyRef.current = undefined;
|
586
608
|
rigidBodyMeshes.delete(rigidBody.handle);
|
587
609
|
};
|
@@ -610,7 +632,7 @@ const useCollider = (body, options = {}) => {
|
|
610
632
|
const objectRef = React.useRef();
|
611
633
|
const getColliderRef = React.useRef(() => {
|
612
634
|
if (!colliderRef.current) {
|
613
|
-
colliderRef.current = createColliderFromOptions(options, world, body.handle);
|
635
|
+
colliderRef.current = createColliderFromOptions(options, world, world.getRigidBody(body.handle));
|
614
636
|
}
|
615
637
|
|
616
638
|
return colliderRef.current;
|
@@ -636,7 +658,7 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
|
|
636
658
|
}
|
637
659
|
|
638
660
|
const scale = ref.current.getWorldScale(new three.Vector3());
|
639
|
-
const collider = createColliderFromOptions(colliderOptions, world, rigidBody.handle, scale);
|
661
|
+
const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
|
640
662
|
return () => {
|
641
663
|
world.removeCollider(collider);
|
642
664
|
};
|
@@ -792,7 +814,6 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
792
814
|
const joint = getJointRef.current();
|
793
815
|
return () => {
|
794
816
|
if (joint) {
|
795
|
-
console.log('remove joint', joint);
|
796
817
|
world.removeImpulseJoint(joint);
|
797
818
|
jointRef.current = undefined;
|
798
819
|
}
|
@@ -943,7 +964,7 @@ const AnyCollider = _ref2 => {
|
|
943
964
|
const ref = React.useRef(null);
|
944
965
|
React.useEffect(() => {
|
945
966
|
const scale = ref.current.getWorldScale(new three.Vector3());
|
946
|
-
const collider = createColliderFromOptions(props, world, rigidBody.
|
967
|
+
const collider = createColliderFromOptions(props, world, rigidBody.raw(), scale, hasCollisionEvents);
|
947
968
|
return () => {
|
948
969
|
world.removeCollider(collider);
|
949
970
|
};
|
@@ -1000,20 +1021,20 @@ const ConvexHullCollider = props => {
|
|
1000
1021
|
};
|
1001
1022
|
|
1002
1023
|
const geometryFromCollider = collider => {
|
1003
|
-
switch (collider.
|
1024
|
+
switch (collider.shape.type) {
|
1004
1025
|
case rapier3dCompat.ShapeType.Cuboid:
|
1005
1026
|
{
|
1006
1027
|
const {
|
1007
1028
|
x,
|
1008
1029
|
y,
|
1009
1030
|
z
|
1010
|
-
} = collider.halfExtents
|
1031
|
+
} = collider.shape.halfExtents;
|
1011
1032
|
return new three.BoxBufferGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
|
1012
1033
|
}
|
1013
1034
|
|
1014
1035
|
case rapier3dCompat.ShapeType.Ball:
|
1015
1036
|
{
|
1016
|
-
const r = collider.radius
|
1037
|
+
const r = collider.shape.radius;
|
1017
1038
|
return new three.SphereBufferGeometry(r + +0.01, 8, 8);
|
1018
1039
|
}
|
1019
1040
|
|
@@ -1021,10 +1042,12 @@ const geometryFromCollider = collider => {
|
|
1021
1042
|
{
|
1022
1043
|
var _g$index;
|
1023
1044
|
|
1024
|
-
const v = collider.vertices
|
1025
|
-
const i = collider.indices
|
1026
|
-
const g = new three.BufferGeometry();
|
1027
|
-
|
1045
|
+
const v = collider.shape.vertices;
|
1046
|
+
const i = collider.shape.indices;
|
1047
|
+
const g = new three.BufferGeometry(); // Vertices are not always a float3darray (???), so we need to convert them
|
1048
|
+
|
1049
|
+
const safeVerts = Float32Array.from(v);
|
1050
|
+
g.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
|
1028
1051
|
(_g$index = g.index) === null || _g$index === void 0 ? void 0 : _g$index.set(i);
|
1029
1052
|
g.setDrawRange(0, g.attributes.position.array.length / 3 - 1);
|
1030
1053
|
return g;
|
@@ -1032,16 +1055,18 @@ const geometryFromCollider = collider => {
|
|
1032
1055
|
|
1033
1056
|
case rapier3dCompat.ShapeType.ConvexPolyhedron:
|
1034
1057
|
{
|
1035
|
-
const cv = collider.vertices()
|
1058
|
+
const cv = collider.shape.vertices; // Vertices are not always a float3darray (???), so we need to convert them
|
1059
|
+
|
1060
|
+
const safeVerts = Float32Array.from(cv);
|
1036
1061
|
const cg = new three.BufferGeometry();
|
1037
|
-
cg.setAttribute("position", new three.BufferAttribute(
|
1062
|
+
cg.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
|
1038
1063
|
return cg;
|
1039
1064
|
}
|
1040
1065
|
|
1041
1066
|
case rapier3dCompat.ShapeType.Cylinder:
|
1042
1067
|
{
|
1043
|
-
const r = collider.radius
|
1044
|
-
const h = collider.halfHeight
|
1068
|
+
const r = collider.shape.radius;
|
1069
|
+
const h = collider.shape.halfHeight;
|
1045
1070
|
const g = new three.CylinderBufferGeometry(r, r, h);
|
1046
1071
|
return g;
|
1047
1072
|
}
|