1
1
import { Graphics } from "./Graphics" ;
2
2
import { Gui } from "./Gui" ;
3
3
import type { DebugInfos } from "./Gui" ;
4
- import * as md5 from "md5" ;
4
+ import md5 from "md5" ;
5
5
import type * as RAPIER from "@dimforge/rapier2d" ;
6
6
7
7
type RAPIER_API = typeof import ( "@dimforge/rapier2d" ) ;
@@ -60,6 +60,10 @@ export class Testbed {
60
60
lastMessageTime : number ;
61
61
snap : Uint8Array ;
62
62
snapStepId : number ;
63
+ frameTime : number ;
64
+ accumulator : number ;
65
+ alpha : number ;
66
+ maxSubsteps : number ;
63
67
64
68
constructor ( RAPIER : RAPIER_API , builders : Builders ) {
65
69
let backends = [ "rapier" ] ;
@@ -73,6 +77,10 @@ export class Testbed {
73
77
this . mouse = { x : 0 , y : 0 } ;
74
78
this . events = new RAPIER . EventQueue ( true ) ;
75
79
this . switchToDemo ( builders . keys ( ) . next ( ) . value ) ;
80
+ this . frameTime = 0 ;
81
+ this . accumulator = 0 ;
82
+ this . alpha = 0 ;
83
+ this . maxSubsteps = 6 ;
76
84
77
85
window . addEventListener ( "mousemove" , ( event ) => {
78
86
this . mouse . x = ( event . clientX / window . innerWidth ) * 2 - 1 ;
@@ -141,43 +149,60 @@ export class Testbed {
141
149
}
142
150
143
151
run ( ) {
144
- if ( this . parameters . running || this . parameters . stepping ) {
145
- this . world . maxVelocityIterations = this . parameters . numVelocityIter ;
146
- this . world . maxVelocityFrictionIterations =
147
- this . parameters . numVelocityIter * 2 ;
148
-
149
- if ( ! ! this . preTimestepAction ) {
150
- this . preTimestepAction ( this . graphics ) ;
152
+ let time = performance . now ( ) ;
153
+ let fixedStep = this . world . timestep ;
154
+ let deltaTime = ( time - this . frameTime ) / 1000 ;
155
+ let substeps = 0 ;
156
+
157
+ this . frameTime = time ;
158
+ this . accumulator += deltaTime ;
159
+
160
+ if ( this . accumulator >= fixedStep && substeps < this . maxSubsteps ) {
161
+ this . accumulator -= fixedStep ;
162
+ substeps ++ ;
163
+
164
+ if ( this . parameters . running || this . parameters . stepping ) {
165
+ this . world . maxVelocityIterations =
166
+ this . parameters . numVelocityIter ;
167
+ this . world . maxVelocityFrictionIterations =
168
+ this . parameters . numVelocityIter * 2 ;
169
+
170
+ if ( ! ! this . preTimestepAction ) {
171
+ this . preTimestepAction ( this . graphics ) ;
172
+ }
173
+
174
+ let t0 = new Date ( ) . getTime ( ) ;
175
+ this . world . step ( this . events ) ;
176
+ this . gui . setTiming ( new Date ( ) . getTime ( ) - t0 ) ;
177
+ this . stepId += 1 ;
178
+
179
+ if ( ! ! this . parameters . debugInfos ) {
180
+ let t0 = performance . now ( ) ;
181
+ let snapshot = this . world . takeSnapshot ( ) ;
182
+ let t1 = performance . now ( ) ;
183
+ let snapshotTime = t1 - t0 ;
184
+
185
+ let debugInfos : DebugInfos = {
186
+ token : this . demoToken ,
187
+ stepId : this . stepId ,
188
+ worldHash : "" ,
189
+ worldHashTime : 0 ,
190
+ snapshotTime : 0 ,
191
+ } ;
192
+ t0 = performance . now ( ) ;
193
+ debugInfos . worldHash = md5 ( snapshot ) ;
194
+ t1 = performance . now ( ) ;
195
+ let worldHashTime = t1 - t0 ;
196
+
197
+ debugInfos . worldHashTime = worldHashTime ;
198
+ debugInfos . snapshotTime = snapshotTime ;
199
+
200
+ this . gui . setDebugInfos ( debugInfos ) ;
201
+ }
151
202
}
152
203
153
- let t0 = new Date ( ) . getTime ( ) ;
154
- this . world . step ( this . events ) ;
155
- this . gui . setTiming ( new Date ( ) . getTime ( ) - t0 ) ;
156
- this . stepId += 1 ;
157
-
158
- if ( ! ! this . parameters . debugInfos ) {
159
- let t0 = performance . now ( ) ;
160
- let snapshot = this . world . takeSnapshot ( ) ;
161
- let t1 = performance . now ( ) ;
162
- let snapshotTime = t1 - t0 ;
163
-
164
- let debugInfos : DebugInfos = {
165
- token : this . demoToken ,
166
- stepId : this . stepId ,
167
- worldHash : "" ,
168
- worldHashTime : 0 ,
169
- snapshotTime : 0 ,
170
- } ;
171
- t0 = performance . now ( ) ;
172
- debugInfos . worldHash = md5 ( snapshot ) ;
173
- t1 = performance . now ( ) ;
174
- let worldHashTime = t1 - t0 ;
175
-
176
- debugInfos . worldHashTime = worldHashTime ;
177
- debugInfos . snapshotTime = snapshotTime ;
178
-
179
- this . gui . setDebugInfos ( debugInfos ) ;
180
- }
204
+ this . accumulator = this . accumulator % fixedStep ;
205
+ this . alpha = this . accumulator / fixedStep ;
181
206
}
182
207
183
208
if ( this . parameters . stepping ) {
@@ -186,7 +211,11 @@ export class Testbed {
186
211
}
187
212
188
213
this . gui . stats . begin ( ) ;
189
- this . graphics . render ( this . world , this . parameters . debugRender ) ;
214
+ this . graphics . render (
215
+ this . world ,
216
+ this . parameters . debugRender ,
217
+ this . alpha ,
218
+ ) ;
190
219
this . gui . stats . end ( ) ;
191
220
192
221
requestAnimationFrame ( ( ) => this . run ( ) ) ;
0 commit comments