@react-three/rapier 0.3.1 → 0.4.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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # @react-three/rapier
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - dd535aa: Update to @dimforge/rapier3d-compat@0.8.1, pinned version
8
+
9
+ ### Patch Changes
10
+
11
+ - dd535aa: Better <Physics /> lifecycle making reinitialization more stable
12
+
3
13
  ## 0.3.1
4
14
 
5
15
  ### Patch Changes
@@ -8,6 +8,8 @@ export declare const createRigidBodyApi: (ref: RefGetter<RigidBody>) => {
8
8
  applyTorqueImpulse({ x, y, z }: Vector3): void;
9
9
  translation(): Vector3;
10
10
  rotation(): Quaternion;
11
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
12
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
11
13
  };
12
14
  export declare const createColliderApi: (ref: RefGetter<Collider>) => {
13
15
  raw: () => Collider | undefined;
@@ -18,7 +20,7 @@ export declare const createWorldApi: (ref: RefGetter<World>) => {
18
20
  getCollider: (handle: number) => Collider;
19
21
  getRigidBody: (handle: number) => RigidBody;
20
22
  createRigidBody: (desc: RigidBodyDesc) => RigidBody;
21
- createCollider: (desc: ColliderDesc, rigidBodyHandle: number) => Collider;
23
+ createCollider: (desc: ColliderDesc, rigidBody: RigidBody) => Collider;
22
24
  removeRigidBody: (rigidBody: RigidBody) => void;
23
25
  removeCollider: (collider: Collider) => void;
24
26
  createImpulseJoint: (params: JointData, rigidBodyA: RigidBody, rigidBodyB: RigidBody) => ImpulseJoint;
@@ -12,6 +12,8 @@ export declare const RigidBody: React.ForwardRefExoticComponent<RigidBodyProps &
12
12
  applyTorqueImpulse({ x, y, z }: Vector3): void;
13
13
  translation(): Vector3;
14
14
  rotation(): import("three").Quaternion;
15
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
16
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
15
17
  }>>;
16
18
  declare type UseColliderOptionsRequiredArgs<T> = Omit<UseColliderOptions<T>, "args"> & {
17
19
  args: T;
@@ -12,6 +12,8 @@ export declare const useRigidBody: <O extends Object3D<import("three").Event>>(o
12
12
  applyTorqueImpulse({ x, y, z }: Vector3): void;
13
13
  translation(): Vector3;
14
14
  rotation(): Quaternion;
15
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
16
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
15
17
  }];
16
18
  export declare const useCollider: <A>(body: RigidBodyApi, options?: UseColliderOptions<A>) => (React.MutableRefObject<Object3D<import("three").Event> | undefined> | {
17
19
  raw: () => Rapier.Collider | undefined;
@@ -24,6 +26,8 @@ export declare const useRigidBodyWithCollider: <A, O extends Object3D<import("th
24
26
  applyTorqueImpulse({ x, y, z }: Vector3): void;
25
27
  translation(): Vector3;
26
28
  rotation(): Quaternion;
29
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
30
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
27
31
  }];
28
32
  export declare const useCuboid: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<CuboidArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
29
33
  raw: () => Rapier.RigidBody | undefined;
@@ -32,6 +36,8 @@ export declare const useCuboid: <T extends Object3D<import("three").Event>>(rigi
32
36
  applyTorqueImpulse({ x, y, z }: Vector3): void;
33
37
  translation(): Vector3;
34
38
  rotation(): Quaternion;
39
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
40
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
35
41
  }];
36
42
  export declare const useBall: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<BallArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
37
43
  raw: () => Rapier.RigidBody | undefined;
@@ -40,6 +46,8 @@ export declare const useBall: <T extends Object3D<import("three").Event>>(rigidB
40
46
  applyTorqueImpulse({ x, y, z }: Vector3): void;
41
47
  translation(): Vector3;
42
48
  rotation(): Quaternion;
49
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
50
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
43
51
  }];
44
52
  export declare const useCapsule: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<CapsuleArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
45
53
  raw: () => Rapier.RigidBody | undefined;
@@ -48,6 +56,8 @@ export declare const useCapsule: <T extends Object3D<import("three").Event>>(rig
48
56
  applyTorqueImpulse({ x, y, z }: Vector3): void;
49
57
  translation(): Vector3;
50
58
  rotation(): Quaternion;
59
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
60
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
51
61
  }];
52
62
  export declare const useHeightfield: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<HeightfieldArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
53
63
  raw: () => Rapier.RigidBody | undefined;
@@ -56,6 +66,8 @@ export declare const useHeightfield: <T extends Object3D<import("three").Event>>
56
66
  applyTorqueImpulse({ x, y, z }: Vector3): void;
57
67
  translation(): Vector3;
58
68
  rotation(): Quaternion;
69
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
70
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
59
71
  }];
60
72
  /**
61
73
  * Create a trimesh collider and rigid body.
@@ -71,6 +83,8 @@ export declare const useTrimesh: {
71
83
  applyTorqueImpulse({ x, y, z }: Vector3): void;
72
84
  translation(): Vector3;
73
85
  rotation(): Quaternion;
86
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
87
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
74
88
  }];
75
89
  fromMesh<T_1 extends Object3D<import("three").Event>>(mesh: Mesh, rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<TrimeshArgs>): [ref: React.MutableRefObject<T_1>, rigidBody: {
76
90
  raw: () => Rapier.RigidBody | undefined;
@@ -79,6 +93,8 @@ export declare const useTrimesh: {
79
93
  applyTorqueImpulse({ x, y, z }: Vector3): void;
80
94
  translation(): Vector3;
81
95
  rotation(): Quaternion;
96
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
97
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
82
98
  }];
83
99
  };
84
100
  export declare const usePolyline: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<PolylineArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
@@ -88,6 +104,8 @@ export declare const usePolyline: <T extends Object3D<import("three").Event>>(ri
88
104
  applyTorqueImpulse({ x, y, z }: Vector3): void;
89
105
  translation(): Vector3;
90
106
  rotation(): Quaternion;
107
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
108
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
91
109
  }];
92
110
  export declare const useRoundCuboid: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundCuboidArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
93
111
  raw: () => Rapier.RigidBody | undefined;
@@ -96,6 +114,8 @@ export declare const useRoundCuboid: <T extends Object3D<import("three").Event>>
96
114
  applyTorqueImpulse({ x, y, z }: Vector3): void;
97
115
  translation(): Vector3;
98
116
  rotation(): Quaternion;
117
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
118
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
99
119
  }];
100
120
  export declare const useCylinder: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<CylinderArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
101
121
  raw: () => Rapier.RigidBody | undefined;
@@ -104,6 +124,8 @@ export declare const useCylinder: <T extends Object3D<import("three").Event>>(ri
104
124
  applyTorqueImpulse({ x, y, z }: Vector3): void;
105
125
  translation(): Vector3;
106
126
  rotation(): Quaternion;
127
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
128
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
107
129
  }];
108
130
  export declare const useRoundCylinder: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundCylinderArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
109
131
  raw: () => Rapier.RigidBody | undefined;
@@ -112,6 +134,8 @@ export declare const useRoundCylinder: <T extends Object3D<import("three").Event
112
134
  applyTorqueImpulse({ x, y, z }: Vector3): void;
113
135
  translation(): Vector3;
114
136
  rotation(): Quaternion;
137
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
138
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
115
139
  }];
116
140
  export declare const useCone: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<ConeArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
117
141
  raw: () => Rapier.RigidBody | undefined;
@@ -120,6 +144,8 @@ export declare const useCone: <T extends Object3D<import("three").Event>>(rigidB
120
144
  applyTorqueImpulse({ x, y, z }: Vector3): void;
121
145
  translation(): Vector3;
122
146
  rotation(): Quaternion;
147
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
148
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
123
149
  }];
124
150
  export declare const useRoundCone: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundCone>) => [ref: React.MutableRefObject<T>, rigidBody: {
125
151
  raw: () => Rapier.RigidBody | undefined;
@@ -128,6 +154,8 @@ export declare const useRoundCone: <T extends Object3D<import("three").Event>>(r
128
154
  applyTorqueImpulse({ x, y, z }: Vector3): void;
129
155
  translation(): Vector3;
130
156
  rotation(): Quaternion;
157
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
158
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
131
159
  }];
132
160
  export declare const useConvexHull: {
133
161
  <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<ConvexHullArgs>): [ref: React.MutableRefObject<T>, rigidBody: {
@@ -137,6 +165,8 @@ export declare const useConvexHull: {
137
165
  applyTorqueImpulse({ x, y, z }: Vector3): void;
138
166
  translation(): Vector3;
139
167
  rotation(): Quaternion;
168
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
169
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
140
170
  }];
141
171
  fromMesh<T_1 extends Object3D<import("three").Event>>(mesh: Mesh, rigidBodyOptions?: UseBodyOptions, colliderOptions?: Omit<UseColliderOptions<ConvexHullArgs>, "colliderArgs">): [ref: React.MutableRefObject<T_1>, rigidBody: {
142
172
  raw: () => Rapier.RigidBody | undefined;
@@ -145,6 +175,8 @@ export declare const useConvexHull: {
145
175
  applyTorqueImpulse({ x, y, z }: Vector3): void;
146
176
  translation(): Vector3;
147
177
  rotation(): Quaternion;
178
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
179
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
148
180
  }];
149
181
  };
150
182
  export declare const useRoundConvexHull: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundConvexHullArgs>) => [ref: React.MutableRefObject<T>, rigidBody: {
@@ -154,6 +186,8 @@ export declare const useRoundConvexHull: <T extends Object3D<import("three").Eve
154
186
  applyTorqueImpulse({ x, y, z }: Vector3): void;
155
187
  translation(): Vector3;
156
188
  rotation(): Quaternion;
189
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
190
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
157
191
  }];
158
192
  export declare const useConvexMesh: {
159
193
  <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<ConvexMeshArgs>): [ref: React.MutableRefObject<Object3D<import("three").Event>>, rigidBody: {
@@ -163,6 +197,8 @@ export declare const useConvexMesh: {
163
197
  applyTorqueImpulse({ x, y, z }: Vector3): void;
164
198
  translation(): Vector3;
165
199
  rotation(): Quaternion;
200
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
201
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
166
202
  }];
167
203
  fromMesh<T_1 extends Object3D<import("three").Event>>(mesh: Mesh, rigidBodyOptions?: UseBodyOptions, colliderOptions?: Omit<UseColliderOptions<ConvexMeshArgs>, "colliderArgs">): [ref: React.MutableRefObject<Object3D<import("three").Event>>, rigidBody: {
168
204
  raw: () => Rapier.RigidBody | undefined;
@@ -171,6 +207,8 @@ export declare const useConvexMesh: {
171
207
  applyTorqueImpulse({ x, y, z }: Vector3): void;
172
208
  translation(): Vector3;
173
209
  rotation(): Quaternion;
210
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
211
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
174
212
  }];
175
213
  };
176
214
  export declare const useRoundConvexMesh: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundConvexMeshArgs>) => [ref: React.MutableRefObject<Object3D<import("three").Event>>, rigidBody: {
@@ -180,6 +218,8 @@ export declare const useRoundConvexMesh: <T extends Object3D<import("three").Eve
180
218
  applyTorqueImpulse({ x, y, z }: Vector3): void;
181
219
  translation(): Vector3;
182
220
  rotation(): Quaternion;
221
+ setNextKinematicRotation({ x, y, z }: Vector3): void;
222
+ setNextKinematicTranslation({ x, y, z }: Vector3): void;
183
223
  }];
184
224
  export declare const useImpulseJoint: <T extends Rapier.ImpulseJoint>(body1: MutableRefObject<RapierRigidBody | undefined | null> | RigidBodyApi, body2: MutableRefObject<RapierRigidBody | undefined | null> | RigidBodyApi, params: Rapier.JointData) => {
185
225
  raw: () => Rapier.ImpulseJoint | undefined;
@@ -1,6 +1,6 @@
1
- import { Collider } from "@dimforge/rapier3d-compat";
1
+ import { Collider, RigidBody } from "@dimforge/rapier3d-compat";
2
2
  import { Object3D, Vector3 } from "three";
3
- import { RapierRigidBody, RigidBodyAutoCollider, RigidBodyShape, RigidBodyTypeString, UseColliderOptions, Vector3Array, WorldApi } from "./types";
3
+ import { RigidBodyAutoCollider, RigidBodyShape, RigidBodyTypeString, UseColliderOptions, Vector3Array, WorldApi } from "./types";
4
4
  export declare const vectorArrayToObject: (arr: Vector3Array) => {
5
5
  x: number;
6
6
  y: number;
@@ -8,10 +8,10 @@ export declare const vectorArrayToObject: (arr: Vector3Array) => {
8
8
  };
9
9
  export declare const rigidBodyTypeFromString: (type: RigidBodyTypeString) => number;
10
10
  export declare const scaleColliderArgs: (shape: RigidBodyShape, args: (number | ArrayLike<number>)[], scale: Vector3) => (number | ArrayLike<number>)[];
11
- export declare const createColliderFromOptions: <A>(options: UseColliderOptions<A>, world: WorldApi, rigidBodyHandle: number, scale?: {
11
+ export declare const createColliderFromOptions: <A>(options: UseColliderOptions<A>, world: WorldApi, rigidBody: RigidBody, scale?: {
12
12
  x: number;
13
13
  y: number;
14
14
  z: number;
15
15
  }, hasCollisionEvents?: boolean) => Collider;
16
- export declare const createCollidersFromChildren: (object: Object3D, rigidBody: RapierRigidBody, type: RigidBodyAutoCollider, world: WorldApi, hasCollisionEvents?: boolean) => Collider[];
16
+ export declare const createCollidersFromChildren: (object: Object3D, rigidBody: RigidBody, type: RigidBodyAutoCollider, world: WorldApi, hasCollisionEvents?: boolean) => Collider[];
17
17
  export declare const scaleVertices: (vertices: ArrayLike<number>, scale: Vector3) => number[];
@@ -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
  });
@@ -259,6 +259,31 @@ const createRigidBodyApi = ref => {
259
259
  w
260
260
  } = ref.current().rotation();
261
261
  return new three.Quaternion(x, y, z, w);
262
+ },
263
+
264
+ setNextKinematicRotation({
265
+ x,
266
+ y,
267
+ z
268
+ }) {
269
+ ref.current().setNextKinematicRotation({
270
+ x,
271
+ y,
272
+ z,
273
+ w: 1
274
+ });
275
+ },
276
+
277
+ setNextKinematicTranslation({
278
+ x,
279
+ y,
280
+ z
281
+ }) {
282
+ ref.current().setNextKinematicTranslation({
283
+ x,
284
+ y,
285
+ z
286
+ });
262
287
  }
263
288
 
264
289
  };
@@ -280,10 +305,10 @@ const createWorldApi = ref => {
280
305
  getCollider: handle => ref.current().getCollider(handle),
281
306
  getRigidBody: handle => ref.current().getRigidBody(handle),
282
307
  createRigidBody: desc => ref.current().createRigidBody(desc),
283
- createCollider: (desc, rigidBodyHandle) => ref.current().createCollider(desc, rigidBodyHandle),
308
+ createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
284
309
  removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
285
310
  removeCollider: collider => ref.current().removeCollider(collider, true),
286
- createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB),
311
+ createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
287
312
  removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
288
313
  forEachCollider: callback => ref.current().forEachCollider(callback)
289
314
  };
@@ -330,7 +355,7 @@ const Physics = ({
330
355
  const [rigidBodyEvents] = React.useState(() => new Map());
331
356
  const [eventQueue] = React.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
332
357
 
333
- React.useLayoutEffect(() => {
358
+ React.useEffect(() => {
334
359
  const world = getWorldRef.current();
335
360
  return () => {
336
361
  if (world) {
@@ -399,10 +424,12 @@ const Physics = ({
399
424
  }); // Collision events
400
425
 
401
426
  eventQueue.drainCollisionEvents((handle1, handle2, started) => {
427
+ var _collider1$parent, _collider2$parent;
428
+
402
429
  const collider1 = world.getCollider(handle1);
403
430
  const collider2 = world.getCollider(handle2);
404
- const rigidBodyHandle1 = collider1.parent();
405
- const rigidBodyHandle2 = collider2.parent();
431
+ const rigidBodyHandle1 = (_collider1$parent = collider1.parent()) === null || _collider1$parent === void 0 ? void 0 : _collider1$parent.handle;
432
+ const rigidBodyHandle2 = (_collider2$parent = collider2.parent()) === null || _collider2$parent === void 0 ? void 0 : _collider2$parent.handle;
406
433
 
407
434
  if (!collider1 || !collider2 || !rigidBodyHandle1 || !rigidBodyHandle2) {
408
435
  return;
@@ -414,7 +441,7 @@ const Physics = ({
414
441
  const events2 = rigidBodyEvents.get(rigidBodyHandle2);
415
442
 
416
443
  if (started) {
417
- world.contactPair(handle1, handle2, (manifold, flipped) => {
444
+ world.contactPair(collider1, collider2, (manifold, flipped) => {
418
445
  var _events1$onCollisionE, _events2$onCollisionE;
419
446
 
420
447
  events1 === null || events1 === void 0 ? void 0 : (_events1$onCollisionE = events1.onCollisionEnter) === null || _events1$onCollisionE === void 0 ? void 0 : _events1$onCollisionE.call(events1, {
@@ -529,7 +556,7 @@ const useRigidBody = (options = {}) => {
529
556
  z: avz
530
557
  }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
531
558
  const rigidBody = world.createRigidBody(desc);
532
- rigidBodyRef.current = rigidBody;
559
+ rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
533
560
  }
534
561
 
535
562
  return rigidBodyRef.current;
@@ -564,6 +591,7 @@ const useRigidBody = (options = {}) => {
564
591
  y: worldPosition.y + y * scale.y,
565
592
  z: worldPosition.z + z * scale.z
566
593
  }, false);
594
+ console.log(rigidBody.isKinematic());
567
595
  const eulerAngles = new three.Euler(rx, ry, rz, 'XYZ');
568
596
  const rotation = new three.Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
569
597
  rigidBody.setRotation({
@@ -580,8 +608,9 @@ const useRigidBody = (options = {}) => {
580
608
  rigidBody.wakeUp();
581
609
  rigidBodyMeshes.set(rigidBody.handle, ref.current);
582
610
  return () => {
611
+ const actualBody = world.getRigidBody(rigidBody.handle);
612
+ world.removeRigidBody(actualBody);
583
613
  autoColliders.forEach(collider => world.removeCollider(collider));
584
- world.removeRigidBody(rigidBody);
585
614
  rigidBodyRef.current = undefined;
586
615
  rigidBodyMeshes.delete(rigidBody.handle);
587
616
  };
@@ -610,7 +639,7 @@ const useCollider = (body, options = {}) => {
610
639
  const objectRef = React.useRef();
611
640
  const getColliderRef = React.useRef(() => {
612
641
  if (!colliderRef.current) {
613
- colliderRef.current = createColliderFromOptions(options, world, body.handle);
642
+ colliderRef.current = createColliderFromOptions(options, world, world.getRigidBody(body.handle));
614
643
  }
615
644
 
616
645
  return colliderRef.current;
@@ -636,7 +665,7 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
636
665
  }
637
666
 
638
667
  const scale = ref.current.getWorldScale(new three.Vector3());
639
- const collider = createColliderFromOptions(colliderOptions, world, rigidBody.handle, scale);
668
+ const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
640
669
  return () => {
641
670
  world.removeCollider(collider);
642
671
  };
@@ -792,7 +821,6 @@ const useImpulseJoint = (body1, body2, params) => {
792
821
  const joint = getJointRef.current();
793
822
  return () => {
794
823
  if (joint) {
795
- console.log('remove joint', joint);
796
824
  world.removeImpulseJoint(joint);
797
825
  jointRef.current = undefined;
798
826
  }
@@ -943,7 +971,7 @@ const AnyCollider = _ref2 => {
943
971
  const ref = React.useRef(null);
944
972
  React.useEffect(() => {
945
973
  const scale = ref.current.getWorldScale(new three.Vector3());
946
- const collider = createColliderFromOptions(props, world, rigidBody.handle, scale, hasCollisionEvents);
974
+ const collider = createColliderFromOptions(props, world, world.getRigidBody(rigidBody.handle), scale, hasCollisionEvents);
947
975
  return () => {
948
976
  world.removeCollider(collider);
949
977
  };
@@ -1000,20 +1028,20 @@ const ConvexHullCollider = props => {
1000
1028
  };
1001
1029
 
1002
1030
  const geometryFromCollider = collider => {
1003
- switch (collider.shapeType()) {
1031
+ switch (collider.shape.type) {
1004
1032
  case rapier3dCompat.ShapeType.Cuboid:
1005
1033
  {
1006
1034
  const {
1007
1035
  x,
1008
1036
  y,
1009
1037
  z
1010
- } = collider.halfExtents();
1038
+ } = collider.shape.halfExtents;
1011
1039
  return new three.BoxBufferGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
1012
1040
  }
1013
1041
 
1014
1042
  case rapier3dCompat.ShapeType.Ball:
1015
1043
  {
1016
- const r = collider.radius();
1044
+ const r = collider.shape.radius;
1017
1045
  return new three.SphereBufferGeometry(r + +0.01, 8, 8);
1018
1046
  }
1019
1047
 
@@ -1021,10 +1049,12 @@ const geometryFromCollider = collider => {
1021
1049
  {
1022
1050
  var _g$index;
1023
1051
 
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));
1052
+ const v = collider.shape.vertices;
1053
+ const i = collider.shape.indices;
1054
+ const g = new three.BufferGeometry(); // Vertices are not always a float3darray (???), so we need to convert them
1055
+
1056
+ const safeVerts = Float32Array.from(v);
1057
+ g.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
1028
1058
  (_g$index = g.index) === null || _g$index === void 0 ? void 0 : _g$index.set(i);
1029
1059
  g.setDrawRange(0, g.attributes.position.array.length / 3 - 1);
1030
1060
  return g;
@@ -1032,16 +1062,18 @@ const geometryFromCollider = collider => {
1032
1062
 
1033
1063
  case rapier3dCompat.ShapeType.ConvexPolyhedron:
1034
1064
  {
1035
- const cv = collider.vertices();
1065
+ const cv = collider.shape.vertices; // Vertices are not always a float3darray (???), so we need to convert them
1066
+
1067
+ const safeVerts = Float32Array.from(cv);
1036
1068
  const cg = new three.BufferGeometry();
1037
- cg.setAttribute("position", new three.BufferAttribute(cv, 3));
1069
+ cg.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
1038
1070
  return cg;
1039
1071
  }
1040
1072
 
1041
1073
  case rapier3dCompat.ShapeType.Cylinder:
1042
1074
  {
1043
- const r = collider.radius();
1044
- const h = collider.halfHeight();
1075
+ const r = collider.shape.radius;
1076
+ const h = collider.shape.halfHeight;
1045
1077
  const g = new three.CylinderBufferGeometry(r, r, h);
1046
1078
  return g;
1047
1079
  }
@@ -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
  });
@@ -259,6 +259,31 @@ const createRigidBodyApi = ref => {
259
259
  w
260
260
  } = ref.current().rotation();
261
261
  return new three.Quaternion(x, y, z, w);
262
+ },
263
+
264
+ setNextKinematicRotation({
265
+ x,
266
+ y,
267
+ z
268
+ }) {
269
+ ref.current().setNextKinematicRotation({
270
+ x,
271
+ y,
272
+ z,
273
+ w: 1
274
+ });
275
+ },
276
+
277
+ setNextKinematicTranslation({
278
+ x,
279
+ y,
280
+ z
281
+ }) {
282
+ ref.current().setNextKinematicTranslation({
283
+ x,
284
+ y,
285
+ z
286
+ });
262
287
  }
263
288
 
264
289
  };
@@ -280,10 +305,10 @@ const createWorldApi = ref => {
280
305
  getCollider: handle => ref.current().getCollider(handle),
281
306
  getRigidBody: handle => ref.current().getRigidBody(handle),
282
307
  createRigidBody: desc => ref.current().createRigidBody(desc),
283
- createCollider: (desc, rigidBodyHandle) => ref.current().createCollider(desc, rigidBodyHandle),
308
+ createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
284
309
  removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
285
310
  removeCollider: collider => ref.current().removeCollider(collider, true),
286
- createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB),
311
+ createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
287
312
  removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
288
313
  forEachCollider: callback => ref.current().forEachCollider(callback)
289
314
  };
@@ -330,7 +355,7 @@ const Physics = ({
330
355
  const [rigidBodyEvents] = React.useState(() => new Map());
331
356
  const [eventQueue] = React.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
332
357
 
333
- React.useLayoutEffect(() => {
358
+ React.useEffect(() => {
334
359
  const world = getWorldRef.current();
335
360
  return () => {
336
361
  if (world) {
@@ -399,10 +424,12 @@ const Physics = ({
399
424
  }); // Collision events
400
425
 
401
426
  eventQueue.drainCollisionEvents((handle1, handle2, started) => {
427
+ var _collider1$parent, _collider2$parent;
428
+
402
429
  const collider1 = world.getCollider(handle1);
403
430
  const collider2 = world.getCollider(handle2);
404
- const rigidBodyHandle1 = collider1.parent();
405
- const rigidBodyHandle2 = collider2.parent();
431
+ const rigidBodyHandle1 = (_collider1$parent = collider1.parent()) === null || _collider1$parent === void 0 ? void 0 : _collider1$parent.handle;
432
+ const rigidBodyHandle2 = (_collider2$parent = collider2.parent()) === null || _collider2$parent === void 0 ? void 0 : _collider2$parent.handle;
406
433
 
407
434
  if (!collider1 || !collider2 || !rigidBodyHandle1 || !rigidBodyHandle2) {
408
435
  return;
@@ -414,7 +441,7 @@ const Physics = ({
414
441
  const events2 = rigidBodyEvents.get(rigidBodyHandle2);
415
442
 
416
443
  if (started) {
417
- world.contactPair(handle1, handle2, (manifold, flipped) => {
444
+ world.contactPair(collider1, collider2, (manifold, flipped) => {
418
445
  var _events1$onCollisionE, _events2$onCollisionE;
419
446
 
420
447
  events1 === null || events1 === void 0 ? void 0 : (_events1$onCollisionE = events1.onCollisionEnter) === null || _events1$onCollisionE === void 0 ? void 0 : _events1$onCollisionE.call(events1, {
@@ -529,7 +556,7 @@ const useRigidBody = (options = {}) => {
529
556
  z: avz
530
557
  }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
531
558
  const rigidBody = world.createRigidBody(desc);
532
- rigidBodyRef.current = rigidBody;
559
+ rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
533
560
  }
534
561
 
535
562
  return rigidBodyRef.current;
@@ -564,6 +591,7 @@ const useRigidBody = (options = {}) => {
564
591
  y: worldPosition.y + y * scale.y,
565
592
  z: worldPosition.z + z * scale.z
566
593
  }, false);
594
+ console.log(rigidBody.isKinematic());
567
595
  const eulerAngles = new three.Euler(rx, ry, rz, 'XYZ');
568
596
  const rotation = new three.Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
569
597
  rigidBody.setRotation({
@@ -580,8 +608,9 @@ const useRigidBody = (options = {}) => {
580
608
  rigidBody.wakeUp();
581
609
  rigidBodyMeshes.set(rigidBody.handle, ref.current);
582
610
  return () => {
611
+ const actualBody = world.getRigidBody(rigidBody.handle);
612
+ world.removeRigidBody(actualBody);
583
613
  autoColliders.forEach(collider => world.removeCollider(collider));
584
- world.removeRigidBody(rigidBody);
585
614
  rigidBodyRef.current = undefined;
586
615
  rigidBodyMeshes.delete(rigidBody.handle);
587
616
  };
@@ -610,7 +639,7 @@ const useCollider = (body, options = {}) => {
610
639
  const objectRef = React.useRef();
611
640
  const getColliderRef = React.useRef(() => {
612
641
  if (!colliderRef.current) {
613
- colliderRef.current = createColliderFromOptions(options, world, body.handle);
642
+ colliderRef.current = createColliderFromOptions(options, world, world.getRigidBody(body.handle));
614
643
  }
615
644
 
616
645
  return colliderRef.current;
@@ -636,7 +665,7 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
636
665
  }
637
666
 
638
667
  const scale = ref.current.getWorldScale(new three.Vector3());
639
- const collider = createColliderFromOptions(colliderOptions, world, rigidBody.handle, scale);
668
+ const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
640
669
  return () => {
641
670
  world.removeCollider(collider);
642
671
  };
@@ -792,7 +821,6 @@ const useImpulseJoint = (body1, body2, params) => {
792
821
  const joint = getJointRef.current();
793
822
  return () => {
794
823
  if (joint) {
795
- console.log('remove joint', joint);
796
824
  world.removeImpulseJoint(joint);
797
825
  jointRef.current = undefined;
798
826
  }
@@ -943,7 +971,7 @@ const AnyCollider = _ref2 => {
943
971
  const ref = React.useRef(null);
944
972
  React.useEffect(() => {
945
973
  const scale = ref.current.getWorldScale(new three.Vector3());
946
- const collider = createColliderFromOptions(props, world, rigidBody.handle, scale, hasCollisionEvents);
974
+ const collider = createColliderFromOptions(props, world, world.getRigidBody(rigidBody.handle), scale, hasCollisionEvents);
947
975
  return () => {
948
976
  world.removeCollider(collider);
949
977
  };
@@ -1000,20 +1028,20 @@ const ConvexHullCollider = props => {
1000
1028
  };
1001
1029
 
1002
1030
  const geometryFromCollider = collider => {
1003
- switch (collider.shapeType()) {
1031
+ switch (collider.shape.type) {
1004
1032
  case rapier3dCompat.ShapeType.Cuboid:
1005
1033
  {
1006
1034
  const {
1007
1035
  x,
1008
1036
  y,
1009
1037
  z
1010
- } = collider.halfExtents();
1038
+ } = collider.shape.halfExtents;
1011
1039
  return new three.BoxBufferGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
1012
1040
  }
1013
1041
 
1014
1042
  case rapier3dCompat.ShapeType.Ball:
1015
1043
  {
1016
- const r = collider.radius();
1044
+ const r = collider.shape.radius;
1017
1045
  return new three.SphereBufferGeometry(r + +0.01, 8, 8);
1018
1046
  }
1019
1047
 
@@ -1021,10 +1049,12 @@ const geometryFromCollider = collider => {
1021
1049
  {
1022
1050
  var _g$index;
1023
1051
 
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));
1052
+ const v = collider.shape.vertices;
1053
+ const i = collider.shape.indices;
1054
+ const g = new three.BufferGeometry(); // Vertices are not always a float3darray (???), so we need to convert them
1055
+
1056
+ const safeVerts = Float32Array.from(v);
1057
+ g.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
1028
1058
  (_g$index = g.index) === null || _g$index === void 0 ? void 0 : _g$index.set(i);
1029
1059
  g.setDrawRange(0, g.attributes.position.array.length / 3 - 1);
1030
1060
  return g;
@@ -1032,16 +1062,18 @@ const geometryFromCollider = collider => {
1032
1062
 
1033
1063
  case rapier3dCompat.ShapeType.ConvexPolyhedron:
1034
1064
  {
1035
- const cv = collider.vertices();
1065
+ const cv = collider.shape.vertices; // Vertices are not always a float3darray (???), so we need to convert them
1066
+
1067
+ const safeVerts = Float32Array.from(cv);
1036
1068
  const cg = new three.BufferGeometry();
1037
- cg.setAttribute("position", new three.BufferAttribute(cv, 3));
1069
+ cg.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
1038
1070
  return cg;
1039
1071
  }
1040
1072
 
1041
1073
  case rapier3dCompat.ShapeType.Cylinder:
1042
1074
  {
1043
- const r = collider.radius();
1044
- const h = collider.halfHeight();
1075
+ const r = collider.shape.radius;
1076
+ const h = collider.shape.halfHeight;
1045
1077
  const g = new three.CylinderBufferGeometry(r, r, h);
1046
1078
  return g;
1047
1079
  }
@@ -1,4 +1,4 @@
1
- import React, { useRef, useState, useLayoutEffect, useMemo, createContext, useContext, useEffect, forwardRef, useImperativeHandle, memo } from 'react';
1
+ import React, { useRef, useState, useEffect, useMemo, createContext, useContext, forwardRef, useImperativeHandle, memo } from 'react';
2
2
  import { useAsset } from 'use-asset';
3
3
  import { useFrame } from '@react-three/fiber';
4
4
  import { ColliderDesc, CoefficientCombineRule, ActiveEvents, EventQueue, ShapeType } from '@dimforge/rapier3d-compat';
@@ -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, rigidBodyHandle, scale = {
41
+ const createColliderFromOptions = (options, world, rigidBody, scale = {
42
42
  x: 1,
43
43
  y: 1,
44
44
  z: 1
@@ -85,7 +85,7 @@ const createColliderFromOptions = (options, world, rigidBodyHandle, scale = {
85
85
  });
86
86
  }
87
87
 
88
- const collider = world.createCollider(colliderDesc, rigidBodyHandle);
88
+ const collider = world.createCollider(colliderDesc, rigidBody);
89
89
  return collider;
90
90
  };
91
91
  const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisionEvents = false) => {
@@ -166,7 +166,7 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
166
166
  desc.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
167
167
  }
168
168
 
169
- const collider = world.createCollider(desc, rigidBody.handle);
169
+ const collider = world.createCollider(desc, rigidBody);
170
170
  colliders.push(collider);
171
171
  }
172
172
  });
@@ -234,6 +234,31 @@ const createRigidBodyApi = ref => {
234
234
  w
235
235
  } = ref.current().rotation();
236
236
  return new Quaternion(x, y, z, w);
237
+ },
238
+
239
+ setNextKinematicRotation({
240
+ x,
241
+ y,
242
+ z
243
+ }) {
244
+ ref.current().setNextKinematicRotation({
245
+ x,
246
+ y,
247
+ z,
248
+ w: 1
249
+ });
250
+ },
251
+
252
+ setNextKinematicTranslation({
253
+ x,
254
+ y,
255
+ z
256
+ }) {
257
+ ref.current().setNextKinematicTranslation({
258
+ x,
259
+ y,
260
+ z
261
+ });
237
262
  }
238
263
 
239
264
  };
@@ -255,10 +280,10 @@ const createWorldApi = ref => {
255
280
  getCollider: handle => ref.current().getCollider(handle),
256
281
  getRigidBody: handle => ref.current().getRigidBody(handle),
257
282
  createRigidBody: desc => ref.current().createRigidBody(desc),
258
- createCollider: (desc, rigidBodyHandle) => ref.current().createCollider(desc, rigidBodyHandle),
283
+ createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
259
284
  removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
260
285
  removeCollider: collider => ref.current().removeCollider(collider, true),
261
- createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB),
286
+ createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
262
287
  removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
263
288
  forEachCollider: callback => ref.current().forEachCollider(callback)
264
289
  };
@@ -305,7 +330,7 @@ const Physics = ({
305
330
  const [rigidBodyEvents] = useState(() => new Map());
306
331
  const [eventQueue] = useState(() => new EventQueue(false)); // Init world
307
332
 
308
- useLayoutEffect(() => {
333
+ useEffect(() => {
309
334
  const world = getWorldRef.current();
310
335
  return () => {
311
336
  if (world) {
@@ -374,10 +399,12 @@ const Physics = ({
374
399
  }); // Collision events
375
400
 
376
401
  eventQueue.drainCollisionEvents((handle1, handle2, started) => {
402
+ var _collider1$parent, _collider2$parent;
403
+
377
404
  const collider1 = world.getCollider(handle1);
378
405
  const collider2 = world.getCollider(handle2);
379
- const rigidBodyHandle1 = collider1.parent();
380
- const rigidBodyHandle2 = collider2.parent();
406
+ const rigidBodyHandle1 = (_collider1$parent = collider1.parent()) === null || _collider1$parent === void 0 ? void 0 : _collider1$parent.handle;
407
+ const rigidBodyHandle2 = (_collider2$parent = collider2.parent()) === null || _collider2$parent === void 0 ? void 0 : _collider2$parent.handle;
381
408
 
382
409
  if (!collider1 || !collider2 || !rigidBodyHandle1 || !rigidBodyHandle2) {
383
410
  return;
@@ -389,7 +416,7 @@ const Physics = ({
389
416
  const events2 = rigidBodyEvents.get(rigidBodyHandle2);
390
417
 
391
418
  if (started) {
392
- world.contactPair(handle1, handle2, (manifold, flipped) => {
419
+ world.contactPair(collider1, collider2, (manifold, flipped) => {
393
420
  var _events1$onCollisionE, _events2$onCollisionE;
394
421
 
395
422
  events1 === null || events1 === void 0 ? void 0 : (_events1$onCollisionE = events1.onCollisionEnter) === null || _events1$onCollisionE === void 0 ? void 0 : _events1$onCollisionE.call(events1, {
@@ -504,7 +531,7 @@ const useRigidBody = (options = {}) => {
504
531
  z: avz
505
532
  }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
506
533
  const rigidBody = world.createRigidBody(desc);
507
- rigidBodyRef.current = rigidBody;
534
+ rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
508
535
  }
509
536
 
510
537
  return rigidBodyRef.current;
@@ -539,6 +566,7 @@ const useRigidBody = (options = {}) => {
539
566
  y: worldPosition.y + y * scale.y,
540
567
  z: worldPosition.z + z * scale.z
541
568
  }, false);
569
+ console.log(rigidBody.isKinematic());
542
570
  const eulerAngles = new Euler(rx, ry, rz, 'XYZ');
543
571
  const rotation = new Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
544
572
  rigidBody.setRotation({
@@ -555,8 +583,9 @@ const useRigidBody = (options = {}) => {
555
583
  rigidBody.wakeUp();
556
584
  rigidBodyMeshes.set(rigidBody.handle, ref.current);
557
585
  return () => {
586
+ const actualBody = world.getRigidBody(rigidBody.handle);
587
+ world.removeRigidBody(actualBody);
558
588
  autoColliders.forEach(collider => world.removeCollider(collider));
559
- world.removeRigidBody(rigidBody);
560
589
  rigidBodyRef.current = undefined;
561
590
  rigidBodyMeshes.delete(rigidBody.handle);
562
591
  };
@@ -585,7 +614,7 @@ const useCollider = (body, options = {}) => {
585
614
  const objectRef = useRef();
586
615
  const getColliderRef = useRef(() => {
587
616
  if (!colliderRef.current) {
588
- colliderRef.current = createColliderFromOptions(options, world, body.handle);
617
+ colliderRef.current = createColliderFromOptions(options, world, world.getRigidBody(body.handle));
589
618
  }
590
619
 
591
620
  return colliderRef.current;
@@ -611,7 +640,7 @@ const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
611
640
  }
612
641
 
613
642
  const scale = ref.current.getWorldScale(new Vector3());
614
- const collider = createColliderFromOptions(colliderOptions, world, rigidBody.handle, scale);
643
+ const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
615
644
  return () => {
616
645
  world.removeCollider(collider);
617
646
  };
@@ -767,7 +796,6 @@ const useImpulseJoint = (body1, body2, params) => {
767
796
  const joint = getJointRef.current();
768
797
  return () => {
769
798
  if (joint) {
770
- console.log('remove joint', joint);
771
799
  world.removeImpulseJoint(joint);
772
800
  jointRef.current = undefined;
773
801
  }
@@ -918,7 +946,7 @@ const AnyCollider = _ref2 => {
918
946
  const ref = useRef(null);
919
947
  useEffect(() => {
920
948
  const scale = ref.current.getWorldScale(new Vector3());
921
- const collider = createColliderFromOptions(props, world, rigidBody.handle, scale, hasCollisionEvents);
949
+ const collider = createColliderFromOptions(props, world, world.getRigidBody(rigidBody.handle), scale, hasCollisionEvents);
922
950
  return () => {
923
951
  world.removeCollider(collider);
924
952
  };
@@ -975,20 +1003,20 @@ const ConvexHullCollider = props => {
975
1003
  };
976
1004
 
977
1005
  const geometryFromCollider = collider => {
978
- switch (collider.shapeType()) {
1006
+ switch (collider.shape.type) {
979
1007
  case ShapeType.Cuboid:
980
1008
  {
981
1009
  const {
982
1010
  x,
983
1011
  y,
984
1012
  z
985
- } = collider.halfExtents();
1013
+ } = collider.shape.halfExtents;
986
1014
  return new BoxBufferGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
987
1015
  }
988
1016
 
989
1017
  case ShapeType.Ball:
990
1018
  {
991
- const r = collider.radius();
1019
+ const r = collider.shape.radius;
992
1020
  return new SphereBufferGeometry(r + +0.01, 8, 8);
993
1021
  }
994
1022
 
@@ -996,10 +1024,12 @@ const geometryFromCollider = collider => {
996
1024
  {
997
1025
  var _g$index;
998
1026
 
999
- const v = collider.vertices();
1000
- const i = collider.indices();
1001
- const g = new BufferGeometry();
1002
- g.setAttribute("position", new BufferAttribute(v, 3));
1027
+ const v = collider.shape.vertices;
1028
+ const i = collider.shape.indices;
1029
+ const g = new BufferGeometry(); // Vertices are not always a float3darray (???), so we need to convert them
1030
+
1031
+ const safeVerts = Float32Array.from(v);
1032
+ g.setAttribute("position", new BufferAttribute(safeVerts, 3));
1003
1033
  (_g$index = g.index) === null || _g$index === void 0 ? void 0 : _g$index.set(i);
1004
1034
  g.setDrawRange(0, g.attributes.position.array.length / 3 - 1);
1005
1035
  return g;
@@ -1007,16 +1037,18 @@ const geometryFromCollider = collider => {
1007
1037
 
1008
1038
  case ShapeType.ConvexPolyhedron:
1009
1039
  {
1010
- const cv = collider.vertices();
1040
+ const cv = collider.shape.vertices; // Vertices are not always a float3darray (???), so we need to convert them
1041
+
1042
+ const safeVerts = Float32Array.from(cv);
1011
1043
  const cg = new BufferGeometry();
1012
- cg.setAttribute("position", new BufferAttribute(cv, 3));
1044
+ cg.setAttribute("position", new BufferAttribute(safeVerts, 3));
1013
1045
  return cg;
1014
1046
  }
1015
1047
 
1016
1048
  case ShapeType.Cylinder:
1017
1049
  {
1018
- const r = collider.radius();
1019
- const h = collider.halfHeight();
1050
+ const r = collider.shape.radius;
1051
+ const h = collider.shape.halfHeight;
1020
1052
  const g = new CylinderBufferGeometry(r, r, h);
1021
1053
  return g;
1022
1054
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
@@ -23,7 +23,7 @@
23
23
  "three": "^0.139.2"
24
24
  },
25
25
  "dependencies": {
26
- "@dimforge/rapier3d-compat": "0.8.0-alpha.2",
26
+ "@dimforge/rapier3d-compat": "0.8.1",
27
27
  "use-asset": "^1.0.4"
28
28
  },
29
29
  "repository": "https://github.com/pmndrs/react-three-rapier/tree/master/packages/react-three-rapier"
package/readme.md ADDED
@@ -0,0 +1,176 @@
1
+ <h1 align="center">@react-three/rapier 🗡</h1>
2
+
3
+ <p align="center">⚠️ Under heavy development. All APIs are subject to change. ⚠️</p>
4
+
5
+ ## Usage
6
+
7
+ ```tsx
8
+ import { Box } from "@react-three/drei";
9
+ import { Canvas } from "@react-three/fiber";
10
+ import { Physics, RigidBody } from "@react-three/rapier";
11
+
12
+ const App = () => {
13
+ return (
14
+ <Canvas>
15
+ <Physics>
16
+ <RigidBody>
17
+ <Box />
18
+ </RigidBody>
19
+ </Physics>
20
+ </Canvas>
21
+ );
22
+ };
23
+ ```
24
+
25
+ ## Automatic colliders
26
+
27
+ RigidBodies generate automatic colliders by default for all meshes that it contains. You can control the default collider by setting the `colliders` prop on a `<RigidBody />`, or change it globally by setting `colliders` on `<Physics />`. Setting `colliders={false}` disables auto-generation.
28
+
29
+ Supported values:
30
+
31
+ - `"cuboid"`, creates a CuboidCollider based on the bounding box of the mesh
32
+ - `"ball"`, creates a SphereCollider based on the bounding sphere of the mesh
33
+ - `"trimesh"`, creates a TrimeshCollider based on the mesh's geometry -- note trimeshes are massless by default (https://rapier.rs/docs/user_guides/javascript/common_mistakes#rigid-body-isnt-affected-by-gravity)
34
+ - `"hull"`, creates a ConvexHullCollider based on the mesh's geometry
35
+ - `false`, disables auto-generation
36
+
37
+ Generate ConvexHull colliders for all meshes in a RigidBody by default:
38
+
39
+ ```tsx
40
+ const Scene = () => (
41
+ <Physics colliders="hull">
42
+ <RigidBody>
43
+ <Box />
44
+ </RigidBody>
45
+ <RigidBody position={[0, 10, 0]}>
46
+ <Sphere />
47
+ </RigidBody>
48
+ </Physics>
49
+ );
50
+ ```
51
+
52
+ Turn off automatic collider generation globally, but apply auto generation locally:
53
+
54
+ ```tsx
55
+ const Scene = () => (
56
+ <Physics colliders={false}>
57
+ <RigidBody colliders="cuboid">
58
+ <Box />
59
+ </RigidBody>
60
+
61
+ <RigidBody position={[0, 10, 0]} colliders="ball">
62
+ <Sphere />
63
+ </RigidBody>
64
+
65
+ <RigidBody position={[0, 10, 0]}>
66
+ <Sphere />
67
+ <BallCollider args={0.5} />
68
+ <BallCollider args={0.5} position={[1, 0, 0]} />
69
+ </RigidBody>
70
+ </Physics>
71
+ );
72
+ ```
73
+
74
+ Objects work inside other transformed objects as well. Simulation runs in world space and is transformed to the objects local space, so that things act as you'd expect.
75
+
76
+ ```tsx
77
+ import { Box } from "@react-three/drei";
78
+ import { RigidBody, CuboidCollider } from "@react-three/rapier";
79
+
80
+ const Scene = () => {
81
+ return (
82
+ <group position={[2, 5, 0]} rotation={[0, 0.3, 2]}>
83
+ <RigidBody>
84
+ <Box />
85
+ <CuboidCollider args={[0.5, 0.5, 0.5]} />
86
+ </RigidBody>
87
+ </group>
88
+ );
89
+ };
90
+ ```
91
+
92
+ ## Debug
93
+
94
+ Use the Debug component to see live representations of all colliders in a scene.
95
+
96
+ > Note: Experimental. Not all shapes are supported. Unsupported shapes are always represented by cubes.
97
+
98
+ ```tsx
99
+ import { Box, Sphere } from "@react-three/drei";
100
+ import { RigidBody, Debug } from "@react-three/rapier";
101
+
102
+ const Scene = () => {
103
+ return (
104
+ <Physics>
105
+ <Debug />
106
+
107
+ <RigidBody>
108
+ <Box />
109
+ </RigidBody>
110
+ <RigidBody>
111
+ <Sphere />
112
+ </RigidBody>
113
+ </Physics>
114
+ );
115
+ };
116
+ ```
117
+
118
+ ## Events
119
+
120
+ You can subscribe collision and state events on the RigidBody.
121
+
122
+ ```tsx
123
+ const RigidBottle = () => {
124
+ const [isAsleep, setIsAsleep] = useState(false);
125
+
126
+ return (
127
+ <RigidBody
128
+ colliders="hull"
129
+ onSleep={() => setIsAsleep(true)}
130
+ onWake={() => setIsAsleep(false)}
131
+ onCollision={({manifold}) => {
132
+ console.log('Collision at world position ', manifold.solverContactPoint(0))
133
+ }}
134
+ >
135
+ <Sphere>
136
+ <meshPhysicalMaterial color={isAsleep ? 'white' : 'blue'}>
137
+ </Sphere>
138
+ </RigidBody>
139
+ )
140
+ }
141
+ ```
142
+
143
+ ## Hooks
144
+
145
+ You can also use hooks to generate rigid bodies and colliders, but it's not encouraged.
146
+
147
+ ```tsx
148
+ import { Box } from "@react-three/drei";
149
+ import { useCuboid } from "@react-three/rapier";
150
+
151
+ const RigidBox = () => {
152
+ // Generates a RigidBody and attaches a BoxCollider to it, returns a ref
153
+ const [box, rigidBody, collider] = useCuboid(
154
+ { position: [1, 1, 1] },
155
+ { args: [0.5, 0.5, 0.5] }
156
+ );
157
+
158
+ return <Box ref={box} />;
159
+ };
160
+ ```
161
+
162
+ ## Roadmap?
163
+
164
+ In order, but also not necessarily:
165
+
166
+ - [x] Draft of all base shapes
167
+ - [x] Draft of all base joints
168
+ - [x] Nested objects retain world transforms
169
+ - [x] Nested objects retain correct collider scale
170
+ - [x] Automatic colliders based on rigidbody children
171
+ - [ ] Translation and rotational constraints
172
+ - [x] Collision events
173
+ - [ ] InstancedMesh support
174
+ - [ ] Docs
175
+ - [ ] CodeSandbox examples
176
+ - [ ] Helpers, for things like Vehicle, Rope, Player, etc