Skip to content

Commit a4f0e43

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
feat(cannon): migrate spring
1 parent dcc5ad5 commit a4f0e43

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

libs/cannon/services/src/constraint.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
HingeConstraintOpts,
66
PointToPointConstraintOpts,
77
} from '@pmndrs/cannon-worker-api';
8-
import { NgtAnyRecord, NgtInjectedRef, assertInjectionContext, injectNgtRef, is } from 'angular-three';
8+
import { NgtAnyRecord, NgtInjectedRef, assertInjectionContext, injectNgtRef, is, makeId } from 'angular-three';
99
import { NGTC_PHYSICS_API } from 'angular-three-cannon';
1010
import * as THREE from 'three';
1111

@@ -105,14 +105,14 @@ function injectConstraint<
105105
const physicsApi = inject(NGTC_PHYSICS_API);
106106
const { worker } = physicsApi();
107107

108-
const uuid = THREE.MathUtils.generateUUID();
108+
const uuid = makeId();
109109

110110
const bodyARef = is.ref(bodyA) ? bodyA : injectNgtRef(bodyA);
111111
const bodyBRef = is.ref(bodyB) ? bodyB : injectNgtRef(bodyB);
112112

113113
effect((onCleanup) => {
114114
deps();
115-
if (bodyARef.untracked && bodyBRef.untracked) {
115+
if (bodyARef.nativeElement && bodyBRef.nativeElement) {
116116
worker.addConstraint({
117117
props: [bodyARef.untracked.uuid, bodyBRef.untracked.uuid, untracked(opts)],
118118
type,

libs/cannon/services/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './body';
22
export * from './constraint';
3+
export * from './spring';

libs/cannon/services/src/spring.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Injector, Signal, computed, effect, inject, runInInjectionContext, untracked } from '@angular/core';
2+
import { SpringOptns } from '@pmndrs/cannon-worker-api';
3+
import { NgtAnyRecord, NgtInjectedRef, assertInjectionContext, injectNgtRef, is, makeId } from 'angular-three';
4+
import { NGTC_PHYSICS_API } from 'angular-three-cannon';
5+
6+
export interface NgtcSpringApi {
7+
setDamping: (value: number) => void;
8+
setRestLength: (value: number) => void;
9+
setStiffness: (value: number) => void;
10+
remove: () => void;
11+
}
12+
13+
export interface NgtcSpringReturn<
14+
TObjectA extends THREE.Object3D = THREE.Object3D,
15+
TObjectB extends THREE.Object3D = THREE.Object3D
16+
> {
17+
bodyA: NgtInjectedRef<TObjectA>;
18+
bodyB: NgtInjectedRef<TObjectB>;
19+
api: Signal<NgtcSpringApi>;
20+
}
21+
22+
export function injectSpring<A extends THREE.Object3D, B extends THREE.Object3D>(
23+
bodyA: NgtInjectedRef<A> | A,
24+
bodyB: NgtInjectedRef<B> | B,
25+
{
26+
injector,
27+
opts = () => ({}),
28+
deps = () => ({}),
29+
}: { injector?: Injector; deps?: () => NgtAnyRecord; opts?: () => SpringOptns } = {}
30+
): NgtcSpringReturn<A, B> {
31+
injector = assertInjectionContext(injectSpring, injector);
32+
return runInInjectionContext(injector, () => {
33+
const physicsApi = inject(NGTC_PHYSICS_API);
34+
const { worker } = physicsApi();
35+
36+
const uuid = makeId();
37+
38+
const bodyARef = is.ref(bodyA) ? bodyA : injectNgtRef(bodyA);
39+
const bodyBRef = is.ref(bodyB) ? bodyB : injectNgtRef(bodyB);
40+
41+
effect((onCleanup) => {
42+
deps();
43+
if (bodyARef.nativeElement && bodyBRef.nativeElement) {
44+
worker.addSpring({
45+
props: [bodyARef.nativeElement.uuid, bodyBRef.nativeElement.uuid, untracked(opts)],
46+
uuid,
47+
});
48+
onCleanup(() => worker.removeSpring({ uuid }));
49+
}
50+
});
51+
52+
const api = computed(() => {
53+
deps();
54+
return {
55+
setDamping: (value: number) => worker.setSpringDamping({ props: value, uuid }),
56+
setRestLength: (value: number) => worker.setSpringRestLength({ props: value, uuid }),
57+
setStiffness: (value: number) => worker.setSpringStiffness({ props: value, uuid }),
58+
remove: () => worker.removeSpring({ uuid }),
59+
};
60+
});
61+
62+
return { bodyA: bodyARef, bodyB: bodyBRef, api };
63+
});
64+
}

0 commit comments

Comments
 (0)