angular-three-cannon 4.0.0-next.12 → 4.0.0-next.121
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/README.md +38 -19
- package/body/README.md +38 -17
- package/constraint/README.md +44 -17
- package/debug/README.md +28 -19
- package/fesm2022/angular-three-cannon-body.mjs +285 -16
- package/fesm2022/angular-three-cannon-body.mjs.map +1 -1
- package/fesm2022/angular-three-cannon-constraint.mjs +144 -13
- package/fesm2022/angular-three-cannon-constraint.mjs.map +1 -1
- package/fesm2022/angular-three-cannon-debug.mjs +52 -7
- package/fesm2022/angular-three-cannon-debug.mjs.map +1 -1
- package/fesm2022/angular-three-cannon.mjs +47 -9
- package/fesm2022/angular-three-cannon.mjs.map +1 -1
- package/package.json +9 -9
- package/types/angular-three-cannon-body.d.ts +398 -0
- package/types/angular-three-cannon-constraint.d.ts +214 -0
- package/types/angular-three-cannon-debug.d.ts +92 -0
- package/types/angular-three-cannon.d.ts +145 -0
- package/body/index.d.ts +0 -2
- package/body/lib/body.d.ts +0 -17
- package/body/lib/types.d.ts +0 -49
- package/body/lib/utils.d.ts +0 -50
- package/constraint/index.d.ts +0 -1
- package/constraint/lib/constraint.d.ts +0 -32
- package/debug/index.d.ts +0 -1
- package/debug/lib/debug.d.ts +0 -24
- package/index.d.ts +0 -1
- package/lib/physics.d.ts +0 -60
package/README.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
This library is a wrapper around the [Cannon.js](https://schteppe.github.io/cannon.js/) physics engine for use with Angular Three.
|
|
4
4
|
|
|
5
|
+
## Documentation
|
|
6
|
+
|
|
7
|
+
All public APIs are documented with JSDoc comments. Your IDE will provide inline documentation, parameter hints, and examples as you code.
|
|
8
|
+
|
|
5
9
|
## Installation
|
|
6
10
|
|
|
7
11
|
```bash
|
|
@@ -15,6 +19,11 @@ npm install angular-three-cannon cannon-es @pmndrs/cannon-worker-api
|
|
|
15
19
|
## Usage
|
|
16
20
|
|
|
17
21
|
```typescript
|
|
22
|
+
import { Component, viewChild, ElementRef } from '@angular/core';
|
|
23
|
+
import { Mesh } from 'three';
|
|
24
|
+
import { NgtcPhysics } from 'angular-three-cannon';
|
|
25
|
+
import { box } from 'angular-three-cannon/body';
|
|
26
|
+
|
|
18
27
|
@Component({
|
|
19
28
|
template: `
|
|
20
29
|
<ngt-mesh #mesh>
|
|
@@ -27,7 +36,7 @@ export class Box {
|
|
|
27
36
|
|
|
28
37
|
constructor() {
|
|
29
38
|
// Make this mesh a Box body in Physics. Only works within ngtc-physics
|
|
30
|
-
|
|
39
|
+
box(() => ({ mass: 10000, position: [0, 0, 0], args: [1, 1, 1] }), this.mesh);
|
|
31
40
|
}
|
|
32
41
|
}
|
|
33
42
|
|
|
@@ -42,24 +51,34 @@ export class Box {
|
|
|
42
51
|
export class SceneGraph {}
|
|
43
52
|
```
|
|
44
53
|
|
|
45
|
-
###
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
### Options
|
|
55
|
+
|
|
56
|
+
The `NgtcPhysics` component accepts an `options` input with the following properties:
|
|
57
|
+
|
|
58
|
+
```html
|
|
59
|
+
<ngtc-physics [options]="{ gravity: [0, -9.81, 0], iterations: 10 }">
|
|
60
|
+
<!-- Physics bodies here -->
|
|
61
|
+
</ngtc-physics>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
| Property | Type | Default |
|
|
65
|
+
| ------------------------ | ------------------------ | ----------------------------------- |
|
|
66
|
+
| `allowSleep` | `boolean` | `false` |
|
|
67
|
+
| `axisIndex` | `0 \| 1 \| 2` | `0` |
|
|
68
|
+
| `broadphase` | `'Naive' \| 'SAP'` | `'Naive'` |
|
|
69
|
+
| `defaultContactMaterial` | `ContactMaterialOptions` | `{ contactEquationStiffness: 1e6 }` |
|
|
70
|
+
| `frictionGravity` | `Vector3 \| null` | `null` |
|
|
71
|
+
| `gravity` | `Vector3` | `[0, -9.81, 0]` |
|
|
72
|
+
| `isPaused` | `boolean` | `false` |
|
|
73
|
+
| `iterations` | `number` | `5` |
|
|
74
|
+
| `maxSubSteps` | `number` | `10` |
|
|
75
|
+
| `quatNormalizeFast` | `boolean` | `false` |
|
|
76
|
+
| `quatNormalizeSkip` | `number` | `0` |
|
|
77
|
+
| `shouldInvalidate` | `boolean` | `true` |
|
|
78
|
+
| `size` | `number` | `1000` |
|
|
79
|
+
| `solver` | `'GS' \| 'Split'` | `'GS'` |
|
|
80
|
+
| `stepSize` | `number` | `1/60` |
|
|
81
|
+
| `tolerance` | `number` | `0.001` |
|
|
63
82
|
|
|
64
83
|
## Debug
|
|
65
84
|
|
package/body/README.md
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
1
|
# `angular-three-cannon/body`
|
|
2
2
|
|
|
3
|
-
This module provides
|
|
4
|
-
|
|
5
|
-
### Available
|
|
6
|
-
|
|
7
|
-
| Function
|
|
8
|
-
|
|
|
9
|
-
| `
|
|
10
|
-
| `
|
|
11
|
-
| `
|
|
12
|
-
| `
|
|
13
|
-
| `
|
|
14
|
-
| `
|
|
15
|
-
| `
|
|
16
|
-
| `
|
|
17
|
-
| `
|
|
3
|
+
This module provides functions to create physics bodies that link `ngt-*` objects in THREE.js with bodies in the Cannon.js physics world. These functions simplify the process of adding physics behavior to your 3D objects.
|
|
4
|
+
|
|
5
|
+
### Available Body Functions
|
|
6
|
+
|
|
7
|
+
| Function | Description | `args` Arguments | `object` Type |
|
|
8
|
+
| :----------------- | :----------------------------------------------- | :----------------------------------------------- | :------------ |
|
|
9
|
+
| `box` | Creates a box-shaped physics body. | `[width, height, depth]` | `ElementRef` |
|
|
10
|
+
| `convexPolyhedron` | Creates a convex polyhedron-shaped physics body. | `[vertices, faces]` | `ElementRef` |
|
|
11
|
+
| `cylinder` | Creates a cylinder-shaped physics body. | `[radiusTop, radiusBottom, height, numSegments]` | `ElementRef` |
|
|
12
|
+
| `heightfield` | Creates a heightfield-shaped physics body. | `[data, { elementSize }]` | `ElementRef` |
|
|
13
|
+
| `particle` | Creates a particle physics body. | (none) | `ElementRef` |
|
|
14
|
+
| `plane` | Creates a plane-shaped physics body. | (none) | `ElementRef` |
|
|
15
|
+
| `sphere` | Creates a sphere-shaped physics body. | `[radius]` | `ElementRef` |
|
|
16
|
+
| `trimesh` | Creates a trimesh-shaped physics body. | `[vertices, indices]` | `ElementRef` |
|
|
17
|
+
| `compound` | Creates a compound physics body. | (uses `shapes` property instead) | `ElementRef` |
|
|
18
18
|
|
|
19
19
|
**All functions also accept an optional `options` argument for additional customization.**
|
|
20
20
|
|
|
21
|
-
### Simple Usage of `
|
|
21
|
+
### Simple Usage of `box()`
|
|
22
22
|
|
|
23
23
|
```typescript
|
|
24
|
+
import { Component, viewChild, ElementRef } from '@angular/core';
|
|
25
|
+
import { Mesh } from 'three';
|
|
26
|
+
import { NgtcPhysics } from 'angular-three-cannon';
|
|
27
|
+
import { box } from 'angular-three-cannon/body';
|
|
28
|
+
|
|
24
29
|
@Component({
|
|
25
30
|
template: `
|
|
26
31
|
<ngt-mesh #mesh>
|
|
@@ -33,7 +38,7 @@ export class Box {
|
|
|
33
38
|
|
|
34
39
|
constructor() {
|
|
35
40
|
// Make this mesh a Box body in Physics. Only works within ngtc-physics
|
|
36
|
-
|
|
41
|
+
box(() => ({ mass: 10000, position: [0, 0, 0], args: [1, 1, 1] }), this.mesh);
|
|
37
42
|
}
|
|
38
43
|
}
|
|
39
44
|
|
|
@@ -47,3 +52,19 @@ export class Box {
|
|
|
47
52
|
})
|
|
48
53
|
export class SceneGraph {}
|
|
49
54
|
```
|
|
55
|
+
|
|
56
|
+
### Deprecated Functions
|
|
57
|
+
|
|
58
|
+
The following `inject*` functions are deprecated and will be removed in v5.0.0. Use the new function names instead:
|
|
59
|
+
|
|
60
|
+
| Deprecated | Use Instead |
|
|
61
|
+
| :----------------------- | :----------------- |
|
|
62
|
+
| `injectBox` | `box` |
|
|
63
|
+
| `injectConvexPolyhedron` | `convexPolyhedron` |
|
|
64
|
+
| `injectCylinder` | `cylinder` |
|
|
65
|
+
| `injectHeightfield` | `heightfield` |
|
|
66
|
+
| `injectParticle` | `particle` |
|
|
67
|
+
| `injectPlane` | `plane` |
|
|
68
|
+
| `injectSphere` | `sphere` |
|
|
69
|
+
| `injectTrimesh` | `trimesh` |
|
|
70
|
+
| `injectCompound` | `compound` |
|
package/constraint/README.md
CHANGED
|
@@ -2,23 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
This module provides functions to create physics constraints that link physics bodies in the Cannon.js physics world. These functions simplify the process of adding constraints between your 3D objects.
|
|
4
4
|
|
|
5
|
-
### Available
|
|
5
|
+
### Available Constraint Functions
|
|
6
6
|
|
|
7
|
-
| Function
|
|
8
|
-
|
|
|
9
|
-
| `
|
|
10
|
-
| `
|
|
11
|
-
| `
|
|
12
|
-
| `
|
|
13
|
-
| `
|
|
7
|
+
| Function | Description |
|
|
8
|
+
| :------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `pointToPoint` | Creates a point-to-point constraint between two bodies. The constraint tries to keep the distance between the anchor points constant. |
|
|
10
|
+
| `coneTwist` | Creates a cone-twist constraint between two bodies. The constraint attempts to keep the bodies aligned along a common axis, allowing swing and twist motion. |
|
|
11
|
+
| `distance` | Creates a distance constraint between two bodies. The constraint tries to keep the distance between the bodies' center of masses constant. |
|
|
12
|
+
| `lock` | Creates a lock constraint between two bodies. The constraint completely locks the motion of one body relative to another. |
|
|
13
|
+
| `hinge` | Creates a hinge constraint between two bodies. The constraint allows for rotation around a shared axis, like a door hinge. |
|
|
14
14
|
|
|
15
|
-
**All functions
|
|
15
|
+
**All functions take `bodyA`, `bodyB`, and an optional `options` object as arguments.**
|
|
16
16
|
|
|
17
|
-
### Simple Usage of `
|
|
17
|
+
### Simple Usage of `hinge()`
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
20
|
import { Component, viewChild, ElementRef } from '@angular/core';
|
|
21
|
-
import {
|
|
21
|
+
import { Mesh } from 'three';
|
|
22
|
+
import { hinge } from 'angular-three-cannon/constraint';
|
|
22
23
|
|
|
23
24
|
@Component({
|
|
24
25
|
template: `
|
|
@@ -36,12 +37,38 @@ export class HingedBoxes {
|
|
|
36
37
|
|
|
37
38
|
constructor() {
|
|
38
39
|
// Create a hinge constraint between mesh1 and mesh2. Only works within <ngtc-physics>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
hinge(this.mesh1, this.mesh2, {
|
|
41
|
+
options: {
|
|
42
|
+
pivotA: [0, 0.5, 0], // hinge location on the first body
|
|
43
|
+
pivotB: [0, -0.5, 0], // hinge location on the second body
|
|
44
|
+
axisA: [0, 1, 0], // axis of rotation on the first body
|
|
45
|
+
axisB: [0, 1, 0], // axis of rotation on the second body
|
|
46
|
+
},
|
|
47
|
+
});
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
50
|
```
|
|
51
|
+
|
|
52
|
+
### Constraint Options
|
|
53
|
+
|
|
54
|
+
All constraint functions accept an optional third argument with the following properties:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
interface NgtcConstraintOptions {
|
|
58
|
+
injector?: Injector; // Angular injector for dependency injection
|
|
59
|
+
disableOnStart?: boolean; // Whether to create the constraint disabled (default: false)
|
|
60
|
+
options?: ConstraintSpecificOptions; // Constraint-specific configuration
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Deprecated Functions
|
|
65
|
+
|
|
66
|
+
The following `inject*` functions are deprecated and will be removed in v5.0.0. Use the new function names instead:
|
|
67
|
+
|
|
68
|
+
| Deprecated | Use Instead |
|
|
69
|
+
| :------------------- | :------------- |
|
|
70
|
+
| `injectPointToPoint` | `pointToPoint` |
|
|
71
|
+
| `injectConeTwist` | `coneTwist` |
|
|
72
|
+
| `injectDistance` | `distance` |
|
|
73
|
+
| `injectLock` | `lock` |
|
|
74
|
+
| `injectHinge` | `hinge` |
|
package/debug/README.md
CHANGED
|
@@ -14,36 +14,45 @@ npm install cannon-es-debugger
|
|
|
14
14
|
# pnpm add cannon-es-debugger
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
## NgtcDebugApi
|
|
18
|
-
|
|
19
|
-
The `NgtcDebugApi` interface provides methods to interact with the debug renderer:
|
|
20
|
-
|
|
21
|
-
- `add(uuid: string, props: BodyProps, type: BodyShapeType)`: Adds a physics body to the debug renderer.
|
|
22
|
-
- `remove(uuid: string)`: Removes a physics body from the debug renderer.
|
|
23
|
-
|
|
24
|
-
### `injectNgtcDebugApi`
|
|
25
|
-
|
|
26
|
-
The `injectNgtcDebugApi` function is used to inject the `NgtcDebugApi` into your components, enabling you to control the debug visualization.
|
|
27
|
-
|
|
28
17
|
## NgtcDebug
|
|
29
18
|
|
|
30
|
-
The `NgtcDebug` directive is applied to the `ngtc-physics` component to enable physics debugging. It has the following
|
|
19
|
+
The `NgtcDebug` directive is applied to the `ngtc-physics` component to enable physics debugging. It has the following input:
|
|
31
20
|
|
|
32
21
|
- `debug`: An object containing the following properties:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
- `scale`: (number) The scale of the debug visualization (default: 1).
|
|
22
|
+
- `enabled`: (boolean) Whether the debug visualization is enabled (default: true).
|
|
23
|
+
- `color`: (string) The color of the debug visualization (default: 'black').
|
|
24
|
+
- `impl`: (typeof CannonDebugger) The implementation of the CannonDebugger to use (default: CannonDebugger).
|
|
25
|
+
- `scale`: (number) The scale of the debug visualization (default: 1).
|
|
38
26
|
|
|
39
27
|
## Usage
|
|
40
28
|
|
|
41
29
|
```html
|
|
42
|
-
<ngtc-physics debug
|
|
30
|
+
<ngtc-physics [debug]="{ enabled: true }">
|
|
31
|
+
<!-- Physics bodies here -->
|
|
32
|
+
</ngtc-physics>
|
|
43
33
|
```
|
|
44
34
|
|
|
45
35
|
You can customize the debug visualization by providing input values within the `debug` object:
|
|
46
36
|
|
|
47
37
|
```html
|
|
48
|
-
<ngtc-physics [debug]="{enabled: true, color: 'red', scale: 0.5}"
|
|
38
|
+
<ngtc-physics [debug]="{ enabled: true, color: 'red', scale: 0.5 }">
|
|
39
|
+
<!-- Physics bodies here -->
|
|
40
|
+
</ngtc-physics>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Toggle debug visualization based on component state:
|
|
44
|
+
|
|
45
|
+
```html
|
|
46
|
+
<ngtc-physics [debug]="{ enabled: isDebugging() }">
|
|
47
|
+
<!-- Physics bodies here -->
|
|
48
|
+
</ngtc-physics>
|
|
49
49
|
```
|
|
50
|
+
|
|
51
|
+
## NgtcDebug Methods
|
|
52
|
+
|
|
53
|
+
The `NgtcDebug` class provides the following methods for internal use:
|
|
54
|
+
|
|
55
|
+
- `add(uuid: string, props: BodyProps, type: BodyShapeType)`: Adds a physics body to the debug renderer.
|
|
56
|
+
- `remove(uuid: string)`: Removes a physics body from the debug renderer.
|
|
57
|
+
|
|
58
|
+
These methods are called automatically by body functions when debug is enabled.
|
|
@@ -20,6 +20,25 @@ function quaternionToRotation(callback) {
|
|
|
20
20
|
return (v) => callback(e.setFromQuaternion(q.fromArray(v)).toArray());
|
|
21
21
|
}
|
|
22
22
|
let incrementingId = 0;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a subscription function for monitoring physics body property changes.
|
|
25
|
+
*
|
|
26
|
+
* @template T - The subscription property name type
|
|
27
|
+
* @param body - The Three.js Object3D associated with the physics body
|
|
28
|
+
* @param worker - The Cannon.js worker API instance
|
|
29
|
+
* @param subscriptions - Map to store active subscriptions
|
|
30
|
+
* @param type - The property type to subscribe to (e.g., 'position', 'velocity')
|
|
31
|
+
* @param index - Optional instance index for InstancedMesh bodies
|
|
32
|
+
* @param target - Subscription target, defaults to 'bodies'
|
|
33
|
+
* @returns Function that creates a subscription and returns an unsubscribe function
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const subscribe = createSubscribe(body, worker, subscriptions, 'position');
|
|
38
|
+
* const unsubscribe = subscribe((position) => console.log(position));
|
|
39
|
+
* // Later: unsubscribe();
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
23
42
|
function createSubscribe(body, worker, subscriptions, type, index, target = 'bodies') {
|
|
24
43
|
return (callback) => {
|
|
25
44
|
const id = incrementingId++;
|
|
@@ -35,15 +54,51 @@ function createSubscribe(body, worker, subscriptions, type, index, target = 'bod
|
|
|
35
54
|
function makeTriplet(v) {
|
|
36
55
|
return is.three(v, 'isVector3') ? [v.x, v.y, v.z] : v;
|
|
37
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Prepares a Three.js Object3D with initial physics body properties.
|
|
59
|
+
* Sets the object's position, rotation, and userData from body props.
|
|
60
|
+
*
|
|
61
|
+
* @param object - The Three.js Object3D to prepare
|
|
62
|
+
* @param props - Body properties containing position, rotation, and userData
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* prepare(mesh, { position: [0, 5, 0], rotation: [0, Math.PI, 0] });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
38
69
|
function prepare(object, { position = [0, 0, 0], rotation = [0, 0, 0], userData = {} }) {
|
|
39
70
|
object.userData = userData;
|
|
40
71
|
object.position.set(...position);
|
|
41
72
|
object.rotation.set(...rotation);
|
|
42
73
|
object.updateMatrix();
|
|
43
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Sets up collision event handlers for a physics body.
|
|
77
|
+
* Registers callbacks for collide, collideBegin, and collideEnd events.
|
|
78
|
+
*
|
|
79
|
+
* @param events - The events map to register handlers in
|
|
80
|
+
* @param props - Body props containing collision callback functions
|
|
81
|
+
* @param uuid - The unique identifier for the physics body
|
|
82
|
+
*/
|
|
44
83
|
function setupCollision(events, { onCollide, onCollideBegin, onCollideEnd }, uuid) {
|
|
45
84
|
events[uuid] = { collide: onCollide, collideBegin: onCollideBegin, collideEnd: onCollideEnd };
|
|
46
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Creates the public API for controlling a physics body.
|
|
88
|
+
* Provides methods for applying forces, setting properties, and subscribing to changes.
|
|
89
|
+
*
|
|
90
|
+
* @param body - The Three.js Object3D associated with the physics body
|
|
91
|
+
* @param worker - The Cannon.js worker API instance
|
|
92
|
+
* @param context - Physics context containing subscriptions and scale overrides
|
|
93
|
+
* @returns The body's public API with all available methods
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const api = makeBodyApi(mesh, worker, { subscriptions, scaleOverrides });
|
|
98
|
+
* api.applyForce([0, 100, 0], [0, 0, 0]);
|
|
99
|
+
* api.position.subscribe((pos) => console.log(pos));
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
47
102
|
function makeBodyApi(body, worker, { subscriptions, scaleOverrides }) {
|
|
48
103
|
const makeAtomic = (type, index) => {
|
|
49
104
|
const op = `set${capitalize(type)}`;
|
|
@@ -175,6 +230,21 @@ function makeBodyApi(body, worker, { subscriptions, scaleOverrides }) {
|
|
|
175
230
|
const cache = {};
|
|
176
231
|
return { ...makeApi(), at: (index) => cache[index] || (cache[index] = makeApi(index)) };
|
|
177
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Default argument transformation functions for each physics body shape type.
|
|
235
|
+
* These functions convert Three.js-friendly arguments to Cannon.js-compatible formats.
|
|
236
|
+
*
|
|
237
|
+
* Each transformer handles the specific requirements of its shape type:
|
|
238
|
+
* - Plane: No arguments needed
|
|
239
|
+
* - Box: [width, height, depth], defaults to [1, 1, 1]
|
|
240
|
+
* - Trimesh: Passes through vertices and indices unchanged
|
|
241
|
+
* - Cylinder: No arguments needed (uses shape defaults)
|
|
242
|
+
* - Heightfield: Passes through height data unchanged
|
|
243
|
+
* - ConvexPolyhedron: Converts Vector3 arrays to triplet arrays
|
|
244
|
+
* - Particle: No arguments needed
|
|
245
|
+
* - Sphere: [radius], defaults to [1]
|
|
246
|
+
* - Compound: Passes through shape array unchanged
|
|
247
|
+
*/
|
|
178
248
|
const defaultTransformArgs = {
|
|
179
249
|
Plane: (_) => [],
|
|
180
250
|
Box: (args = [1, 1, 1]) => args,
|
|
@@ -197,11 +267,11 @@ const defaultTransformArgs = {
|
|
|
197
267
|
Compound: (args) => args,
|
|
198
268
|
};
|
|
199
269
|
|
|
200
|
-
function
|
|
201
|
-
return (getPropFn, ref, options) =>
|
|
270
|
+
function createBody(type) {
|
|
271
|
+
return (getPropFn, ref, options) => body(type, getPropFn, ref, options);
|
|
202
272
|
}
|
|
203
|
-
function
|
|
204
|
-
return assertInjector(
|
|
273
|
+
function body(type, getPropFn, ref, { transformArgs, injector } = {}) {
|
|
274
|
+
return assertInjector(body, injector, () => {
|
|
205
275
|
const physics = inject(NgtcPhysics, { optional: true });
|
|
206
276
|
if (!physics) {
|
|
207
277
|
throw new Error(`[NGT Cannon] injectBody was called outside of <ngtc-physics>`);
|
|
@@ -210,7 +280,7 @@ function injectBody(type, getPropFn, ref, { transformArgs, injector } = {}) {
|
|
|
210
280
|
const transform = transformArgs ?? defaultTransformArgs[type];
|
|
211
281
|
const isRefSignal = isSignal(ref);
|
|
212
282
|
const bodyRef = (isRefSignal ? ref : signal(ref));
|
|
213
|
-
const body = computed(() => resolveRef(bodyRef()));
|
|
283
|
+
const body = computed(() => resolveRef(bodyRef()), ...(ngDevMode ? [{ debugName: "body" }] : []));
|
|
214
284
|
const api = computed(() => {
|
|
215
285
|
const _body = body();
|
|
216
286
|
if (!_body)
|
|
@@ -220,7 +290,7 @@ function injectBody(type, getPropFn, ref, { transformArgs, injector } = {}) {
|
|
|
220
290
|
if (!_worker)
|
|
221
291
|
return null;
|
|
222
292
|
return makeBodyApi(_body, _worker, rest);
|
|
223
|
-
});
|
|
293
|
+
}, ...(ngDevMode ? [{ debugName: "api" }] : []));
|
|
224
294
|
effect((onCleanup) => {
|
|
225
295
|
const currentWorker = physics.worker();
|
|
226
296
|
if (!currentWorker)
|
|
@@ -288,19 +358,218 @@ function injectBody(type, getPropFn, ref, { transformArgs, injector } = {}) {
|
|
|
288
358
|
return api;
|
|
289
359
|
});
|
|
290
360
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
361
|
+
/**
|
|
362
|
+
* Creates a box-shaped physics body.
|
|
363
|
+
*
|
|
364
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
365
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
366
|
+
* @param options - Optional configuration for the body
|
|
367
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```typescript
|
|
371
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
372
|
+
* const api = box(() => ({ mass: 1, args: [1, 1, 1], position: [0, 5, 0] }), mesh);
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
375
|
+
const box = createBody('Box');
|
|
376
|
+
/**
|
|
377
|
+
* Creates a convex polyhedron physics body from vertices and faces.
|
|
378
|
+
*
|
|
379
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
380
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
381
|
+
* @param options - Optional configuration for the body
|
|
382
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```typescript
|
|
386
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
387
|
+
* const api = convexPolyhedron(() => ({
|
|
388
|
+
* mass: 1,
|
|
389
|
+
* args: [vertices, faces],
|
|
390
|
+
* position: [0, 5, 0]
|
|
391
|
+
* }), mesh);
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
const convexPolyhedron = createBody('ConvexPolyhedron');
|
|
395
|
+
/**
|
|
396
|
+
* Creates a cylinder-shaped physics body.
|
|
397
|
+
*
|
|
398
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
399
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
400
|
+
* @param options - Optional configuration for the body
|
|
401
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
402
|
+
*
|
|
403
|
+
* @example
|
|
404
|
+
* ```typescript
|
|
405
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
406
|
+
* // args: [radiusTop, radiusBottom, height, numSegments]
|
|
407
|
+
* const api = cylinder(() => ({ mass: 1, args: [0.5, 0.5, 2, 16], position: [0, 5, 0] }), mesh);
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
const cylinder = createBody('Cylinder');
|
|
411
|
+
/**
|
|
412
|
+
* Creates a heightfield physics body for terrain simulation.
|
|
413
|
+
*
|
|
414
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
415
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
416
|
+
* @param options - Optional configuration for the body
|
|
417
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
422
|
+
* const heightData = [[0, 0, 0], [0, 1, 0], [0, 0, 0]];
|
|
423
|
+
* const api = heightfield(() => ({
|
|
424
|
+
* mass: 0,
|
|
425
|
+
* args: [heightData, { elementSize: 1 }],
|
|
426
|
+
* position: [0, 0, 0]
|
|
427
|
+
* }), mesh);
|
|
428
|
+
* ```
|
|
429
|
+
*/
|
|
430
|
+
const heightfield = createBody('Heightfield');
|
|
431
|
+
/**
|
|
432
|
+
* Creates a particle (point mass) physics body with no shape.
|
|
433
|
+
*
|
|
434
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
435
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
436
|
+
* @param options - Optional configuration for the body
|
|
437
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
442
|
+
* const api = particle(() => ({ mass: 1, position: [0, 5, 0] }), mesh);
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
const particle = createBody('Particle');
|
|
446
|
+
/**
|
|
447
|
+
* Creates an infinite plane physics body, typically used for floors or walls.
|
|
448
|
+
*
|
|
449
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
450
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
451
|
+
* @param options - Optional configuration for the body
|
|
452
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```typescript
|
|
456
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
457
|
+
* const api = plane(() => ({
|
|
458
|
+
* mass: 0, // Static body
|
|
459
|
+
* rotation: [-Math.PI / 2, 0, 0], // Horizontal
|
|
460
|
+
* position: [0, 0, 0]
|
|
461
|
+
* }), mesh);
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
464
|
+
const plane = createBody('Plane');
|
|
465
|
+
/**
|
|
466
|
+
* Creates a sphere-shaped physics body.
|
|
467
|
+
*
|
|
468
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
469
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
470
|
+
* @param options - Optional configuration for the body
|
|
471
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```typescript
|
|
475
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
476
|
+
* // args: [radius]
|
|
477
|
+
* const api = sphere(() => ({ mass: 1, args: [1], position: [0, 5, 0] }), mesh);
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
const sphere = createBody('Sphere');
|
|
481
|
+
/**
|
|
482
|
+
* Creates a trimesh physics body from vertices and indices.
|
|
483
|
+
* Useful for complex static geometry like terrain or obstacles.
|
|
484
|
+
*
|
|
485
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
486
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
487
|
+
* @param options - Optional configuration for the body
|
|
488
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```typescript
|
|
492
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
493
|
+
* const api = trimesh(() => ({
|
|
494
|
+
* mass: 0,
|
|
495
|
+
* args: [vertices, indices],
|
|
496
|
+
* position: [0, 0, 0]
|
|
497
|
+
* }), mesh);
|
|
498
|
+
* ```
|
|
499
|
+
*/
|
|
500
|
+
const trimesh = createBody('Trimesh');
|
|
501
|
+
/**
|
|
502
|
+
* Creates a compound physics body composed of multiple shapes.
|
|
503
|
+
* Useful for complex objects that can be approximated by combining primitives.
|
|
504
|
+
*
|
|
505
|
+
* @param getPropFn - Function returning body properties for each instance index
|
|
506
|
+
* @param ref - Reference to the Three.js Object3D to attach physics to
|
|
507
|
+
* @param options - Optional configuration for the body
|
|
508
|
+
* @returns Signal containing the body's public API, or null if not ready
|
|
509
|
+
*
|
|
510
|
+
* @example
|
|
511
|
+
* ```typescript
|
|
512
|
+
* const mesh = viewChild.required<ElementRef<Mesh>>('mesh');
|
|
513
|
+
* const api = compound(() => ({
|
|
514
|
+
* mass: 1,
|
|
515
|
+
* shapes: [
|
|
516
|
+
* { type: 'Box', args: [1, 1, 1], position: [0, 0, 0] },
|
|
517
|
+
* { type: 'Sphere', args: [0.5], position: [0, 1, 0] }
|
|
518
|
+
* ],
|
|
519
|
+
* position: [0, 5, 0]
|
|
520
|
+
* }), mesh);
|
|
521
|
+
* ```
|
|
522
|
+
*/
|
|
523
|
+
const compound = createBody('Compound');
|
|
524
|
+
/**
|
|
525
|
+
* @deprecated Use `box` instead. Will be removed in v5.0.0
|
|
526
|
+
* @since v4.0.0
|
|
527
|
+
*/
|
|
528
|
+
const injectBox = box;
|
|
529
|
+
/**
|
|
530
|
+
* @deprecated Use `convexPolyhedron` instead. Will be removed in v5.0.0
|
|
531
|
+
* @since v4.0.0
|
|
532
|
+
*/
|
|
533
|
+
const injectConvexPolyhedron = convexPolyhedron;
|
|
534
|
+
/**
|
|
535
|
+
* @deprecated Use `cylinder` instead. Will be removed in v5.0.0
|
|
536
|
+
* @since v4.0.0
|
|
537
|
+
*/
|
|
538
|
+
const injectCylinder = cylinder;
|
|
539
|
+
/**
|
|
540
|
+
* @deprecated Use `heightfield` instead. Will be removed in v5.0.0
|
|
541
|
+
* @since v4.0.0
|
|
542
|
+
*/
|
|
543
|
+
const injectHeightfield = heightfield;
|
|
544
|
+
/**
|
|
545
|
+
* @deprecated Use `particle` instead. Will be removed in v5.0.0
|
|
546
|
+
* @since v4.0.0
|
|
547
|
+
*/
|
|
548
|
+
const injectParticle = particle;
|
|
549
|
+
/**
|
|
550
|
+
* @deprecated Use `plane` instead. Will be removed in v5.0.0
|
|
551
|
+
* @since v4.0.0
|
|
552
|
+
*/
|
|
553
|
+
const injectPlane = plane;
|
|
554
|
+
/**
|
|
555
|
+
* @deprecated Use `sphere` instead. Will be removed in v5.0.0
|
|
556
|
+
* @since v4.0.0
|
|
557
|
+
*/
|
|
558
|
+
const injectSphere = sphere;
|
|
559
|
+
/**
|
|
560
|
+
* @deprecated Use `trimesh` instead. Will be removed in v5.0.0
|
|
561
|
+
* @since v4.0.0
|
|
562
|
+
*/
|
|
563
|
+
const injectTrimesh = trimesh;
|
|
564
|
+
/**
|
|
565
|
+
* @deprecated Use `compound` instead. Will be removed in v5.0.0
|
|
566
|
+
* @since v4.0.0
|
|
567
|
+
*/
|
|
568
|
+
const injectCompound = compound;
|
|
300
569
|
|
|
301
570
|
/**
|
|
302
571
|
* Generated bundle index. Do not edit.
|
|
303
572
|
*/
|
|
304
573
|
|
|
305
|
-
export { injectBox, injectCompound, injectConvexPolyhedron, injectCylinder, injectHeightfield, injectParticle, injectPlane, injectSphere, injectTrimesh };
|
|
574
|
+
export { box, compound, convexPolyhedron, cylinder, heightfield, injectBox, injectCompound, injectConvexPolyhedron, injectCylinder, injectHeightfield, injectParticle, injectPlane, injectSphere, injectTrimesh, particle, plane, sphere, trimesh };
|
|
306
575
|
//# sourceMappingURL=angular-three-cannon-body.mjs.map
|