Hey everyone,
I noticed a strange problem and was wondering if anyone has any clue as to what’s going on here.
The setup: I’m pooling some entities that each have a RigidBodyComponent
. When I fetch an entity from the pool and then enable it, the entity shows at its previous position (the position it had prior to being returned to the pool) for just one frame. After some digging, I found that the culprit is RigidBodyComponent.syncEntityToBody
which is called by RigidBodyComponent.enableSimulation
when the entity is fetched from the pool and re-enabled.
Here’s a recording of the issue:
The syncEntityToBody
-method calls _getEntityTransform
with the shared variable _ammoTransform
as the argument. Somehow, _getEntityTransform
appears to receive a copy of this transform rather than a reference. As a result, when _getEntityTransform
returns, the _ammoTransform
in syncEntityToBody
remains unchanged. The entity will remain in the wrong position until the rigid body is moved in the next frame and the correct position is applied to the entity via _updateDynamic
.
Is it somehow possible that the _ammoTransform
is passed by value to _getEntityTransform
? I’ve never seen objects being passed by value in JavaScript, so I’m guessing maybe some Enscripten wrapper magic is going on here. Another weird observation: if I manually invoke _getEntityTransform
from the console within syncEntityToBody
, this problem does not occur and everything works as expected. Could there be some JavaScript optimization under the hood that’s messing things up?
Thanks for the help.