|
1 | | -import { |
2 | | - afterNextRender, |
3 | | - ChangeDetectionStrategy, |
4 | | - Component, |
5 | | - computed, |
6 | | - CUSTOM_ELEMENTS_SCHEMA, |
7 | | - inject, |
8 | | - Injector, |
9 | | - input, |
10 | | - viewChild, |
11 | | -} from '@angular/core'; |
| 1 | +import { ChangeDetectionStrategy, Component, computed, CUSTOM_ELEMENTS_SCHEMA, input, viewChild } from '@angular/core'; |
12 | 2 | import { NgtArgs, NgtVector3, vector3 } from 'angular-three'; |
13 | 3 | import { injectSpringJoint, NgtrBallCollider, NgtrRigidBody } from 'angular-three-rapier'; |
14 | 4 | import { ColorRepresentation } from 'three'; |
@@ -64,27 +54,22 @@ export class BallSpring { |
64 | 54 | private stiffness = 1.0e3; |
65 | 55 |
|
66 | 56 | constructor() { |
67 | | - const injector = inject(Injector); |
| 57 | + const floorBody = computed(() => this.floorRigidBody().rigidBody()); |
| 58 | + const ballBody = computed(() => this.ballBody().rigidBody()); |
68 | 59 |
|
69 | | - afterNextRender(() => { |
70 | | - const floorBody = computed(() => this.floorRigidBody().rigidBody()); |
71 | | - const ballBody = computed(() => this.ballBody().rigidBody()); |
| 60 | + const criticalDamping = computed(() => 2 * Math.sqrt(this.stiffness * this.mass())); |
| 61 | + const dampingRatio = computed(() => this.jointNum() / (this.total() / 2)); |
| 62 | + const damping = computed(() => dampingRatio() * criticalDamping()); |
| 63 | + const positionVector = vector3(this.position); |
72 | 64 |
|
73 | | - const criticalDamping = computed(() => 2 * Math.sqrt(this.stiffness * this.mass())); |
74 | | - const dampingRatio = computed(() => this.jointNum() / (this.total() / 2)); |
75 | | - const damping = computed(() => dampingRatio() * criticalDamping()); |
76 | | - const positionVector = vector3(this.position); |
77 | | - |
78 | | - injectSpringJoint(ballBody, floorBody, { |
79 | | - injector, |
80 | | - data: { |
81 | | - body1Anchor: [0, 0, 0], |
82 | | - body2Anchor: [positionVector().x, positionVector().y - 3, positionVector().z], |
83 | | - restLength: 0, |
84 | | - stiffness: this.stiffness, |
85 | | - damping: damping(), |
86 | | - }, |
87 | | - }); |
| 65 | + injectSpringJoint(ballBody, floorBody, { |
| 66 | + data: () => ({ |
| 67 | + body1Anchor: [0, 0, 0], |
| 68 | + body2Anchor: [positionVector().x, positionVector().y - 3, positionVector().z], |
| 69 | + restLength: 0, |
| 70 | + stiffness: this.stiffness, |
| 71 | + damping: damping(), |
| 72 | + }), |
88 | 73 | }); |
89 | 74 | } |
90 | 75 | } |
|
0 commit comments