@@ -30,7 +30,7 @@ import {
30
30
NgtInjectedRef ,
31
31
assertInjectionContext ,
32
32
injectNgtRef ,
33
- queueMicrotaskInInjectionContext ,
33
+ requestAnimationFrameInInjectionContext ,
34
34
} from 'angular-three' ;
35
35
import { NGTC_PHYSICS_API , NgtcCannonEvents } from 'angular-three-cannon' ;
36
36
import { NGTC_DEBUG_API } from 'angular-three-cannon/debug' ;
@@ -249,16 +249,17 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
249
249
const debugApi = inject ( NGTC_DEBUG_API , { optional : true } ) ;
250
250
251
251
const { add : debugAdd , remove : debugRemove } = debugApi ?.( ) || { } ;
252
- const { refs, worker, subscriptions, scaleOverrides, events } = physicsApi ( ) ;
252
+ const { refs, worker, subscriptions, scaleOverrides, events, bodies } = physicsApi ( ) ;
253
253
254
- queueMicrotaskInInjectionContext ( ( ) => {
254
+ requestAnimationFrameInInjectionContext ( ( ) => {
255
255
effect (
256
256
( onCleanup ) => {
257
257
// register deps
258
258
deps ( ) ;
259
259
260
260
if ( ! bodyRef . nativeElement ) {
261
261
bodyRef . nativeElement = new THREE . Object3D ( ) as TObject ;
262
+ return ;
262
263
}
263
264
264
265
const object = bodyRef . nativeElement ;
@@ -339,11 +340,11 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
339
340
return {
340
341
copy : ( { w, x, y, z } : THREE . Quaternion ) => {
341
342
const uuid = getUUID ( bodyRef , index ) ;
342
- uuid && worker ( ) . setQuaternion ( { props : [ x , y , z , w ] , uuid } ) ;
343
+ uuid && bodies [ uuid ] != null && worker ( ) . setQuaternion ( { props : [ x , y , z , w ] , uuid } ) ;
343
344
} ,
344
345
set : ( x : number , y : number , z : number , w : number ) => {
345
346
const uuid = getUUID ( bodyRef , index ) ;
346
- uuid && worker ( ) . setQuaternion ( { props : [ x , y , z , w ] , uuid } ) ;
347
+ uuid && bodies [ uuid ] != null && worker ( ) . setQuaternion ( { props : [ x , y , z , w ] , uuid } ) ;
347
348
} ,
348
349
subscribe : subscribe ( bodyRef , worker ( ) , subscriptions , type , index ) ,
349
350
} ;
@@ -353,11 +354,11 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
353
354
return {
354
355
copy : ( { x, y, z } : THREE . Vector3 | THREE . Euler ) => {
355
356
const uuid = getUUID ( bodyRef , index ) ;
356
- uuid && worker ( ) . setRotation ( { props : [ x , y , z ] , uuid } ) ;
357
+ uuid && bodies [ uuid ] != null && worker ( ) . setRotation ( { props : [ x , y , z ] , uuid } ) ;
357
358
} ,
358
359
set : ( x : number , y : number , z : number ) => {
359
360
const uuid = getUUID ( bodyRef , index ) ;
360
- uuid && worker ( ) . setRotation ( { props : [ x , y , z ] , uuid } ) ;
361
+ uuid && bodies [ uuid ] != null && worker ( ) . setRotation ( { props : [ x , y , z ] , uuid } ) ;
361
362
} ,
362
363
subscribe : ( callback : ( value : Triplet ) => void ) => {
363
364
const id = incrementingId ++ ;
@@ -366,7 +367,7 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
366
367
const uuid = getUUID ( bodyRef , index ) ;
367
368
368
369
subscriptions [ id ] = { [ type ] : quaternionToRotation ( callback ) } ;
369
- uuid && worker ( ) . subscribe ( { props : { id, target, type } , uuid } ) ;
370
+ uuid && bodies [ uuid ] != null && worker ( ) . subscribe ( { props : { id, target, type } , uuid } ) ;
370
371
return ( ) => {
371
372
delete subscriptions [ id ] ;
372
373
worker ( ) . unsubscribe ( { props : id } ) ;
@@ -380,11 +381,11 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
380
381
return {
381
382
copy : ( { x, y, z } : THREE . Vector3 | THREE . Euler ) => {
382
383
const uuid = getUUID ( bodyRef , index ) ;
383
- uuid && worker ( ) [ op ] ( { props : [ x , y , z ] , uuid } ) ;
384
+ uuid && bodies [ uuid ] != null && worker ( ) [ op ] ( { props : [ x , y , z ] , uuid } ) ;
384
385
} ,
385
386
set : ( x : number , y : number , z : number ) => {
386
387
const uuid = getUUID ( bodyRef , index ) ;
387
- uuid && worker ( ) [ op ] ( { props : [ x , y , z ] , uuid } ) ;
388
+ uuid && bodies [ uuid ] != null && worker ( ) [ op ] ( { props : [ x , y , z ] , uuid } ) ;
388
389
} ,
389
390
subscribe : subscribe ( bodyRef , worker ( ) , subscriptions , type , index ) ,
390
391
} ;
@@ -405,23 +406,25 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
405
406
angularVelocity : makeVec ( 'angularVelocity' , index ) ,
406
407
applyForce ( force : Triplet , worldPoint : Triplet ) {
407
408
const uuid = getUUID ( bodyRef , index ) ;
408
- uuid && worker ( ) . applyForce ( { props : [ force , worldPoint ] , uuid } ) ;
409
+ uuid && bodies [ uuid ] != null && worker ( ) . applyForce ( { props : [ force , worldPoint ] , uuid } ) ;
409
410
} ,
410
411
applyImpulse ( impulse : Triplet , worldPoint : Triplet ) {
411
412
const uuid = getUUID ( bodyRef , index ) ;
412
- uuid && worker ( ) . applyImpulse ( { props : [ impulse , worldPoint ] , uuid } ) ;
413
+ uuid && bodies [ uuid ] != null && worker ( ) . applyImpulse ( { props : [ impulse , worldPoint ] , uuid } ) ;
413
414
} ,
414
415
applyLocalForce ( force : Triplet , localPoint : Triplet ) {
415
416
const uuid = getUUID ( bodyRef , index ) ;
416
- uuid && worker ( ) . applyLocalForce ( { props : [ force , localPoint ] , uuid } ) ;
417
+ uuid && bodies [ uuid ] != null && worker ( ) . applyLocalForce ( { props : [ force , localPoint ] , uuid } ) ;
417
418
} ,
418
419
applyLocalImpulse ( impulse : Triplet , localPoint : Triplet ) {
419
420
const uuid = getUUID ( bodyRef , index ) ;
420
- uuid && worker ( ) . applyLocalImpulse ( { props : [ impulse , localPoint ] , uuid } ) ;
421
+ uuid &&
422
+ bodies [ uuid ] != null &&
423
+ worker ( ) . applyLocalImpulse ( { props : [ impulse , localPoint ] , uuid } ) ;
421
424
} ,
422
425
applyTorque ( torque : Triplet ) {
423
426
const uuid = getUUID ( bodyRef , index ) ;
424
- uuid && worker ( ) . applyTorque ( { props : [ torque ] , uuid } ) ;
427
+ uuid && bodies [ uuid ] != null && worker ( ) . applyTorque ( { props : [ torque ] , uuid } ) ;
425
428
} ,
426
429
collisionFilterGroup : makeAtomic ( 'collisionFilterGroup' , index ) ,
427
430
collisionFilterMask : makeAtomic ( 'collisionFilterMask' , index ) ,
@@ -441,7 +444,7 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
441
444
} ,
442
445
sleep ( ) {
443
446
const uuid = getUUID ( bodyRef , index ) ;
444
- uuid && worker ( ) . sleep ( { uuid } ) ;
447
+ uuid && bodies [ uuid ] != null && worker ( ) . sleep ( { uuid } ) ;
445
448
} ,
446
449
sleepSpeedLimit : makeAtomic ( 'sleepSpeedLimit' , index ) ,
447
450
sleepTimeLimit : makeAtomic ( 'sleepTimeLimit' , index ) ,
@@ -450,7 +453,7 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
450
453
remove : makeRemove ( index ) ,
451
454
wakeUp ( ) {
452
455
const uuid = getUUID ( bodyRef , index ) ;
453
- uuid && worker ( ) . wakeUp ( { uuid } ) ;
456
+ uuid && bodies [ uuid ] != null && worker ( ) . wakeUp ( { uuid } ) ;
454
457
} ,
455
458
} ;
456
459
}
0 commit comments