|
| 1 | +--- |
| 2 | +title: Body Functions |
| 3 | +description: Detailed explanation of the Body functions in Angular Three Cannon |
| 4 | +--- |
| 5 | + |
| 6 | +Angular Three Cannon provides various body functions to create different types of physics bodies. These functions are used to add physical properties to your 3D objects. |
| 7 | + |
| 8 | +## Available Body Functions |
| 9 | + |
| 10 | +All body functions are available from `angular-three-cannon/body`: |
| 11 | + |
| 12 | +```angular-ts |
| 13 | +import { |
| 14 | + injectBox, |
| 15 | + injectSphere, |
| 16 | + injectPlane, |
| 17 | + injectCylinder, |
| 18 | + injectHeightfield, |
| 19 | + injectParticle, |
| 20 | + injectConvexPolyhedron, |
| 21 | + injectTrimesh, |
| 22 | + injectCompound |
| 23 | +} from 'angular-three-cannon/body'; |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +The general pattern for using these functions is: |
| 29 | + |
| 30 | +```angular-ts |
| 31 | +import { Component, ElementRef, viewChild } from '@angular/core'; |
| 32 | +import { injectBox } from 'angular-three-cannon/body'; |
| 33 | +import { NgtMesh } from 'angular-three'; |
| 34 | +
|
| 35 | +@Component({ |
| 36 | + selector: 'app-physics-box', |
| 37 | + standalone: true, |
| 38 | + template: ` |
| 39 | + <ngt-mesh #mesh> |
| 40 | + <ngt-box-geometry /> |
| 41 | + <ngt-mesh-standard-material /> |
| 42 | + </ngt-mesh> |
| 43 | + `, |
| 44 | +}) |
| 45 | +export class PhysicsBox { |
| 46 | + mesh = viewChild.required<ElementRef<THREE.Mesh>>('mesh'); |
| 47 | +
|
| 48 | + boxBody = injectBox( |
| 49 | + () => ({ |
| 50 | + mass: 1, |
| 51 | + position: [0, 5, 0], |
| 52 | + args: [1, 1, 1], |
| 53 | + }), |
| 54 | + this.mesh |
| 55 | + ); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## Body Functions |
| 60 | + |
| 61 | +| Function | Description | Specific Options | |
| 62 | +|----------|-------------|------------------| |
| 63 | +| `injectBox` | Creates a box-shaped body | `args: [width, height, depth]` | |
| 64 | +| `injectSphere` | Creates a spherical body | `args: [radius]` | |
| 65 | +| `injectPlane` | Creates an infinite plane | No specific options | |
| 66 | +| `injectCylinder` | Creates a cylindrical body | `args: [radiusTop, radiusBottom, height, numSegments]` | |
| 67 | +| `injectHeightfield` | Creates a heightfield body | `args: [data, options]` | |
| 68 | +| `injectParticle` | Creates a particle (point mass) | No specific options | |
| 69 | +| `injectConvexPolyhedron` | Creates a convex polyhedron | `args: [vertices, faces]` | |
| 70 | +| `injectTrimesh` | Creates a triangular mesh body | `args: [vertices, indices]` | |
| 71 | +| `injectCompound` | Creates a compound body | `shapes: Array<{ type, args, position?, rotation? }>` | |
| 72 | + |
| 73 | +## Common Options |
| 74 | + |
| 75 | +All body functions accept a set of common options: |
| 76 | + |
| 77 | +| Option | Type | Description | |
| 78 | +|--------|------|-------------| |
| 79 | +| `mass` | number | The mass of the body (0 for static bodies) | |
| 80 | +| `position` | [x: number, y: number, z: number] | Initial position of the body | |
| 81 | +| `rotation` | [x: number, y: number, z: number] | Initial rotation of the body (in radians) | |
| 82 | +| `velocity` | [x: number, y: number, z: number] | Initial velocity of the body | |
| 83 | +| `angularVelocity` | [x: number, y: number, z: number] | Initial angular velocity of the body | |
| 84 | +| `linearDamping` | number | Linear damping of the body (0 = no damping, 1 = full damping) | |
| 85 | +| `angularDamping` | number | Angular damping of the body | |
| 86 | +| `fixedRotation` | boolean | If true, body will not rotate | |
| 87 | +| `collisionFilterGroup` | number | The collision group the body belongs to | |
| 88 | +| `collisionFilterMask` | number | Which groups this body can collide with | |
| 89 | +| `trigger` | boolean | If true, body acts as a trigger (no collision response) | |
| 90 | +| `onCollide` | function | Callback function when collision occurs | |
| 91 | +| `onCollideBegin` | function | Callback function when collision begins | |
| 92 | +| `onCollideEnd` | function | Callback function when collision ends | |
| 93 | + |
| 94 | +## Advanced Usage |
| 95 | + |
| 96 | +You can dynamically update body properties using the returned API: |
| 97 | + |
| 98 | +```angular-ts |
| 99 | +import { Component, ElementRef, viewChild, signal } from '@angular/core'; |
| 100 | +import { injectBox } from 'angular-three-cannon/body'; |
| 101 | +import { NgtMesh } from 'angular-three'; |
| 102 | +
|
| 103 | +@Component({ |
| 104 | + selector: 'app-physics-box', |
| 105 | + standalone: true, |
| 106 | + template: ` |
| 107 | + <ngt-mesh #mesh> |
| 108 | + <ngt-box-geometry /> |
| 109 | + <ngt-mesh-standard-material /> |
| 110 | + </ngt-mesh> |
| 111 | + <button (click)="jump()">Jump</button> |
| 112 | + `, |
| 113 | +}) |
| 114 | +export class PhysicsBox { |
| 115 | + mesh = viewChild.required<ElementRef<THREE.Mesh>>('mesh'); |
| 116 | +
|
| 117 | + boxBody = injectBox( |
| 118 | + () => ({ |
| 119 | + mass: 1, |
| 120 | + position: [0, 5, 0], |
| 121 | + args: [1, 1, 1], |
| 122 | + }), |
| 123 | + this.mesh |
| 124 | + ); |
| 125 | +
|
| 126 | + jump() { |
| 127 | + const api = this.boxBody(); |
| 128 | + if (api) { |
| 129 | + api.applyImpulse([0, 5, 0], [0, 0, 0]); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +This example shows how to apply an impulse to make the box "jump" when a button is clicked. |
0 commit comments