engine
engine copied to clipboard
Adding a fixedUpdate for full control over physics.
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);
}
}
This PR introduces some new APIs and changes some existing internals.
- 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
});
-
If there are any internal behavior changes? Please describe them.
-
If the are any breaking changes?
-
Are there are a performance implications?
-
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.
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.