engine icon indicating copy to clipboard operation
engine copied to clipboard

Adding a fixedUpdate for full control over physics.

Open AlexAPPi opened this issue 5 months ago • 2 comments

This PR adds two new events — fixedUpdate and postFixedUpdate — to the engine, allowing for more flexible update management. It also introduces the ability to dynamically change the fixed timestep via app.fixedTimeStep (in seconds), providing greater control over update frequency.

A fixedUpdate method has also been added for scripts, which will be called on the fixedUpdate event with a fixed timestep of app.fixedTimeStep (in seconds), regardless of whether the usePostFixedUpdateForPhysicsSim flag is set to false.

Additionally, a new parameter usePostFixedUpdateForPhysicsSim has been added, which controls the physics simulation mode. If set to false, physics will continue to run via the standard Ammo stepSimulation call, with no changes for existing projects. If set to true, the simulation will be executed strictly after fixedUpdate with the fixedTimeStep, enabling more precise and predictable physics behavior without inaccuracies.

This will also mark the beginning of the integration of the Jolt Physics engine into the playcanvas ecosystem. PR data is coming soon.

Cycle application:

  • initialize
  • postInitialize
  • fixedUpdate cycle:
    • fixedUpdate
    • postFixedUpdate
  • update
  • animationUpdate
  • postUpdate

PhysicsExampleScript.mjs:

import { Script, Entity } from "playcanvas";

export class PhysicsExampleScript extends Script {
    
    static scriptName = 'PhysicsExampleScript';
    
    /**
     * @attribute
     * @type {Entity}
     * @placeholder Dynamic Rigidbody Entity
     */
    dynamicRbEntity;
    
    initialize() {
          this.app.usePostFixedUpdateForPhysicsSim = true; // activate postFixedUpdate for physics simulation
          this.app.fixedTimeStep = 1 / 30; // calls update physics 30 times per second
    }
    
    fixedUpdate(dt, stepIndex) {
         // call before simulation physics step
         // now we don't need to use deltaTime like in the update method
         // the force will not accumulate.
         this.dynamicRbEntity.rigidbody.applyForce(10, 10, 10);
    }
}

AlexAPPi avatar Jun 11 '25 16:06 AlexAPPi

This PR introduces some new APIs and changes some existing internals.

  1. What new APIs are introduced, please provide a simple block in the PR's post with new APIs, something like:
// pc.XrManager
xr.meshDetection // XrMeshDetection interface that provides an access to XrMeshe's
xr.start(camera, pc.XRTYPE_AR, pc.XRSPACE_LOCALFLOOR, {
    meshDetection: true // new option to request mesh detection features
});
  1. If there are any internal behavior changes? Please describe them.

  2. If the are any breaking changes?

  3. Are there are a performance implications?

  4. Can fixed update frequency be adjusted?

The naming conventions and style for the code, please refer to contributors doc, more specifically the event names, they need to follow a camelCase naming convention, as the rest of the codebase.

Maksims avatar Jun 12 '25 07:06 Maksims

There are no breaking changes in this PR, as the main modifications relate to physics, to activate the new features, you need to explicitly set usePostFixedUpdateForPhysicsSim to true. Essentially, this PR adds a fixed update event, the step size of which can be adjusted in real time.

AlexAPPi avatar Jun 13 '25 02:06 AlexAPPi