Skip to content

Adding a fixedUpdate for full control over physics. #7754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

AlexAPPi
Copy link
Contributor

@AlexAPPi AlexAPPi commented Jun 11, 2025

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);
    }
}

@Maksims
Copy link
Collaborator

Maksims commented Jun 12, 2025

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.

@AlexAPPi
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants