How can I execute code every physics step?

I’m trying to have force that is constantly applied to an object, rather than a one-off impulse. Right now I’m calling applyForce() from inside the render loop. The problem is, this causes the force to be weaker or stronger depending on your framerate. As far as I know, the physics engine updates at a fixed rate independent of your framerate, but I can’t find a way to hook into that update loop and run my own code at the same rate (like you can with runRenderLoop). Basically, is there a physics version of runRenderLoop?

By the way, I’m currently using the cannon.js plugin, but I’m not opposed to switching to one of the other ones if there’s a good reason to.

Thanks!

1 Like

You would need to use a Physics Imposter and register a physics update handler.

Example:

abstractMesh.physicsImpostor.registerAfterPhysicsStep(()=>{
      console.log("Run custom Code Here");
});

5 Likes

That’s perfect, thanks!