Skip to content

Commit

Permalink
Fix an issue in the frame rate synchronization of the Physics behavior (
Browse files Browse the repository at this point in the history
#5649)

* It should avoid jittery cameras that was happening from time to time.
  • Loading branch information
D8H authored Sep 19, 2023
1 parent cd896c3 commit dfe84fb
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions Extensions/Physics2Behavior/physics2runtimebehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,21 @@ namespace gdjs {

step(deltaTime: float): void {
this.frameTime += deltaTime;
if (this.frameTime >= this.timeStep) {
let numberOfSteps = Math.floor(this.frameTime / this.timeStep);
this.frameTime -= numberOfSteps * this.timeStep;
if (numberOfSteps > 5) {
numberOfSteps = 5;
}
for (let i = 0; i < numberOfSteps; i++) {
this.world.Step(this.timeStep * this.timeScale, 8, 10);
}
this.world.ClearForces();
// `frameTime` can take negative values.
// It's better to be a bit early rather than skipping a frame and being
// a lot more late.
let numberOfSteps = Math.max(
0,
Math.round(this.frameTime / this.timeStep)
);
this.frameTime -= numberOfSteps * this.timeStep;
if (numberOfSteps > 5) {
numberOfSteps = 5;
}
for (let i = 0; i < numberOfSteps; i++) {
this.world.Step(this.timeStep * this.timeScale, 8, 10);
}
this.world.ClearForces();
this.stepped = true;
}

Expand Down

0 comments on commit dfe84fb

Please sign in to comment.