impostor.registerBeforePhysicsStep vs. scene.onBeforePhysicsObservable

Hi all!

The topic says it … what is the difference? When to use what?

At least there is a noticable difference: Babylon.js Playground

Thanks for any help,

. J

pinging @Cedric

… and additionally: is any of these the right place for applyForce()? Or are there better ones?

Physics engine can have subtime steps. for example, if you set the subtime step to 10, it will run 10 times each frame with a deltaTime/10 time for each steps. This is useful for example when you need extra precision for high velocity moving bodies.
That said, registerBeforePhysicsStep is run once per frame whereas onBeforePhysicsObservable can run multiple times per frame, depending on the sub step count.

For the applyforce/impulse, you can call it whenever you want, even multiple times a frame. It will be used only when the physics engine is ticked.

1 Like

@Cedric, great answer, thank you!

This brings me directly to another question that bothers me. I know that physics engines need to callback for force computation (e.g. with a Runge-Kutta-style of stepper) several times per frame. Is this the callback I see in onBeforePhysicsObservable?

And in this case … is there any timing information available (‘please calculate forces for time t’), so time-dependent forces can be calculated?

If you want the body to reach a particular speed you can use applyImpulse or call setLinearVelocity. What’s your use case?

My use case is a flight simulator, esp. the landing case. At some point in time during a frame interval the plane touches ground an gets repelled by the undercarriage (by the help of impostors). This changes all aerodynamic forces at that certain moment and need to be recalculated.
Second there could be a quickly changing force like a torque from an uprunning engine or such, which is a function of time explicitly. This also holds even without any collision whenever force gradients are that steep that they cannot be considered constant for the 16ms frame.

But my question has a more general character. AFAIK integration steppers work this way calling the force and velocity calculations a couple of times during the integration interval. Runge-Kutta, Cash-Karp and such (you name them) work like that.
I assume Cannon/Ammo/Oimo do so internally with their impostors for forces during collisions are typically extremely high, but they also should do so with ‘external’ (not caused by collisions, joints, motors) forces?

I was hoping that onBeforePhysicsObservable could be that kind of callback?

Could be that I’m completely off track though … just asking.

It depends how frequent and stable your computation needs to be. If your computation is reliable with a variable framerate, you can do it once per frame. You’d get potentially variable delta time when FPS is not 60.
If you want stable delta time, you can use onBeforePhysicsObservable. As it’s dependant of framerate, you’ll encounter multiple steps but the delta time sum will be 0.0166ms.
If you want more steps, you can change the step count in the physics engine so instead of doing 1 computation per frame, ift can perform an arbitrary number of iterations. This is usefull when you have high velocity.