@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.
@@ -113,7 +113,8 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
113
113
  const collider = world.createCollider(colliderDesc, rigidBody);
114
114
  return collider;
115
115
  };
116
- const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisionEvents = false) => {
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 (type) {
139
+ switch (options.colliders) {
139
140
  case "cuboid":
140
141
  {
141
142
  geometry.computeBoundingBox();
@@ -186,11 +187,9 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
186
187
  z: rz,
187
188
  w: rw
188
189
  });
189
-
190
- if (hasCollisionEvents) {
191
- desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
192
- }
193
-
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);
194
193
  const collider = world.createCollider(desc, rigidBody);
195
194
  colliders.push(collider);
196
195
  }
@@ -209,7 +208,47 @@ const scaleVertices = (vertices, scale) => {
209
208
  return scaledVerts;
210
209
  };
211
210
 
212
- // TODO: Flesh this out
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
- applyImpulse({
222
- x,
223
- y,
224
- z
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
- x,
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,
@@ -261,31 +293,37 @@ const createRigidBodyApi = ref => {
261
293
  return new three.Quaternion(x, y, z, w);
262
294
  },
263
295
 
264
- setNextKinematicRotation({
265
- x,
266
- y,
267
- z
268
- }) {
269
- ref.current().setNextKinematicRotation({
296
+ setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
297
+ w: 1
298
+ }), true),
299
+
300
+ linvel() {
301
+ const {
270
302
  x,
271
303
  y,
272
- z,
273
- w: 1
274
- });
304
+ z
305
+ } = ref.current().linvel();
306
+ return new three.Vector3(x, y, z);
275
307
  },
276
308
 
277
- setNextKinematicTranslation({
278
- x,
279
- y,
280
- z
281
- }) {
282
- ref.current().setNextKinematicTranslation({
309
+ setLinvel: velocity => ref.current().setLinvel(velocity, true),
310
+
311
+ angvel() {
312
+ const {
283
313
  x,
284
314
  y,
285
315
  z
286
- });
287
- }
316
+ } = ref.current().angvel();
317
+ return new three.Vector3(x, y, z);
318
+ },
288
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)
289
327
  };
290
328
  }; // TODO: Flesh this out
291
329
 
@@ -485,47 +523,6 @@ const Physics = ({
485
523
  }, children);
486
524
  };
487
525
 
488
- function _defineProperty(obj, key, value) {
489
- if (key in obj) {
490
- Object.defineProperty(obj, key, {
491
- value: value,
492
- enumerable: true,
493
- configurable: true,
494
- writable: true
495
- });
496
- } else {
497
- obj[key] = value;
498
- }
499
-
500
- return obj;
501
- }
502
-
503
- function ownKeys(object, enumerableOnly) {
504
- var keys = Object.keys(object);
505
-
506
- if (Object.getOwnPropertySymbols) {
507
- var symbols = Object.getOwnPropertySymbols(object);
508
- enumerableOnly && (symbols = symbols.filter(function (sym) {
509
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
510
- })), keys.push.apply(keys, symbols);
511
- }
512
-
513
- return keys;
514
- }
515
-
516
- function _objectSpread2(target) {
517
- for (var i = 1; i < arguments.length; i++) {
518
- var source = null != arguments[i] ? arguments[i] : {};
519
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
520
- _defineProperty(target, key, source[key]);
521
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
522
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
523
- });
524
- }
525
-
526
- return target;
527
- }
528
-
529
526
  const useRapier = () => {
530
527
  return React.useContext(RapierContext);
531
528
  };
@@ -591,7 +588,6 @@ const useRigidBody = (options = {}) => {
591
588
  y: worldPosition.y + y * scale.y,
592
589
  z: worldPosition.z + z * scale.z
593
590
  }, false);
594
- console.log(rigidBody.isKinematic());
595
591
  const eulerAngles = new three.Euler(rx, ry, rz, 'XYZ');
596
592
  const rotation = new three.Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
597
593
  rigidBody.setRotation({
@@ -603,13 +599,12 @@ const useRigidBody = (options = {}) => {
603
599
  rigidBody.resetForces(false);
604
600
  rigidBody.resetTorques(false);
605
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;
606
- const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
607
- const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, colliderSetting, world, hasCollisionEvents) : [];
608
- rigidBody.wakeUp();
602
+ const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
603
+ colliders: colliderSetting
604
+ }), world) : [];
609
605
  rigidBodyMeshes.set(rigidBody.handle, ref.current);
610
606
  return () => {
611
- const actualBody = world.getRigidBody(rigidBody.handle);
612
- world.removeRigidBody(actualBody);
607
+ world.removeRigidBody(rigidBody);
613
608
  autoColliders.forEach(collider => world.removeCollider(collider));
614
609
  rigidBodyRef.current = undefined;
615
610
  rigidBodyMeshes.delete(rigidBody.handle);
@@ -658,7 +653,9 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
658
653
  const {
659
654
  world
660
655
  } = useRapier();
661
- const [ref, rigidBody] = useRigidBody(rigidBodyOptions);
656
+ const [ref, rigidBody] = useRigidBody(_objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
657
+ colliders: false
658
+ }));
662
659
  React.useEffect(() => {
663
660
  if (!colliderOptions) {
664
661
  return;
@@ -971,7 +968,7 @@ const AnyCollider = _ref2 => {
971
968
  const ref = React.useRef(null);
972
969
  React.useEffect(() => {
973
970
  const scale = ref.current.getWorldScale(new three.Vector3());
974
- const collider = createColliderFromOptions(props, world, world.getRigidBody(rigidBody.handle), scale, hasCollisionEvents);
971
+ const collider = createColliderFromOptions(props, world, rigidBody.raw(), scale, hasCollisionEvents);
975
972
  return () => {
976
973
  world.removeCollider(collider);
977
974
  };
@@ -113,7 +113,8 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
113
113
  const collider = world.createCollider(colliderDesc, rigidBody);
114
114
  return collider;
115
115
  };
116
- const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisionEvents = false) => {
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 (type) {
139
+ switch (options.colliders) {
139
140
  case "cuboid":
140
141
  {
141
142
  geometry.computeBoundingBox();
@@ -186,11 +187,9 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
186
187
  z: rz,
187
188
  w: rw
188
189
  });
189
-
190
- if (hasCollisionEvents) {
191
- desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
192
- }
193
-
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);
194
193
  const collider = world.createCollider(desc, rigidBody);
195
194
  colliders.push(collider);
196
195
  }
@@ -209,7 +208,47 @@ const scaleVertices = (vertices, scale) => {
209
208
  return scaledVerts;
210
209
  };
211
210
 
212
- // TODO: Flesh this out
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
- applyImpulse({
222
- x,
223
- y,
224
- z
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
- x,
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,
@@ -261,31 +293,37 @@ const createRigidBodyApi = ref => {
261
293
  return new three.Quaternion(x, y, z, w);
262
294
  },
263
295
 
264
- setNextKinematicRotation({
265
- x,
266
- y,
267
- z
268
- }) {
269
- ref.current().setNextKinematicRotation({
296
+ setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
297
+ w: 1
298
+ }), true),
299
+
300
+ linvel() {
301
+ const {
270
302
  x,
271
303
  y,
272
- z,
273
- w: 1
274
- });
304
+ z
305
+ } = ref.current().linvel();
306
+ return new three.Vector3(x, y, z);
275
307
  },
276
308
 
277
- setNextKinematicTranslation({
278
- x,
279
- y,
280
- z
281
- }) {
282
- ref.current().setNextKinematicTranslation({
309
+ setLinvel: velocity => ref.current().setLinvel(velocity, true),
310
+
311
+ angvel() {
312
+ const {
283
313
  x,
284
314
  y,
285
315
  z
286
- });
287
- }
316
+ } = ref.current().angvel();
317
+ return new three.Vector3(x, y, z);
318
+ },
288
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)
289
327
  };
290
328
  }; // TODO: Flesh this out
291
329
 
@@ -485,47 +523,6 @@ const Physics = ({
485
523
  }, children);
486
524
  };
487
525
 
488
- function _defineProperty(obj, key, value) {
489
- if (key in obj) {
490
- Object.defineProperty(obj, key, {
491
- value: value,
492
- enumerable: true,
493
- configurable: true,
494
- writable: true
495
- });
496
- } else {
497
- obj[key] = value;
498
- }
499
-
500
- return obj;
501
- }
502
-
503
- function ownKeys(object, enumerableOnly) {
504
- var keys = Object.keys(object);
505
-
506
- if (Object.getOwnPropertySymbols) {
507
- var symbols = Object.getOwnPropertySymbols(object);
508
- enumerableOnly && (symbols = symbols.filter(function (sym) {
509
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
510
- })), keys.push.apply(keys, symbols);
511
- }
512
-
513
- return keys;
514
- }
515
-
516
- function _objectSpread2(target) {
517
- for (var i = 1; i < arguments.length; i++) {
518
- var source = null != arguments[i] ? arguments[i] : {};
519
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
520
- _defineProperty(target, key, source[key]);
521
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
522
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
523
- });
524
- }
525
-
526
- return target;
527
- }
528
-
529
526
  const useRapier = () => {
530
527
  return React.useContext(RapierContext);
531
528
  };
@@ -591,7 +588,6 @@ const useRigidBody = (options = {}) => {
591
588
  y: worldPosition.y + y * scale.y,
592
589
  z: worldPosition.z + z * scale.z
593
590
  }, false);
594
- console.log(rigidBody.isKinematic());
595
591
  const eulerAngles = new three.Euler(rx, ry, rz, 'XYZ');
596
592
  const rotation = new three.Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
597
593
  rigidBody.setRotation({
@@ -603,13 +599,12 @@ const useRigidBody = (options = {}) => {
603
599
  rigidBody.resetForces(false);
604
600
  rigidBody.resetTorques(false);
605
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;
606
- const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
607
- const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, colliderSetting, world, hasCollisionEvents) : [];
608
- rigidBody.wakeUp();
602
+ const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
603
+ colliders: colliderSetting
604
+ }), world) : [];
609
605
  rigidBodyMeshes.set(rigidBody.handle, ref.current);
610
606
  return () => {
611
- const actualBody = world.getRigidBody(rigidBody.handle);
612
- world.removeRigidBody(actualBody);
607
+ world.removeRigidBody(rigidBody);
613
608
  autoColliders.forEach(collider => world.removeCollider(collider));
614
609
  rigidBodyRef.current = undefined;
615
610
  rigidBodyMeshes.delete(rigidBody.handle);
@@ -658,7 +653,9 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
658
653
  const {
659
654
  world
660
655
  } = useRapier();
661
- const [ref, rigidBody] = useRigidBody(rigidBodyOptions);
656
+ const [ref, rigidBody] = useRigidBody(_objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
657
+ colliders: false
658
+ }));
662
659
  React.useEffect(() => {
663
660
  if (!colliderOptions) {
664
661
  return;
@@ -971,7 +968,7 @@ const AnyCollider = _ref2 => {
971
968
  const ref = React.useRef(null);
972
969
  React.useEffect(() => {
973
970
  const scale = ref.current.getWorldScale(new three.Vector3());
974
- const collider = createColliderFromOptions(props, world, world.getRigidBody(rigidBody.handle), scale, hasCollisionEvents);
971
+ const collider = createColliderFromOptions(props, world, rigidBody.raw(), scale, hasCollisionEvents);
975
972
  return () => {
976
973
  world.removeCollider(collider);
977
974
  };