Skip to content

Commit be86fdf

Browse files
fix: pr feedback
1 parent e60ba17 commit be86fdf

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

testbed3d/src/Testbed.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ export class Testbed {
5555
preTimestepAction?: (gfx: Graphics) => void;
5656
stepId: number;
5757
prevDemo: string;
58-
lastMessageTime: number;
5958
snap: Uint8Array;
6059
snapStepId: number;
6160
time: number;
61+
physicsTime: number;
6262
accumulator: number;
6363

6464
constructor(RAPIER: RAPIER_API, builders: Builders) {
@@ -73,6 +73,7 @@ export class Testbed {
7373
this.mouse = {x: 0, y: 0};
7474
this.events = new RAPIER.EventQueue(true);
7575
this.time = 0;
76+
this.physicsTime = 0;
7677
this.accumulator = 0;
7778

7879
this.switchToDemo(builders.keys().next().value);
@@ -91,16 +92,13 @@ export class Testbed {
9192
this.preTimestepAction = null;
9293
this.world = world;
9394
this.world.maxVelocityIterations = this.parameters.numVelocityIter;
94-
this.world.timestep = 1 / 60;
9595
this.demoToken += 1;
9696
this.stepId = 0;
9797
this.gui.resetTiming();
9898

9999
world.forEachCollider((coll) => {
100100
this.graphics.addCollider(this.RAPIER, world, coll);
101101
});
102-
103-
this.lastMessageTime = new Date().getTime();
104102
}
105103

106104
lookAt(pos: Parameters<Graphics["lookAt"]>[0]) {
@@ -141,15 +139,15 @@ export class Testbed {
141139
}
142140

143141
run() {
144-
const time = performance.now();
145-
const fixedStep = this.world.timestep;
146-
const frameTime = Math.min(0.01, (time - this.time) / 1000);
142+
let time = performance.now();
143+
let fixedStep = this.world.timestep;
144+
let deltaTime = (time - this.time) / 1000;
145+
let physicsDeltaTime = (time - this.physicsTime) / 1000;
147146

148147
this.time = time;
149-
this.accumulator += frameTime;
148+
this.accumulator += deltaTime;
150149

151-
// Run physics at a fixed update interval
152-
while (this.accumulator >= fixedStep) {
150+
if (physicsDeltaTime >= fixedStep) {
153151
if (this.parameters.running || this.parameters.stepping) {
154152
this.world.maxVelocityIterations =
155153
this.parameters.numVelocityIter;
@@ -158,8 +156,9 @@ export class Testbed {
158156
this.preTimestepAction(this.graphics);
159157
}
160158

159+
let t = performance.now();
161160
this.world.step(this.events);
162-
this.gui.setTiming(performance.now() - time);
161+
this.gui.setTiming(performance.now() - t);
163162
this.stepId += 1;
164163

165164
if (!!this.parameters.debugInfos) {
@@ -187,10 +186,14 @@ export class Testbed {
187186
}
188187
}
189188

189+
this.physicsTime = time;
190190
this.accumulator -= fixedStep;
191191
}
192192

193-
const alpha = this.accumulator / fixedStep;
193+
// let alpha = this.accumulator / fixedStep;
194+
let alpha = 1;
195+
196+
console.log(alpha);
194197

195198
if (this.parameters.stepping) {
196199
this.parameters.running = false;

0 commit comments

Comments
 (0)