@react-three/rapier 0.4.1 → 0.5.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 +28 -0
- package/dist/declarations/src/Physics.d.ts +23 -1
- package/dist/declarations/src/api.d.ts +17 -0
- package/dist/declarations/src/hooks.d.ts +3 -35
- package/dist/declarations/src/types.d.ts +30 -3
- package/dist/declarations/src/utils.d.ts +4 -3
- package/dist/react-three-rapier.cjs.dev.js +66 -178
- package/dist/react-three-rapier.cjs.prod.js +66 -178
- package/dist/react-three-rapier.esm.js +68 -164
- package/package.json +2 -2
- package/readme.md +17 -25
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,33 @@
|
|
1
1
|
# @react-three/rapier
|
2
2
|
|
3
|
+
## 0.5.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- a3be5f6: Remove hooks api in favor of better fleshed out components
|
8
|
+
|
9
|
+
### Patch Changes
|
10
|
+
|
11
|
+
- a3be5f6: Update types for Joints -- now only allow RefObjects of RigidBodyApi
|
12
|
+
- a3be5f6: Fix setKinematicRotation (convert Vector3 to Quaternion)
|
13
|
+
- a3be5f6: Update to @dimforge/rapier3d-compat@0.9.0
|
14
|
+
- a3be5f6: Allow setting the physics timeStep
|
15
|
+
- a3be5f6: Add rotational and transitional constraits to RigidBody
|
16
|
+
- a3be5f6: Allow updating the gravity at runtime
|
17
|
+
|
18
|
+
## 0.4.3
|
19
|
+
|
20
|
+
### Patch Changes
|
21
|
+
|
22
|
+
- f7a8a2d: Rigid body creation hooks should not use auto colliders
|
23
|
+
- 663eeb5: Fix default collider setting
|
24
|
+
|
25
|
+
## 0.4.2
|
26
|
+
|
27
|
+
### Patch Changes
|
28
|
+
|
29
|
+
- 387b32c: Add restitution and friction as props for auto-generated colliders on RigidBody
|
30
|
+
|
3
31
|
## 0.4.1
|
4
32
|
|
5
33
|
### Patch Changes
|
@@ -27,9 +27,31 @@ declare type EventMap = Map<ColliderHandle | RigidBodyHandle, {
|
|
27
27
|
}): void;
|
28
28
|
}>;
|
29
29
|
interface RapierWorldProps {
|
30
|
+
children: ReactNode;
|
31
|
+
/**
|
32
|
+
* Set the gravity of the physics world
|
33
|
+
* @defaultValue [0, -9.81, 0]
|
34
|
+
*/
|
30
35
|
gravity?: Vector3Array;
|
36
|
+
/**
|
37
|
+
* Set the base automatic colliders for this physics world
|
38
|
+
* All Meshes inside RigidBodies will generate a collider
|
39
|
+
* based on this value, if not overridden.
|
40
|
+
*/
|
31
41
|
colliders?: RigidBodyAutoCollider;
|
32
|
-
|
42
|
+
/**
|
43
|
+
* Set the timestep for the simulation.
|
44
|
+
* Setting this to a number (eg. 1/60) will run the
|
45
|
+
* simulation at that framerate.
|
46
|
+
*
|
47
|
+
* "vary" will run the simulation at a delta-value based
|
48
|
+
* on the users current framerate. This ensures simulations
|
49
|
+
* run at the same percieved speed at all framerates, but
|
50
|
+
* can also lead to instability.
|
51
|
+
*
|
52
|
+
* @defaultValue "vary"
|
53
|
+
*/
|
54
|
+
timeStep?: number | 'vary';
|
33
55
|
}
|
34
56
|
export declare const Physics: FC<RapierWorldProps>;
|
35
57
|
export {};
|
@@ -101,6 +101,22 @@ export interface RigidBodyApi {
|
|
101
101
|
* Resets to zero the user torques applied to this rigid-body.
|
102
102
|
*/
|
103
103
|
resetTorques(): void;
|
104
|
+
/**
|
105
|
+
* Locks or unlocks the ability of this rigid-body to rotate.
|
106
|
+
*/
|
107
|
+
lockRotations(locked: boolean): void;
|
108
|
+
/**
|
109
|
+
* Locks or unlocks the ability of this rigid-body to translate.
|
110
|
+
*/
|
111
|
+
lockTranslations(locked: boolean): void;
|
112
|
+
/**
|
113
|
+
* Locks or unlocks the ability of this rigid-body to rotate along individual coordinate axes.
|
114
|
+
*/
|
115
|
+
setEnabledRotations(x: boolean, y: boolean, z: boolean): void;
|
116
|
+
/**
|
117
|
+
* Locks or unlocks the ability of this rigid-body to translate along individual coordinate axes.
|
118
|
+
*/
|
119
|
+
setEnabledTranslations(x: boolean, y: boolean, z: boolean): void;
|
104
120
|
}
|
105
121
|
export declare const createRigidBodyApi: (ref: RefGetter<RigidBody>) => RigidBodyApi;
|
106
122
|
export declare const createColliderApi: (ref: RefGetter<Collider>) => {
|
@@ -118,6 +134,7 @@ export interface WorldApi {
|
|
118
134
|
createImpulseJoint(params: JointData, rigidBodyA: RigidBody, rigidBodyB: RigidBody): ImpulseJoint;
|
119
135
|
removeImpulseJoint(joint: ImpulseJoint): void;
|
120
136
|
forEachCollider(callback: (collider: Collider) => void): void;
|
137
|
+
setGravity(gravity: Vector3): void;
|
121
138
|
}
|
122
139
|
export declare const createWorldApi: (ref: RefGetter<World>) => WorldApi;
|
123
140
|
export declare const createJointApi: (ref: RefGetter<ImpulseJoint>) => {
|
@@ -1,47 +1,15 @@
|
|
1
1
|
import React, { MutableRefObject } from "react";
|
2
2
|
import { RapierContext } from "./Physics";
|
3
|
-
import {
|
3
|
+
import { Object3D } from "three";
|
4
4
|
import type Rapier from "@dimforge/rapier3d-compat";
|
5
5
|
export declare const useRapier: () => RapierContext;
|
6
|
-
import {
|
7
|
-
import { RoundCone } from "@dimforge/rapier3d-compat";
|
6
|
+
import { UseRigidBodyOptions, UseImpulseJoint, SphericalJointParams, FixedJointParams, PrismaticJointParams, RevoluteJointParams, UseColliderOptions, RigidBodyApi, RigidBodyApiRef } from "./types";
|
8
7
|
export declare const useRigidBody: <O extends Object3D<import("three").Event>>(options?: UseRigidBodyOptions) => [React.MutableRefObject<O>, import("./api").RigidBodyApi];
|
9
8
|
export declare const useCollider: <A>(body: RigidBodyApi, options?: UseColliderOptions<A>) => (React.MutableRefObject<Object3D<import("three").Event> | undefined> | {
|
10
9
|
raw: () => Rapier.Collider | undefined;
|
11
10
|
readonly handle: number;
|
12
11
|
})[];
|
13
|
-
export declare const
|
14
|
-
export declare const useCuboid: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<CuboidArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
15
|
-
export declare const useBall: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<BallArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
16
|
-
export declare const useCapsule: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<CapsuleArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
17
|
-
export declare const useHeightfield: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<HeightfieldArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
18
|
-
/**
|
19
|
-
* Create a trimesh collider and rigid body.
|
20
|
-
* Note that Trimeshes don't have mass unless provided.
|
21
|
-
* See https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
|
22
|
-
* for available properties.
|
23
|
-
*/
|
24
|
-
export declare const useTrimesh: {
|
25
|
-
<T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<TrimeshArgs>): [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
26
|
-
fromMesh<T_1 extends Object3D<import("three").Event>>(mesh: Mesh, rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<TrimeshArgs>): [ref: React.MutableRefObject<T_1>, rigidBody: import("./api").RigidBodyApi];
|
27
|
-
};
|
28
|
-
export declare const usePolyline: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<PolylineArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
29
|
-
export declare const useRoundCuboid: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundCuboidArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
30
|
-
export declare const useCylinder: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<CylinderArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
31
|
-
export declare const useRoundCylinder: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundCylinderArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
32
|
-
export declare const useCone: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<ConeArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
33
|
-
export declare const useRoundCone: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundCone>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
34
|
-
export declare const useConvexHull: {
|
35
|
-
<T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<ConvexHullArgs>): [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
36
|
-
fromMesh<T_1 extends Object3D<import("three").Event>>(mesh: Mesh, rigidBodyOptions?: UseBodyOptions, colliderOptions?: Omit<UseColliderOptions<ConvexHullArgs>, "colliderArgs">): [ref: React.MutableRefObject<T_1>, rigidBody: import("./api").RigidBodyApi];
|
37
|
-
};
|
38
|
-
export declare const useRoundConvexHull: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundConvexHullArgs>) => [ref: React.MutableRefObject<T>, rigidBody: import("./api").RigidBodyApi];
|
39
|
-
export declare const useConvexMesh: {
|
40
|
-
<T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<ConvexMeshArgs>): [ref: React.MutableRefObject<Object3D<import("three").Event>>, rigidBody: import("./api").RigidBodyApi];
|
41
|
-
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: import("./api").RigidBodyApi];
|
42
|
-
};
|
43
|
-
export declare const useRoundConvexMesh: <T extends Object3D<import("three").Event>>(rigidBodyOptions?: UseBodyOptions, colliderOptions?: UseColliderOptions<RoundConvexMeshArgs>) => [ref: React.MutableRefObject<Object3D<import("three").Event>>, rigidBody: import("./api").RigidBodyApi];
|
44
|
-
export declare const useImpulseJoint: <T extends Rapier.ImpulseJoint>(body1: MutableRefObject<RapierRigidBody | undefined | null> | RigidBodyApi, body2: MutableRefObject<RapierRigidBody | undefined | null> | RigidBodyApi, params: Rapier.JointData) => {
|
12
|
+
export declare const useImpulseJoint: <T extends Rapier.ImpulseJoint>(body1: RigidBodyApiRef, body2: RigidBodyApiRef, params: Rapier.JointData) => {
|
45
13
|
raw: () => Rapier.ImpulseJoint | undefined;
|
46
14
|
readonly handle: number;
|
47
15
|
configureMotorPosition: (targetPos: number, stiffness: number, damping: number) => void;
|
@@ -64,6 +64,7 @@ export declare type UseBodyOptions = Omit<UseRigidBodyOptions, "shape">;
|
|
64
64
|
export declare type RigidBodyTypeString = "fixed" | "dynamic" | "kinematicPosition" | "kinematicVelocity";
|
65
65
|
export declare type RigidBodyShape = "cuboid" | "trimesh" | "ball" | "capsule" | "convexHull" | "heightfield" | "polyline" | "roundCuboid" | "cylinder" | "roundCylinder" | "cone" | "roundCone" | "convexMesh" | "roundConvexHull" | "roundConvexMesh";
|
66
66
|
export declare type Vector3Array = [x: number, y: number, z: number];
|
67
|
+
export declare type Boolean3Array = [x: boolean, y: boolean, z: boolean];
|
67
68
|
export interface UseColliderOptions<A> {
|
68
69
|
/**
|
69
70
|
* The shape of your collider
|
@@ -162,6 +163,16 @@ export interface UseRigidBodyOptions {
|
|
162
163
|
* Setting this to false will disable automatic colliders.
|
163
164
|
*/
|
164
165
|
colliders?: RigidBodyAutoCollider | false;
|
166
|
+
/**
|
167
|
+
* Set the friction of auto-generated colliders.
|
168
|
+
* This does not affect any non-automatic child collider-components.
|
169
|
+
*/
|
170
|
+
friction?: number;
|
171
|
+
/**
|
172
|
+
* Set the restitution (bounciness) of auto-generated colliders.
|
173
|
+
* This does not affect any non-automatic child collider-components.
|
174
|
+
*/
|
175
|
+
restitution?: number;
|
165
176
|
/**
|
166
177
|
* Callback when this rigidbody collides with another rigidbody
|
167
178
|
*/
|
@@ -178,6 +189,22 @@ export interface UseRigidBodyOptions {
|
|
178
189
|
}): void;
|
179
190
|
onSleep?(): void;
|
180
191
|
onWake?(): void;
|
192
|
+
/**
|
193
|
+
* Locks all rotations that would have resulted from forces on the created rigid-body.
|
194
|
+
*/
|
195
|
+
lockRotations?: boolean;
|
196
|
+
/**
|
197
|
+
* Locks all translations that would have resulted from forces on the created rigid-body.
|
198
|
+
*/
|
199
|
+
lockTranslations?: boolean;
|
200
|
+
/**
|
201
|
+
* Allow rotation of this rigid-body only along specific axes.
|
202
|
+
*/
|
203
|
+
enabledRotations?: Boolean3Array;
|
204
|
+
/**
|
205
|
+
* Allow rotation of this rigid-body only along specific axes.
|
206
|
+
*/
|
207
|
+
enabledTranslations?: Boolean3Array;
|
181
208
|
}
|
182
209
|
export declare type SphericalJointParams = [
|
183
210
|
body1Anchor: Vector3Array,
|
@@ -197,12 +224,12 @@ export declare type PrismaticJointParams = [
|
|
197
224
|
];
|
198
225
|
export declare type RevoluteJointParams = [
|
199
226
|
body1Anchor: Vector3Array,
|
200
|
-
body1LocalFrame: Vector3Array,
|
201
227
|
body2Anchor: Vector3Array,
|
202
|
-
|
228
|
+
axis: Vector3Array
|
203
229
|
];
|
230
|
+
export declare type RigidBodyApiRef = MutableRefObject<undefined | null | RigidBodyApi>;
|
204
231
|
export interface UseImpulseJoint<P> {
|
205
|
-
(body1:
|
232
|
+
(body1: RigidBodyApiRef, body2: RigidBodyApiRef, params: P): JointApi;
|
206
233
|
}
|
207
234
|
export declare type RigidBodyApi = ReturnType<typeof createRigidBodyApi>;
|
208
235
|
export declare type ColliderApi = ReturnType<typeof createColliderApi>;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Collider, RigidBody } from "@dimforge/rapier3d-compat";
|
2
|
-
import { Object3D, Vector3 } from "three";
|
3
|
-
import {
|
2
|
+
import { Object3D, Quaternion, Vector3 } from "three";
|
3
|
+
import { RigidBodyShape, RigidBodyTypeString, UseColliderOptions, UseRigidBodyOptions, Vector3Array, WorldApi } from "./types";
|
4
4
|
export declare const vectorArrayToObject: (arr: Vector3Array) => {
|
5
5
|
x: number;
|
6
6
|
y: number;
|
@@ -13,5 +13,6 @@ export declare const createColliderFromOptions: <A>(options: UseColliderOptions<
|
|
13
13
|
y: number;
|
14
14
|
z: number;
|
15
15
|
}, hasCollisionEvents?: boolean) => Collider;
|
16
|
-
export declare const createCollidersFromChildren: (object: Object3D, rigidBody: RigidBody,
|
16
|
+
export declare const createCollidersFromChildren: (object: Object3D, rigidBody: RigidBody, options: UseRigidBodyOptions, world: WorldApi) => Collider[];
|
17
17
|
export declare const scaleVertices: (vertices: ArrayLike<number>, scale: Vector3) => number[];
|
18
|
+
export declare const vector3ToQuaternion: (v: Vector3) => Quaternion;
|
@@ -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,
|
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,11 +187,9 @@ const createCollidersFromChildren = (object, rigidBody, type, world, hasCollisio
|
|
186
187
|
z: rz,
|
187
188
|
w: rw
|
188
189
|
});
|
189
|
-
|
190
|
-
if (
|
191
|
-
|
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
|
}
|
@@ -208,6 +207,11 @@ const scaleVertices = (vertices, scale) => {
|
|
208
207
|
|
209
208
|
return scaledVerts;
|
210
209
|
};
|
210
|
+
const quaternion = new three.Quaternion();
|
211
|
+
const euler = new three.Euler();
|
212
|
+
const vector3ToQuaternion = v => {
|
213
|
+
return quaternion.setFromEuler(euler.setFromVector3(v));
|
214
|
+
};
|
211
215
|
|
212
216
|
function _defineProperty(obj, key, value) {
|
213
217
|
if (key in obj) {
|
@@ -319,12 +323,26 @@ const createRigidBodyApi = ref => {
|
|
319
323
|
},
|
320
324
|
|
321
325
|
setAngvel: velocity => ref.current().setAngvel(velocity, true),
|
322
|
-
setNextKinematicRotation:
|
323
|
-
|
324
|
-
|
326
|
+
setNextKinematicRotation: ({
|
327
|
+
x,
|
328
|
+
y,
|
329
|
+
z
|
330
|
+
}) => {
|
331
|
+
const q = vector3ToQuaternion(new three.Vector3(x, y, z));
|
332
|
+
ref.current().setNextKinematicRotation({
|
333
|
+
x: q.x,
|
334
|
+
y: q.y,
|
335
|
+
z: q.z,
|
336
|
+
w: q.w
|
337
|
+
});
|
338
|
+
},
|
325
339
|
setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
|
326
340
|
resetForces: () => ref.current().resetForces(true),
|
327
|
-
resetTorques: () => ref.current().resetTorques(true)
|
341
|
+
resetTorques: () => ref.current().resetTorques(true),
|
342
|
+
lockRotations: locked => ref.current().lockRotations(locked, true),
|
343
|
+
lockTranslations: locked => ref.current().lockTranslations(locked, true),
|
344
|
+
setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
|
345
|
+
setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
|
328
346
|
};
|
329
347
|
}; // TODO: Flesh this out
|
330
348
|
|
@@ -349,7 +367,16 @@ const createWorldApi = ref => {
|
|
349
367
|
removeCollider: collider => ref.current().removeCollider(collider, true),
|
350
368
|
createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
|
351
369
|
removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
|
352
|
-
forEachCollider: callback => ref.current().forEachCollider(callback)
|
370
|
+
forEachCollider: callback => ref.current().forEachCollider(callback),
|
371
|
+
setGravity: ({
|
372
|
+
x,
|
373
|
+
y,
|
374
|
+
z
|
375
|
+
}) => ref.current().gravity = {
|
376
|
+
x,
|
377
|
+
y,
|
378
|
+
z
|
379
|
+
}
|
353
380
|
};
|
354
381
|
}; // TODO: Broken currently, waiting for Rapier3D to fix
|
355
382
|
|
@@ -377,7 +404,8 @@ const importRapier = async () => {
|
|
377
404
|
const Physics = ({
|
378
405
|
colliders: _colliders = 'cuboid',
|
379
406
|
gravity: _gravity = [0, -9.81, 0],
|
380
|
-
children
|
407
|
+
children,
|
408
|
+
timeStep: _timeStep = 'vary'
|
381
409
|
}) => {
|
382
410
|
const rapier = useAsset.useAsset(importRapier);
|
383
411
|
const worldRef = React.useRef();
|
@@ -402,7 +430,15 @@ const Physics = ({
|
|
402
430
|
worldRef.current = undefined;
|
403
431
|
}
|
404
432
|
};
|
405
|
-
}, []);
|
433
|
+
}, []); // Update gravity
|
434
|
+
|
435
|
+
React.useEffect(() => {
|
436
|
+
const world = worldRef.current;
|
437
|
+
|
438
|
+
if (world) {
|
439
|
+
world.gravity = vectorArrayToObject(_gravity);
|
440
|
+
}
|
441
|
+
}, [_gravity]);
|
406
442
|
const time = React.useRef(performance.now());
|
407
443
|
fiber.useFrame(context => {
|
408
444
|
const world = worldRef.current;
|
@@ -411,7 +447,13 @@ const Physics = ({
|
|
411
447
|
|
412
448
|
const now = performance.now();
|
413
449
|
const delta = Math.min(100, now - time.current);
|
414
|
-
|
450
|
+
|
451
|
+
if (_timeStep === 'vary') {
|
452
|
+
world.timestep = delta / 1000;
|
453
|
+
} else {
|
454
|
+
world.timestep = _timeStep;
|
455
|
+
}
|
456
|
+
|
415
457
|
world.step(eventQueue); // Update meshes
|
416
458
|
|
417
459
|
rigidBodyMeshes.forEach((mesh, handle) => {
|
@@ -540,7 +582,7 @@ const useRigidBody = (options = {}) => {
|
|
540
582
|
const rigidBodyRef = React.useRef();
|
541
583
|
const getRigidBodyRef = React.useRef(() => {
|
542
584
|
if (!rigidBodyRef.current) {
|
543
|
-
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd;
|
585
|
+
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
|
544
586
|
|
545
587
|
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
546
588
|
const [lvx, lvy, lvz] = (_options$linearVeloci = options === null || options === void 0 ? void 0 : options.linearVelocity) !== null && _options$linearVeloci !== void 0 ? _options$linearVeloci : [0, 0, 0];
|
@@ -548,11 +590,15 @@ const useRigidBody = (options = {}) => {
|
|
548
590
|
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
549
591
|
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
550
592
|
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
593
|
+
const [erx, ery, erz] = (_options$enabledRotat = options === null || options === void 0 ? void 0 : options.enabledRotations) !== null && _options$enabledRotat !== void 0 ? _options$enabledRotat : [true, true, true];
|
594
|
+
const [etx, ety, etz] = (_options$enabledTrans = options === null || options === void 0 ? void 0 : options.enabledTranslations) !== null && _options$enabledTrans !== void 0 ? _options$enabledTrans : [true, true, true];
|
551
595
|
const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
552
596
|
x: avx,
|
553
597
|
y: avy,
|
554
598
|
z: avz
|
555
|
-
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
|
599
|
+
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
|
600
|
+
if (options.lockRotations) desc.lockRotations();
|
601
|
+
if (options.lockTranslations) desc.lockTranslations();
|
556
602
|
const rigidBody = world.createRigidBody(desc);
|
557
603
|
rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
|
558
604
|
}
|
@@ -600,9 +646,9 @@ const useRigidBody = (options = {}) => {
|
|
600
646
|
rigidBody.resetForces(false);
|
601
647
|
rigidBody.resetTorques(false);
|
602
648
|
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;
|
603
|
-
const
|
604
|
-
|
605
|
-
|
649
|
+
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
|
650
|
+
colliders: colliderSetting
|
651
|
+
}), world) : [];
|
606
652
|
rigidBodyMeshes.set(rigidBody.handle, ref.current);
|
607
653
|
return () => {
|
608
654
|
world.removeRigidBody(rigidBody);
|
@@ -649,142 +695,6 @@ const useCollider = (body, options = {}) => {
|
|
649
695
|
}, []);
|
650
696
|
const api = React.useMemo(() => createColliderApi(getColliderRef), []);
|
651
697
|
return [objectRef, api];
|
652
|
-
};
|
653
|
-
const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
|
654
|
-
const {
|
655
|
-
world
|
656
|
-
} = useRapier();
|
657
|
-
const [ref, rigidBody] = useRigidBody(rigidBodyOptions);
|
658
|
-
React.useEffect(() => {
|
659
|
-
if (!colliderOptions) {
|
660
|
-
return;
|
661
|
-
}
|
662
|
-
|
663
|
-
const scale = ref.current.getWorldScale(new three.Vector3());
|
664
|
-
const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
|
665
|
-
return () => {
|
666
|
-
world.removeCollider(collider);
|
667
|
-
};
|
668
|
-
}, []);
|
669
|
-
return [ref, rigidBody];
|
670
|
-
};
|
671
|
-
const useCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
672
|
-
var _colliderOptions$args;
|
673
|
-
|
674
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
675
|
-
shape: "cuboid",
|
676
|
-
args: (_colliderOptions$args = colliderOptions.args) !== null && _colliderOptions$args !== void 0 ? _colliderOptions$args : [0.5, 0.5, 0.5]
|
677
|
-
}, colliderOptions));
|
678
|
-
};
|
679
|
-
const useBall = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
680
|
-
var _colliderOptions$args2;
|
681
|
-
|
682
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
683
|
-
shape: "ball",
|
684
|
-
args: (_colliderOptions$args2 = colliderOptions.args) !== null && _colliderOptions$args2 !== void 0 ? _colliderOptions$args2 : [0.5]
|
685
|
-
}, colliderOptions));
|
686
|
-
};
|
687
|
-
const useCapsule = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
688
|
-
var _colliderOptions$args3;
|
689
|
-
|
690
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
691
|
-
shape: "capsule",
|
692
|
-
args: (_colliderOptions$args3 = colliderOptions.args) !== null && _colliderOptions$args3 !== void 0 ? _colliderOptions$args3 : [0.5, 0.5]
|
693
|
-
}, colliderOptions));
|
694
|
-
};
|
695
|
-
const useHeightfield = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
696
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
697
|
-
shape: "heightfield"
|
698
|
-
}, colliderOptions));
|
699
|
-
};
|
700
|
-
/**
|
701
|
-
* Create a trimesh collider and rigid body.
|
702
|
-
* Note that Trimeshes don't have mass unless provided.
|
703
|
-
* See https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
|
704
|
-
* for available properties.
|
705
|
-
*/
|
706
|
-
|
707
|
-
const useTrimesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
708
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
709
|
-
shape: "trimesh"
|
710
|
-
}, colliderOptions));
|
711
|
-
};
|
712
|
-
|
713
|
-
useTrimesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
714
|
-
var _mesh$geometry, _mesh$geometry$index;
|
715
|
-
|
716
|
-
return useTrimesh(rigidBodyOptions, _objectSpread2({
|
717
|
-
args: [mesh.geometry.attributes.position.array, ((_mesh$geometry = mesh.geometry) === null || _mesh$geometry === void 0 ? void 0 : (_mesh$geometry$index = _mesh$geometry.index) === null || _mesh$geometry$index === void 0 ? void 0 : _mesh$geometry$index.array) || []]
|
718
|
-
}, colliderOptions));
|
719
|
-
};
|
720
|
-
|
721
|
-
const usePolyline = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
722
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
723
|
-
shape: "polyline"
|
724
|
-
}, colliderOptions));
|
725
|
-
};
|
726
|
-
const useRoundCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
727
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
728
|
-
shape: "roundCuboid"
|
729
|
-
}, colliderOptions));
|
730
|
-
};
|
731
|
-
const useCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
732
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
733
|
-
shape: "cylinder"
|
734
|
-
}, colliderOptions));
|
735
|
-
};
|
736
|
-
const useRoundCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
737
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
738
|
-
shape: "roundCylinder"
|
739
|
-
}, colliderOptions));
|
740
|
-
};
|
741
|
-
const useCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
742
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
743
|
-
shape: "cone"
|
744
|
-
}, colliderOptions));
|
745
|
-
};
|
746
|
-
const useRoundCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
747
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
748
|
-
shape: "roundCone"
|
749
|
-
}, colliderOptions));
|
750
|
-
};
|
751
|
-
const useConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
752
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
753
|
-
shape: "convexHull"
|
754
|
-
}, colliderOptions));
|
755
|
-
};
|
756
|
-
|
757
|
-
useConvexHull.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
758
|
-
var _mesh$geometry2, _mesh$geometry2$attri, _mesh$geometry2$attri2;
|
759
|
-
|
760
|
-
return useConvexHull(rigidBodyOptions, _objectSpread2({
|
761
|
-
args: [(mesh === null || mesh === void 0 ? void 0 : (_mesh$geometry2 = mesh.geometry) === null || _mesh$geometry2 === void 0 ? void 0 : (_mesh$geometry2$attri = _mesh$geometry2.attributes) === null || _mesh$geometry2$attri === void 0 ? void 0 : (_mesh$geometry2$attri2 = _mesh$geometry2$attri.position) === null || _mesh$geometry2$attri2 === void 0 ? void 0 : _mesh$geometry2$attri2.array) || []]
|
762
|
-
}, colliderOptions));
|
763
|
-
};
|
764
|
-
|
765
|
-
const useRoundConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
766
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
767
|
-
shape: "roundConvexHull"
|
768
|
-
}, colliderOptions));
|
769
|
-
};
|
770
|
-
const useConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
771
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
772
|
-
shape: "convexMesh"
|
773
|
-
}, colliderOptions));
|
774
|
-
};
|
775
|
-
|
776
|
-
useConvexMesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
777
|
-
var _mesh$geometry3, _mesh$geometry3$attri, _mesh$geometry3$attri2, _mesh$geometry4, _mesh$geometry4$index;
|
778
|
-
|
779
|
-
return useConvexMesh(rigidBodyOptions, _objectSpread2({
|
780
|
-
args: [mesh === null || mesh === void 0 ? void 0 : (_mesh$geometry3 = mesh.geometry) === null || _mesh$geometry3 === void 0 ? void 0 : (_mesh$geometry3$attri = _mesh$geometry3.attributes) === null || _mesh$geometry3$attri === void 0 ? void 0 : (_mesh$geometry3$attri2 = _mesh$geometry3$attri.position) === null || _mesh$geometry3$attri2 === void 0 ? void 0 : _mesh$geometry3$attri2.array, ((_mesh$geometry4 = mesh.geometry) === null || _mesh$geometry4 === void 0 ? void 0 : (_mesh$geometry4$index = _mesh$geometry4.index) === null || _mesh$geometry4$index === void 0 ? void 0 : _mesh$geometry4$index.array) || []]
|
781
|
-
}, colliderOptions));
|
782
|
-
};
|
783
|
-
|
784
|
-
const useRoundConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
785
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
786
|
-
shape: "convexMesh"
|
787
|
-
}, colliderOptions));
|
788
698
|
}; // Joints
|
789
699
|
|
790
700
|
const useImpulseJoint = (body1, body2, params) => {
|
@@ -797,12 +707,6 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
797
707
|
let rb1;
|
798
708
|
let rb2;
|
799
709
|
|
800
|
-
if ('handle' in body1 && 'handle' in body2) {
|
801
|
-
rb1 = world.getRigidBody(body1.handle);
|
802
|
-
rb2 = world.getRigidBody(body2.handle);
|
803
|
-
jointRef.current = world.createImpulseJoint(params, rb1, rb2);
|
804
|
-
}
|
805
|
-
|
806
710
|
if ('current' in body1 && body1.current && 'current' in body2 && body2.current) {
|
807
711
|
rb1 = world.getRigidBody(body1.current.handle);
|
808
712
|
rb2 = world.getRigidBody(body2.current.handle);
|
@@ -1162,27 +1066,11 @@ exports.Physics = Physics;
|
|
1162
1066
|
exports.RigidBody = RigidBody;
|
1163
1067
|
exports.RoundCuboidCollider = RoundCuboidCollider;
|
1164
1068
|
exports.TrimeshCollider = TrimeshCollider;
|
1165
|
-
exports.useBall = useBall;
|
1166
|
-
exports.useCapsule = useCapsule;
|
1167
1069
|
exports.useCollider = useCollider;
|
1168
|
-
exports.useCone = useCone;
|
1169
|
-
exports.useConvexHull = useConvexHull;
|
1170
|
-
exports.useConvexMesh = useConvexMesh;
|
1171
|
-
exports.useCuboid = useCuboid;
|
1172
|
-
exports.useCylinder = useCylinder;
|
1173
1070
|
exports.useFixedJoint = useFixedJoint;
|
1174
|
-
exports.useHeightfield = useHeightfield;
|
1175
1071
|
exports.useImpulseJoint = useImpulseJoint;
|
1176
|
-
exports.usePolyline = usePolyline;
|
1177
1072
|
exports.usePrismaticJoint = usePrismaticJoint;
|
1178
1073
|
exports.useRapier = useRapier;
|
1179
1074
|
exports.useRevoluteJoint = useRevoluteJoint;
|
1180
1075
|
exports.useRigidBody = useRigidBody;
|
1181
|
-
exports.useRigidBodyWithCollider = useRigidBodyWithCollider;
|
1182
|
-
exports.useRoundCone = useRoundCone;
|
1183
|
-
exports.useRoundConvexHull = useRoundConvexHull;
|
1184
|
-
exports.useRoundConvexMesh = useRoundConvexMesh;
|
1185
|
-
exports.useRoundCuboid = useRoundCuboid;
|
1186
|
-
exports.useRoundCylinder = useRoundCylinder;
|
1187
1076
|
exports.useSphericalJoint = useSphericalJoint;
|
1188
|
-
exports.useTrimesh = useTrimesh;
|