@react-three/rapier 0.9.0 → 0.10.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/dist/declarations/src/Attractor.d.ts +51 -0
- package/dist/declarations/src/MeshCollider.d.ts +4 -1
- package/dist/declarations/src/Physics.d.ts +33 -4
- package/dist/declarations/src/index.d.ts +2 -1
- package/dist/declarations/src/{bitmasks.d.ts → interaction-groups.d.ts} +0 -0
- package/dist/declarations/src/types.d.ts +20 -6
- package/dist/react-three-rapier.cjs.dev.js +1055 -917
- package/dist/react-three-rapier.cjs.prod.js +1055 -917
- package/dist/react-three-rapier.esm.js +1059 -922
- package/package.json +1 -1
- package/readme.md +112 -47
package/package.json
CHANGED
package/readme.md
CHANGED
@@ -11,12 +11,12 @@
|
|
11
11
|
|
12
12
|
For contributions, please read the [contributing guide](https://github.com/pmndrs/react-three-rapier/blob/main/packages/react-three-rapier/CONTRIBUTING.md).
|
13
13
|
|
14
|
-
## Usage
|
14
|
+
## Basic Usage
|
15
15
|
|
16
16
|
```tsx
|
17
17
|
import { Box, Torus } from "@react-three/drei";
|
18
18
|
import { Canvas } from "@react-three/fiber";
|
19
|
-
import { Physics, RigidBody } from "@react-three/rapier";
|
19
|
+
import { Physics, RigidBody, Debug } from "@react-three/rapier";
|
20
20
|
|
21
21
|
const App = () => {
|
22
22
|
return (
|
@@ -27,9 +27,9 @@ const App = () => {
|
|
27
27
|
<Torus />
|
28
28
|
</RigidBody>
|
29
29
|
|
30
|
-
<
|
31
|
-
|
32
|
-
|
30
|
+
<CuboidCollider position={[0, -2, 0]} args={[20, .5, 20]}>
|
31
|
+
|
32
|
+
<Debug />
|
33
33
|
</Physics>
|
34
34
|
</Suspense>
|
35
35
|
</Canvas>
|
@@ -37,6 +37,23 @@ const App = () => {
|
|
37
37
|
};
|
38
38
|
```
|
39
39
|
|
40
|
+
---
|
41
|
+
|
42
|
+
## Readme Topics
|
43
|
+
|
44
|
+
- [Automatic Colliders](#automatic-colliders)
|
45
|
+
- [Instanced Meshes](#instanced-meshes)
|
46
|
+
- [Debug](#debug)
|
47
|
+
- [Collision Events](#collision-events)
|
48
|
+
- [Collision Groups](#configuring-collision-and-solver-groups)
|
49
|
+
- [Contact Force Events](#contact-force-events)
|
50
|
+
- [Sensors](#sensors)
|
51
|
+
- [Attractors](#attractors)
|
52
|
+
- [The timeStep](#configuring-time-step-size)
|
53
|
+
- [Manual stepping](#manual-stepping)
|
54
|
+
|
55
|
+
---
|
56
|
+
|
40
57
|
## Automatic colliders
|
41
58
|
|
42
59
|
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.
|
@@ -138,8 +155,6 @@ Instanced meshes can also be used and have automatic colliders generated from th
|
|
138
155
|
|
139
156
|
By wrapping the `InstancedMesh` in `<InstancedRigidBodies />`, each instance will be attached to an individual `RigidBody`.
|
140
157
|
|
141
|
-
> Note: Custom colliders (compound shapes) for InstancedMesh is currently not supported
|
142
|
-
|
143
158
|
```tsx
|
144
159
|
import { InstancedRigidBodies } from "@react-three/rapier";
|
145
160
|
|
@@ -225,21 +240,35 @@ You can subscribe to collision and state events on a RigidBody:
|
|
225
240
|
const RigidBottle = () => {
|
226
241
|
const [isAsleep, setIsAsleep] = useState(false);
|
227
242
|
|
228
|
-
return (
|
243
|
+
return (
|
229
244
|
<RigidBody
|
230
245
|
colliders="hull"
|
231
246
|
onSleep={() => setIsAsleep(true)}
|
232
247
|
onWake={() => setIsAsleep(false)}
|
233
|
-
|
234
|
-
|
248
|
+
name="Bally McBallFace"
|
249
|
+
onCollisionEnter={({ manifold, target, other }) => {
|
250
|
+
console.log(
|
251
|
+
"Collision at world position ",
|
252
|
+
manifold.solverContactPoint(0)
|
253
|
+
);
|
254
|
+
|
255
|
+
if (other.rigidBodyObject) {
|
256
|
+
console.log(
|
257
|
+
// this rigid body's Object3D
|
258
|
+
target.rigidBodyObject.name,
|
259
|
+
" collided with ",
|
260
|
+
// the other rigid body's Object3D
|
261
|
+
other.rigidBodyObject.name
|
262
|
+
);
|
263
|
+
}
|
235
264
|
}}
|
236
265
|
>
|
237
266
|
<Sphere>
|
238
|
-
<meshPhysicalMaterial color={isAsleep ?
|
267
|
+
<meshPhysicalMaterial color={isAsleep ? "white" : "blue"} />
|
239
268
|
</Sphere>
|
240
269
|
</RigidBody>
|
241
|
-
)
|
242
|
-
}
|
270
|
+
);
|
271
|
+
};
|
243
272
|
```
|
244
273
|
|
245
274
|
You may also subscribe to collision events on individual Colliders:
|
@@ -258,14 +287,21 @@ You may also subscribe to collision events on individual Colliders:
|
|
258
287
|
The `payload` object for all collision callbacks contains the following properties:
|
259
288
|
|
260
289
|
- `target`
|
261
|
-
|
262
|
-
- `
|
263
|
-
|
264
|
-
- `manifold` (
|
290
|
+
`CollisionTarget` of the object firing the event.
|
291
|
+
- `other`
|
292
|
+
`CollisionTarget` of the other object involved in the event.
|
293
|
+
- `manifold` (onCollisionEnter only)
|
265
294
|
The [contact manifold](https://rapier.rs/javascript3d/classes/TempContactManifold.html) generated by the collision event.
|
266
|
-
- `flipped` (
|
295
|
+
- `flipped` (onCollisionEnter only)
|
267
296
|
`true` if the data in the `manifold` [is flipped](https://rapier.rs/javascript3d/classes/World.html#contactPair).
|
268
297
|
|
298
|
+
A `CollisionTarget` is an object containing references to objects involved in a collision event. It has the following properties:
|
299
|
+
|
300
|
+
- `rigidBody` (if exists): `Rapier.RigidBody`
|
301
|
+
- `rigidBodyObject` (if exists): `Three.Object3D`
|
302
|
+
- `collider`: `Rapier.Collider`
|
303
|
+
- `colliderObject`: `Three.Object3D`
|
304
|
+
|
269
305
|
### Configuring collision and solver groups
|
270
306
|
|
271
307
|
Both `<RigidBody>` as well as all collider components allow you to configure `collisionsGroups` and `solverGroups` properties that configures which groups the colliders are in, and what other groups they should interact with in potential collision and solving events (you will find more details on this in the [Rapier documentation](https://rapier.rs/docs/user_guides/javascript/colliders/#collision-groups-and-solver-groups).)
|
@@ -314,14 +350,18 @@ Contact force events are triggered on `<RigidBody>` and any collider components
|
|
314
350
|
|
315
351
|
The payload for the contact force event contains the following properties:
|
316
352
|
|
317
|
-
`
|
318
|
-
`
|
319
|
-
|
320
|
-
`
|
321
|
-
`totalForce`
|
322
|
-
|
323
|
-
`
|
324
|
-
|
353
|
+
- `target`
|
354
|
+
`CollisionTarget` of the object firing the event
|
355
|
+
- `other`
|
356
|
+
`CollisionTarget` of the other object involved in the event
|
357
|
+
- `totalForce`
|
358
|
+
The sum of all the forces between the two colliders
|
359
|
+
- `totalForceMagnitude`
|
360
|
+
The sum of the magnitudes of each force between the two colliders
|
361
|
+
- `maxForceDirection`
|
362
|
+
The magnitude of the largest force at a contact point of this contact pair
|
363
|
+
- `maxForceMagnitude`
|
364
|
+
The world-space (unit) direction of the force with strongest magnitude
|
325
365
|
|
326
366
|
More information about each property can be found in the rapier [TempContactForceEvent API documentation](https://rapier.rs/javascript3d/classes/TempContactForceEvent.html).
|
327
367
|
|
@@ -339,6 +379,8 @@ You can also add the `onContactForce` event to any collider.
|
|
339
379
|
|
340
380
|
A Collider can be set to be a sensor, which means that it will not generate any contact points, and will not be affected by forces. This is useful for detecting when a collider enters or leaves another collider, without affecting the other collider.
|
341
381
|
|
382
|
+
To detect when a collider enters or leaves another collider, you can use the `onIntersectionEnter` and `onIntersectionExit` events on the collider.
|
383
|
+
|
342
384
|
```tsx
|
343
385
|
<RigidBody>
|
344
386
|
<GoalPosts />
|
@@ -351,9 +393,39 @@ A Collider can be set to be a sensor, which means that it will not generate any
|
|
351
393
|
</RigidBody>
|
352
394
|
```
|
353
395
|
|
354
|
-
##
|
396
|
+
## Attractors
|
355
397
|
|
356
|
-
|
398
|
+
An attractor simulates a source of gravity. Any `RigidBody` within range will be _pulled_ (attracted) toward the attractor.
|
399
|
+
Setting the `strength` to a negative value will cause the `RigidBody` to be _pushed_ (repelled) away from the attractor.
|
400
|
+
|
401
|
+
The force applied to rigid-bodies within range is calculated differently depending on the `type`.
|
402
|
+
|
403
|
+
```tsx
|
404
|
+
// Standard attractor
|
405
|
+
<Attractor range={10} strength={5} type="linear" position={[5, -5, 0]} />
|
406
|
+
|
407
|
+
// An attractor with negative strength, repels RigidBodies
|
408
|
+
<Attractor range={10} strength={-5} position={[5, -5, 0]} />
|
409
|
+
|
410
|
+
// You can also assign InteractionGroups.
|
411
|
+
// An attractor belonging to group 0 only affecting bodies in group 2, and 3
|
412
|
+
<Attractor range={10} strength={10} position={[5, -5, 0]} collisionGroups={interactionGroups(0, [2,3])} />
|
413
|
+
```
|
414
|
+
|
415
|
+
Gravity types:
|
416
|
+
|
417
|
+
- "static" (Default)
|
418
|
+
Static gravity means that the same force (`strength`) is applied on all rigid-bodies within range, regardless of distance.
|
419
|
+
|
420
|
+
- "linear"
|
421
|
+
Linear gravity means that force is calculated as `strength * distance / range`. That means the force applied decreases the farther a rigid-body is from the attractor position.
|
422
|
+
|
423
|
+
- "newtonian"
|
424
|
+
Newtonian gravity uses the traditional method of calculating gravitational force (`F = GMm/r^2`) and as such force is calculated as `gravitationalConstant * mass1 * mass2 / Math.pow(distance, 2)`.
|
425
|
+
- `gravitationalConstant` defaults to 6.673e-11 but you can provide your own
|
426
|
+
- `mass1` here is the "mass" of the Attractor, which is just the `strength` property
|
427
|
+
- `mass2` is the mass of the rigid-body at the time of calculation. Note that rigid-bodies with colliders will use the mass provided to the collider. This is not a value you can control from the attractor, only from wherever you're creating rigid-body components in the scene.
|
428
|
+
- `distance` is the distance between the attractor and rigid-body at the time of calculation
|
357
429
|
|
358
430
|
## Configuring Time Step Size
|
359
431
|
|
@@ -371,23 +443,16 @@ The `timeStep` prop may also be set to `"vary"`, which will cause the simulation
|
|
371
443
|
|
372
444
|
> **Note** This is useful for games that run at variable frame rates, but may cause instability in the simulation. It also prevents the physics simulation from being fully deterministic. Please use with care!
|
373
445
|
|
374
|
-
|
446
|
+
## Manual stepping
|
447
|
+
|
448
|
+
You can also manually step the physics simulation by calling the `step` method from the `useRapier` hook.
|
375
449
|
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
- [x] Translation and rotational constraints
|
386
|
-
- [x] Collision events
|
387
|
-
- [x] Colliders outside RigidBodies
|
388
|
-
- [x] InstancedMesh support
|
389
|
-
- [x] Timestep improvements for determinism
|
390
|
-
- [x] Normalize and improve collision events (add events to single Colliders)
|
391
|
-
- [x] Add collision events to InstancedRigidBodies
|
392
|
-
- [ ] Docs
|
393
|
-
- [ ] CodeSandbox examples
|
450
|
+
```tsx
|
451
|
+
const { step } = useRapier();
|
452
|
+
|
453
|
+
step(1 / 60);
|
454
|
+
```
|
455
|
+
|
456
|
+
## Joints
|
457
|
+
|
458
|
+
WIP
|