|
| 1 | +import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, viewChild } from '@angular/core'; |
| 2 | +import { injectBeforeRender } from 'angular-three'; |
| 3 | +import { NgtrRigidBody } from 'angular-three-rapier'; |
| 4 | +import * as THREE from 'three'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'app-ball', |
| 8 | + template: ` |
| 9 | + <ngt-object3D rigidBody [options]="{ colliders: 'ball' }"> |
| 10 | + <ngt-mesh castShadow receiveShadow> |
| 11 | + <ngt-sphere-geometry /> |
| 12 | + <ngt-mesh-physical-material color="red" /> |
| 13 | + </ngt-mesh> |
| 14 | + </ngt-object3D> |
| 15 | + `, |
| 16 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 17 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 18 | + imports: [NgtrRigidBody], |
| 19 | +}) |
| 20 | +export class Ball { |
| 21 | + private rigidBodyRef = viewChild.required(NgtrRigidBody); |
| 22 | + |
| 23 | + constructor() { |
| 24 | + injectBeforeRender(() => { |
| 25 | + const rigidBody = this.rigidBodyRef().rigidBody(); |
| 26 | + if (!rigidBody) return; |
| 27 | + if (rigidBody.translation().y < -10) { |
| 28 | + rigidBody.setTranslation({ x: Math.random() * 2, y: 20, z: 0 }, true); |
| 29 | + rigidBody.setLinvel({ x: 0, y: 0, z: 0 }, true); |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +@Component({ |
| 36 | + selector: 'app-kinematics-rapier', |
| 37 | + template: ` |
| 38 | + <ngt-group> |
| 39 | + <app-ball /> |
| 40 | + <app-ball /> |
| 41 | + <app-ball /> |
| 42 | + <app-ball /> |
| 43 | + <app-ball /> |
| 44 | +
|
| 45 | + <ngt-object3D |
| 46 | + #torus="rigidBody" |
| 47 | + rigidBody="kinematicPosition" |
| 48 | + [options]="{ colliders: 'trimesh', restitution: 1 }" |
| 49 | + [position]="[0, 2, 0]" |
| 50 | + > |
| 51 | + <ngt-mesh castShadow receiveShadow [scale]="5"> |
| 52 | + <ngt-torus-geometry /> |
| 53 | + <ngt-mesh-physical-material /> |
| 54 | + </ngt-mesh> |
| 55 | + </ngt-object3D> |
| 56 | +
|
| 57 | + <ngt-object3D |
| 58 | + #platform="rigidBody" |
| 59 | + rigidBody="kinematicPosition" |
| 60 | + [options]="{ colliders: 'cuboid' }" |
| 61 | + [position]="[0, -8, 0]" |
| 62 | + > |
| 63 | + <ngt-mesh castShadow receiveShadow [geometry]="boxGeometry"> |
| 64 | + <!-- TODO: rigidBody does not work with *args--> |
| 65 | + <!-- <ngt-box-geometry *args="[40, 1, 40]" />--> |
| 66 | + <ngt-mesh-physical-material /> |
| 67 | + </ngt-mesh> |
| 68 | + </ngt-object3D> |
| 69 | + </ngt-group> |
| 70 | + `, |
| 71 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 72 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 73 | + imports: [Ball, NgtrRigidBody], |
| 74 | +}) |
| 75 | +export default class KinematicsExample { |
| 76 | + protected boxGeometry = new THREE.BoxGeometry(40, 1, 40); |
| 77 | + |
| 78 | + private torusRef = viewChild.required('torus', { read: NgtrRigidBody }); |
| 79 | + private platformRef = viewChild.required('platform', { read: NgtrRigidBody }); |
| 80 | + |
| 81 | + constructor() { |
| 82 | + injectBeforeRender(() => { |
| 83 | + const now = performance.now(); |
| 84 | + |
| 85 | + const torus = this.torusRef().rigidBody(); |
| 86 | + if (torus) { |
| 87 | + const euler = new THREE.Euler(now / 1000, 0, 0); |
| 88 | + torus.setNextKinematicRotation(new THREE.Quaternion().setFromEuler(euler)); |
| 89 | + } |
| 90 | + |
| 91 | + const platform = this.platformRef().rigidBody(); |
| 92 | + if (platform) { |
| 93 | + platform.setNextKinematicTranslation({ |
| 94 | + x: Math.sin(now / 100), |
| 95 | + y: -8 + Math.sin(now / 50) * 0.5, |
| 96 | + z: 0, |
| 97 | + }); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
0 commit comments