Skip to content

Commit f8973c4

Browse files
committed
fix(cannon): clean up physics
1 parent 824ef24 commit f8973c4

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

libs/cannon/body/src/lib/body.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
4848
const debug = inject(NgtcDebug, { optional: true });
4949

5050
const transform = transformArgs ?? defaultTransformArgs[type];
51-
const bodyRef = isSignal(ref) ? ref : signal(ref);
51+
const isRefSignal = isSignal(ref);
52+
const bodyRef = (isRefSignal ? ref : signal(ref)) as WritableSignal<TObject | undefined>;
5253
const body = computed(() => resolveRef(bodyRef()));
5354

5455
const api = computed(() => {
5556
const _body = body();
5657
if (!_body) return null;
58+
5759
const { worker, ...rest } = physics.api;
58-
if (!worker()) return null;
59-
return makeBodyApi(_body, worker(), rest);
60+
const _worker = worker();
61+
if (!_worker) return null;
62+
63+
return makeBodyApi(_body, _worker, rest);
6064
});
6165

6266
effect((onCleanup) => {
@@ -65,9 +69,10 @@ function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
6569

6670
const object = body();
6771

68-
if (!isSignal(ref) && !object) {
72+
if (!isRefSignal && !object) {
73+
// TODO (signal): find a better way to handle this. no setting signal
6974
untracked(() => {
70-
(bodyRef as WritableSignal<TObject | undefined>).set(resolveRef(ref));
75+
bodyRef.set(resolveRef(ref));
7176
});
7277
return;
7378
}
@@ -103,6 +108,7 @@ function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
103108
}),
104109
];
105110
})();
111+
106112
// Register on mount, unregister on unmount
107113
currentWorker.addBodies({
108114
props: props.map(({ onCollide, onCollideBegin, onCollideEnd, ...serializableProps }) => {

libs/cannon/debug/src/lib/debug.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export class NgtcDebug {
4848

4949
private cannonDebugger!: ReturnType<typeof CannonDebugger>;
5050

51-
api = {};
52-
5351
constructor() {
5452
afterNextRender(() => {
5553
this.defaultScene().add(this.scene);
@@ -61,6 +59,8 @@ export class NgtcDebug {
6159

6260
injectBeforeRender(() => {
6361
if (!this.cannonDebugger) return;
62+
63+
const enabled = this.debug().enabled;
6464
const refs = this.physics.api.refs;
6565
for (const uuid in this.bodyMap) {
6666
const ref = refs[uuid];
@@ -71,13 +71,9 @@ export class NgtcDebug {
7171
body.quaternion.copy(q as unknown as CQuarternion);
7272
}
7373
}
74-
for (const child of this.scene.children) {
75-
child.visible = this.debug().enabled;
76-
}
7774

78-
if (this.debug().enabled) {
79-
this.cannonDebugger.update();
80-
}
75+
for (const child of this.scene.children) child.visible = enabled;
76+
if (enabled) this.cannonDebugger.update();
8177
});
8278
}
8379

libs/cannon/src/lib/physics.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import {
22
ChangeDetectionStrategy,
33
Component,
4-
DestroyRef,
5-
EmbeddedViewRef,
64
Signal,
75
afterNextRender,
86
effect,
9-
inject,
107
input,
118
signal,
129
untracked,
@@ -152,8 +149,6 @@ export class NgtcPhysics {
152149
worker: this.worker.asReadonly(),
153150
};
154151

155-
private ref?: EmbeddedViewRef<{ $implicit: NgtcPhysicsApi }>;
156-
157152
constructor() {
158153
afterNextRender(() => {
159154
this.worker.set(new CannonWorkerAPI(this.options()));
@@ -180,10 +175,6 @@ export class NgtcPhysics {
180175
worker.step({ maxSubSteps, stepSize, timeSinceLastCalled });
181176
timeSinceLastCalled = 0;
182177
});
183-
184-
inject(DestroyRef).onDestroy(() => {
185-
this.ref?.destroy();
186-
});
187178
}
188179

189180
private connectWorkerEffect() {

0 commit comments

Comments
 (0)