Skip to content

Commit

Permalink
fix: opt pr code
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfengzzz committed Mar 22, 2022
1 parent 0323440 commit c4533b8
Showing 1 changed file with 8 additions and 32 deletions.
40 changes: 8 additions & 32 deletions packages/core/src/physics/PhysicsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export class PhysicsManager {
static _nativePhysics: IPhysics;

private _engine: Engine;
private _fixedTimeStep: number = 1 / 60;
private _maxSumTimeStep: number = 1 / 3;
private _maxStepCount: number = 20;
private _restTime: number = 0;

private _gravity: Vector3 = new Vector3();
Expand Down Expand Up @@ -109,6 +106,12 @@ export class PhysicsManager {
}
};

/** The fixed time step in seconds at which physics are performed. */
fixedTimeStep: number = 1 / 60;

/** The max sum of time step in seconds one frame. */
maxSumTimeStep: number = 1 / 3;

get gravity(): Vector3 {
return this._gravity;
}
Expand All @@ -121,30 +124,6 @@ export class PhysicsManager {
this._nativePhysicsManager.setGravity(gravity);
}

/**
* The fixed time step in seconds at which physics are performed.
*/
get fixedTimeStep(): number {
return this._fixedTimeStep;
}

set fixedTimeStep(value: number) {
this._fixedTimeStep = value;
this._maxStepCount = Math.floor(this._maxSumTimeStep / value);
}

/**
* The max sum of time step in seconds one frame.
*/
get maxSumTimeStep(): number {
return this._maxSumTimeStep;
}

set maxSumTimeStep(value: number) {
this._maxSumTimeStep = value;
this._maxStepCount = Math.floor(value / this._fixedTimeStep);
}

constructor(engine: Engine) {
this._engine = engine;
this._nativePhysicsManager = PhysicsManager._nativePhysics.createPhysicsManager(
Expand Down Expand Up @@ -264,14 +243,11 @@ export class PhysicsManager {
* @internal
*/
_update(deltaTime: number): void {
const {
_fixedTimeStep: fixedTimeStep,
_nativePhysicsManager: nativePhysicsManager
} = this;
const { fixedTimeStep: fixedTimeStep, _nativePhysicsManager: nativePhysicsManager } = this;
const componentManager = this._engine._componentsManager;

const simulateTime = deltaTime + this._restTime;
const step = Math.min(this._maxStepCount, Math.floor(simulateTime / fixedTimeStep));
const step = Math.floor(Math.min(this.maxSumTimeStep, simulateTime) / fixedTimeStep);
this._restTime = simulateTime - step * fixedTimeStep;
for (let i = 0; i < step; i++) {
componentManager.callScriptOnPhysicsUpdate();
Expand Down

0 comments on commit c4533b8

Please sign in to comment.