What do you think of this? Is there a better method?
I move meshes manually all the time with physics enabled. The advice on the forums is consistent:
disable disablePrestep on the physics body right before you want to change a transform on the transformNode. If it is a one time assignment, enable it again in the next frame or so (performance).
Keeping track of which meshes need to re-disable PreStep after the current frame isn’t straightforward. How about something like this?
const disablePreStepArray = Array(); // make sure this is available to functions below function physicsBodyEnableUpdateTransform(body,updateCallback) { body.disablePreStep = false; if(updateCallback) updateCallback(body) disablePreStepArray.push(body); if (disablePreStepArray.length == 1) { // add observer once scene.onAfterPhysicsObservable.addOnce(disablePreStepObserver); // need access to scene } } function disablePreStepObserver () { if (disablePreStepArray.length){ disablePreStepArray.forEach((body)=>body.disablePreStep=true); disablePreStepArray.length = 0; } }
So instead of taking the performance hit of disabled PreStep all the time
mesh.physicsBody.disablePreStep = true;
Do this instead:
physicsBodyUpdateTransform(mesh.physicsBody, (body)=>{body.transformNode.position.y+=10});
Then the functions above take care of setting disablePreStep back to true, one time, and completely remove the observable to get back any performance impact.
Edit: IMPORTANT OBSERVATION
Since I originally wrote this, I’ve noticed an issue with timing. The original code here enabled prestep and immediately added to the array. This could result in the observer setting disable prior to your code updating the transform. The callback added in this edit (should) solve this.