[SOLVED] RigidBodyComponent's syncEntityToBody has no effect

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.

So thankfully it turned out to be a Chrome dev tools issue and the actual code is working fine. It seems that when you debug code via source maps, the Chrome dev tools may initialize certain variables a second time just for the source maps (no idea why).

In my case, I was debugging the engine (playcanvas-1.71.6.dbg.js) through the source map rigid-body/component.js. The shared variable _ammoTransform was initialized once in the engine and once in the source map. When debugging, the dev tools erroneously show the _ammoTransform from the source map when it should be showing the one from the engine. If you debug the original source code directly, everything works as expected.

1 Like