@@ -128,6 +128,8 @@ export class World {
128
128
/** @internal */ m_positionIterations : number ;
129
129
/** @internal */ m_t : number ;
130
130
131
+ /** @internal */ m_step_callback : ( ( world : World ) => unknown ) [ ] = [ ] ;
132
+
131
133
// TODO
132
134
/** @internal */ _listeners : {
133
135
[ key : string ] : any [ ]
@@ -469,10 +471,12 @@ export class World {
469
471
* position -= newOrigin
470
472
*
471
473
* @param newOrigin The new origin with respect to the old origin
474
+ *
475
+ * Warning: This function is locked when a world simulation step is in progress. Use queueUpdate to schedule a function to be called after the step.
472
476
*/
473
477
shiftOrigin ( newOrigin : Vec2Value ) : void {
474
- if ( _ASSERT ) console . assert ( this . m_locked == false ) ;
475
- if ( this . m_locked ) {
478
+ if ( _ASSERT ) console . assert ( this . isLocked ( ) == false ) ;
479
+ if ( this . isLocked ( ) ) {
476
480
return ;
477
481
}
478
482
@@ -510,7 +514,7 @@ export class World {
510
514
* Create a rigid body given a definition. No reference to the definition is
511
515
* retained.
512
516
*
513
- * Warning: This function is locked during callbacks .
517
+ * Warning: This function is locked when a world simulation step is in progress. Use queueUpdate to schedule a function to be called after the step .
514
518
*/
515
519
createBody ( def ?: BodyDef ) : Body ;
516
520
createBody ( position : Vec2Value , angle ?: number ) : Body ;
@@ -565,12 +569,11 @@ export class World {
565
569
}
566
570
567
571
/**
568
- * Destroy a rigid body given a definition. No reference to the definition is
569
- * retained.
572
+ * Destroy a body from the world.
570
573
*
571
574
* Warning: This automatically deletes all associated shapes and joints.
572
575
*
573
- * Warning: This function is locked during callbacks .
576
+ * Warning: This function is locked when a world simulation step is in progress. Use queueUpdate to schedule a function to be called after the step .
574
577
*/
575
578
destroyBody ( b : Body ) : boolean {
576
579
if ( _ASSERT ) console . assert ( this . m_bodyCount > 0 ) ;
@@ -647,7 +650,7 @@ export class World {
647
650
* Create a joint to constrain bodies together. No reference to the definition
648
651
* is retained. This may cause the connected bodies to cease colliding.
649
652
*
650
- * Warning: This function is locked during callbacks .
653
+ * Warning: This function is locked when a world simulation step is in progress. Use queueUpdate to schedule a function to be called after the step .
651
654
*/
652
655
createJoint < T extends Joint > ( joint : T ) : T | null {
653
656
if ( _ASSERT ) console . assert ( ! ! joint . m_bodyA ) ;
@@ -700,8 +703,11 @@ export class World {
700
703
}
701
704
702
705
/**
703
- * Destroy a joint. This may cause the connected bodies to begin colliding.
704
- * Warning: This function is locked during callbacks.
706
+ * Destroy a joint.
707
+ *
708
+ * Warning: This may cause the connected bodies to begin colliding.
709
+ *
710
+ * Warning: This function is locked when a world simulation step is in progress. Use queueUpdate to schedule a function to be called after the step.
705
711
*/
706
712
destroyJoint ( joint : Joint ) : void {
707
713
if ( _ASSERT ) console . assert ( this . isLocked ( ) == false ) ;
@@ -854,9 +860,25 @@ export class World {
854
860
855
861
this . m_locked = false ;
856
862
863
+ let callback : ( world : World ) => unknown ;
864
+ while ( callback = this . m_step_callback . pop ( ) ) {
865
+ callback ( this ) ;
866
+ }
867
+
857
868
this . publish ( "post-step" , timeStep ) ;
858
869
}
859
870
871
+ /**
872
+ * Queue a function to be called after ongoing simulation step. If no simulation is in progress call it immediately.
873
+ */
874
+ queueUpdate ( callback : ( world : World ) => unknown ) : void {
875
+ if ( ! this . isLocked ( ) ) {
876
+ callback ( this ) ;
877
+ } else {
878
+ this . m_step_callback . push ( callback ) ;
879
+ }
880
+ }
881
+
860
882
/**
861
883
* @internal
862
884
* Call this method to find new contacts.
0 commit comments