Physics: should setSubTimeStep speed up simulation?

Hi all!

I was playing around with the PG in Advanced Animation Methods | Babylon.js Documentation and tried adding setSubTimeStep.

I assumed that setSubTimeStep would not affect the number of seconds a simulation would take to run. My understanding may be wrong, but I thought that setSubTimeStep would insert simulation steps in between our chosen timestep (with physEngine.setTimeStep(1/60);) to help us increase the resolution of our simulation.

PG with setSubTimeStep: https://playground.babylonjs.com/#DU4FPJ#167

// console.log output
sphere is at rest on stepId: 33
duration: 0.616
box1.rotation.y is: 1.7000000000000008

PG without setSubTimeStep: https://playground.babylonjs.com/#DU4FPJ#168

// console.log output
sphere is at rest on stepId: 551
duration: 9.226
box1.rotation.y is: 27.600000000000257

By setting setSubTimeStep(1), not only does the simulation complete much faster, but also the number of simulation steps has been reduced from 551 to 33.

Could someone please help clarify if we should expect this behavior? Thank you for your help!

So I have absolutely no clue and I am not that versed about our Physics engines, and unfortunately for you, only @Evgeni_Popov and myself are present from the core team until Jan.

In case the community does not find it before, let me add @RaananW and @Cedric to the thread for when they come back in Jan.

1 Like

Thank you so much, sebavan!

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

Thank you so much for all of your help, Cedric!

I would love to have the second scenario you mentioned with a classic 60FPS with each frame having 16 physics iterations

I tried using

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

in the Playground: https://playground.babylonjs.com/#DU4FPJ#170, but this seems to yield the same result as using CannonJSPlugin(false) and the simulation runs at super fast speed.

Uncommenting the line scene.getPhysicsEngine().setSubTimeStep(1); will allow the simulation to run at normal speed.

Thank you so much for taking the time to provide such a detailed answer over your break :slight_smile: I’d be really grateful even if you responded after your vacation, and please enjoy your holidays!