@@ -30,7 +30,7 @@ import {
3030 NgtInjectedRef ,
3131 assertInjectionContext ,
3232 injectNgtRef ,
33- queueMicrotaskInInjectionContext ,
33+ requestAnimationFrameInInjectionContext ,
3434} from 'angular-three' ;
3535import { NGTC_PHYSICS_API , NgtcCannonEvents } from 'angular-three-cannon' ;
3636import { NGTC_DEBUG_API } from 'angular-three-cannon/debug' ;
@@ -249,16 +249,17 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
249249 const debugApi = inject ( NGTC_DEBUG_API , { optional : true } ) ;
250250
251251 const { add : debugAdd , remove : debugRemove } = debugApi ?.( ) || { } ;
252- const { refs, worker, subscriptions, scaleOverrides, events } = physicsApi ( ) ;
252+ const { refs, worker, subscriptions, scaleOverrides, events, bodies } = physicsApi ( ) ;
253253
254- queueMicrotaskInInjectionContext ( ( ) => {
254+ requestAnimationFrameInInjectionContext ( ( ) => {
255255 effect (
256256 ( onCleanup ) => {
257257 // register deps
258258 deps ( ) ;
259259
260260 if ( ! bodyRef . nativeElement ) {
261261 bodyRef . nativeElement = new THREE . Object3D ( ) as TObject ;
262+ return ;
262263 }
263264
264265 const object = bodyRef . nativeElement ;
@@ -339,11 +340,11 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
339340 return {
340341 copy : ( { w, x, y, z } : THREE . Quaternion ) => {
341342 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 } ) ;
343344 } ,
344345 set : ( x : number , y : number , z : number , w : number ) => {
345346 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 } ) ;
347348 } ,
348349 subscribe : subscribe ( bodyRef , worker ( ) , subscriptions , type , index ) ,
349350 } ;
@@ -353,11 +354,11 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
353354 return {
354355 copy : ( { x, y, z } : THREE . Vector3 | THREE . Euler ) => {
355356 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 } ) ;
357358 } ,
358359 set : ( x : number , y : number , z : number ) => {
359360 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 } ) ;
361362 } ,
362363 subscribe : ( callback : ( value : Triplet ) => void ) => {
363364 const id = incrementingId ++ ;
@@ -366,7 +367,7 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
366367 const uuid = getUUID ( bodyRef , index ) ;
367368
368369 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 } ) ;
370371 return ( ) => {
371372 delete subscriptions [ id ] ;
372373 worker ( ) . unsubscribe ( { props : id } ) ;
@@ -380,11 +381,11 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
380381 return {
381382 copy : ( { x, y, z } : THREE . Vector3 | THREE . Euler ) => {
382383 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 } ) ;
384385 } ,
385386 set : ( x : number , y : number , z : number ) => {
386387 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 } ) ;
388389 } ,
389390 subscribe : subscribe ( bodyRef , worker ( ) , subscriptions , type , index ) ,
390391 } ;
@@ -405,23 +406,25 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
405406 angularVelocity : makeVec ( 'angularVelocity' , index ) ,
406407 applyForce ( force : Triplet , worldPoint : Triplet ) {
407408 const uuid = getUUID ( bodyRef , index ) ;
408- uuid && worker ( ) . applyForce ( { props : [ force , worldPoint ] , uuid } ) ;
409+ uuid && bodies [ uuid ] != null && worker ( ) . applyForce ( { props : [ force , worldPoint ] , uuid } ) ;
409410 } ,
410411 applyImpulse ( impulse : Triplet , worldPoint : Triplet ) {
411412 const uuid = getUUID ( bodyRef , index ) ;
412- uuid && worker ( ) . applyImpulse ( { props : [ impulse , worldPoint ] , uuid } ) ;
413+ uuid && bodies [ uuid ] != null && worker ( ) . applyImpulse ( { props : [ impulse , worldPoint ] , uuid } ) ;
413414 } ,
414415 applyLocalForce ( force : Triplet , localPoint : Triplet ) {
415416 const uuid = getUUID ( bodyRef , index ) ;
416- uuid && worker ( ) . applyLocalForce ( { props : [ force , localPoint ] , uuid } ) ;
417+ uuid && bodies [ uuid ] != null && worker ( ) . applyLocalForce ( { props : [ force , localPoint ] , uuid } ) ;
417418 } ,
418419 applyLocalImpulse ( impulse : Triplet , localPoint : Triplet ) {
419420 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 } ) ;
421424 } ,
422425 applyTorque ( torque : Triplet ) {
423426 const uuid = getUUID ( bodyRef , index ) ;
424- uuid && worker ( ) . applyTorque ( { props : [ torque ] , uuid } ) ;
427+ uuid && bodies [ uuid ] != null && worker ( ) . applyTorque ( { props : [ torque ] , uuid } ) ;
425428 } ,
426429 collisionFilterGroup : makeAtomic ( 'collisionFilterGroup' , index ) ,
427430 collisionFilterMask : makeAtomic ( 'collisionFilterMask' , index ) ,
@@ -441,7 +444,7 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
441444 } ,
442445 sleep ( ) {
443446 const uuid = getUUID ( bodyRef , index ) ;
444- uuid && worker ( ) . sleep ( { uuid } ) ;
447+ uuid && bodies [ uuid ] != null && worker ( ) . sleep ( { uuid } ) ;
445448 } ,
446449 sleepSpeedLimit : makeAtomic ( 'sleepSpeedLimit' , index ) ,
447450 sleepTimeLimit : makeAtomic ( 'sleepTimeLimit' , index ) ,
@@ -450,7 +453,7 @@ function injectBody<TBodyProps extends BodyProps, TObject extends THREE.Object3D
450453 remove : makeRemove ( index ) ,
451454 wakeUp ( ) {
452455 const uuid = getUUID ( bodyRef , index ) ;
453- uuid && worker ( ) . wakeUp ( { uuid } ) ;
456+ uuid && bodies [ uuid ] != null && worker ( ) . wakeUp ( { uuid } ) ;
454457 } ,
455458 } ;
456459 }
0 commit comments