I’m trying to understand the execution frequency of scene.onAfterPhysicsObservable.add().
When I log something inside scene.onBeforeRenderObservable, I get around 120 outputs per second, which matches my FPS as expected. However, when I log something inside scene.onAfterPhysicsObservable, it also seems to print around 120 times per second.
Does that mean onAfterPhysicsObservable is called once per render frame, or is it tied to the physics engine’s internal tick rate (for example, Havok - 60 steps per second)?
I’m trying to confirm whether physics updates are locked to frame updates or can run at a different fixed timestep internally.
observers are usually called on a frame basis. But physics allows sub steps.
Depending on subTime (you can have 60Hz frame update and ask for a sub step of 1ms giving 16 physics steps per frame update)
Check this code :
If you don’t need extra precision with fast moving bodies, you don’t have to use subStep and you’ll get 1 onBeforePhysicsObservable and 1 onAfterPhysicsObservable per frame
So in my case, I actually need a callback that only runs once per physics tick (not per render frame), since I want to implement my own fixedUpdate logic tied to the physics step rather than the render rate.
What would be the best way to hook into that?
Should I rely on onAfterPhysicsObservable and manually check for substeps, or is there a lower-level callback/event exposed directly from the physics engine (like Havok) that fires only once per physics tick?
// After you enable physics
// scene.enablePhysics(new BABYLON.Vector3(0, -9.81, 0), new BABYLON.HavokPlugin(true, havok));
const pe = scene.getPhysicsEngine();
// Make physics run at fixed 60 Hz regardless of render FPS
pe.setSubTimeStep(1000 / 60); // milliseconds per physics step
function fixedUpdate(dt) {
// dt is seconds per physics step
// put gameplay logic that must be deterministic here
}
scene.onAfterPhysicsObservable.add(() => {
// Called once per physics substep
const dt = pe.getSubTimeStep() / 1000;
fixedUpdate(dt);
});