Physics: should setSubTimeStep speed up simulation?

Hi @gbz

There are many controls on time with physics and it can be complicated to get it right once you want a clear control and steps.

First thing first:
var physEngine = new BABYLON.CannonJSPlugin(false); the parameter determines if steps (or substeps) time delta should be fixed of not. if false, the steps (or substeps) will alsways be the same.

It means having substeps with fixed time step results in simulation running faster.

Let’s say you want substeps in order to improve physics resolution like when having fast moving impostors and you want extra precision to not miss collision detection.

var physEngine = new BABYLON.CannonJSPlugin(true);
scene.enablePhysics(new BABYLON.Vector3(0, -9.8, 0), physEngine);
scene.getPhysicsEngine().setSubTimeStep(1);

The physics engine is instanced with variable time steps and the subtime step is set to 1ms. So, for a classic 60FPS, frames will be computed with 16 physics iterations.

Now if we want to slow down physics:

    var physEngine = new BABYLON.CannonJSPlugin(false);
    scene.enablePhysics(new BABYLON.Vector3(0, -9.8, 0), physEngine);
    physEngine.setTimeStep(1/1200);

This will enable fixed time steps with each physics iteration running with a delta of 1/1200 second. The simulation will run at 60FPS, 1 physics iteration per frame but the each frame time in the physics world will be 1/1200 of a second.

Hope this will help you :slight_smile: Let me know if you need more explanation on this topic

4 Likes