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