Performance Tips
♻️ Reduce, Reuse, Recycle ♻️
The main point to take advantage of when using the API is to reuse as many objects as possible. For example, if you have a bowling game, instead of disposing of and recreating your ball and pins on every shot, you could keep the same objects and just reset their positions to the "default" state. By default, when you change the properties of the node associated with a body, there will be no effect. That is because the pre-step that updates the physics body with the values from the transform node is disabled by default to improve performance. You can solve this by enabling the pre-step for a frame and then disabling it again on the next frame:
body.disablePreStep = false;// The position where you want to move the body tobody.transformNode.position.set(1, 2, 3);scene.onAfterRenderObservable.addOnce(() => { // Turn disablePreStep on again for maximum performance body.disablePreStep = true;})Please note that doing so may or may not allow the body to enter sleep mode, depending on the physics engine.
The reuse part also applies to collision shapes. Since bowling pins have the same shape, you can create only one PhysicsShape for all of them.
Use simpler shapes
Whenever possible, don't use the entire mesh as a collision shape, especially when it's a very high poly mesh. Try to use compounds or convex hull shapes instead.
Keep collision observables simple
Collision observables are functions that run every time a body collides with another. Since this can happen as often as every frame, it's important to avoid complex computations, or else the simulation will have to wait for these observables to finish before continuing.
Stress test Convex Hull Vs Mesh Test