11import { Graphics } from "./Graphics" ;
22import { Gui } from "./Gui" ;
33import type { DebugInfos } from "./Gui" ;
4- import * as md5 from "md5" ;
4+ import md5 from "md5" ;
55import type * as RAPIER from "@dimforge/rapier2d" ;
66
77type RAPIER_API = typeof import ( "@dimforge/rapier2d" ) ;
@@ -60,6 +60,10 @@ export class Testbed {
6060 lastMessageTime : number ;
6161 snap : Uint8Array ;
6262 snapStepId : number ;
63+ frameTime : number ;
64+ accumulator : number ;
65+ alpha : number ;
66+ maxSubsteps : number ;
6367
6468 constructor ( RAPIER : RAPIER_API , builders : Builders ) {
6569 let backends = [ "rapier" ] ;
@@ -73,6 +77,10 @@ export class Testbed {
7377 this . mouse = { x : 0 , y : 0 } ;
7478 this . events = new RAPIER . EventQueue ( true ) ;
7579 this . switchToDemo ( builders . keys ( ) . next ( ) . value ) ;
80+ this . frameTime = 0 ;
81+ this . accumulator = 0 ;
82+ this . alpha = 0 ;
83+ this . maxSubsteps = 6 ;
7684
7785 window . addEventListener ( "mousemove" , ( event ) => {
7886 this . mouse . x = ( event . clientX / window . innerWidth ) * 2 - 1 ;
@@ -141,43 +149,60 @@ export class Testbed {
141149 }
142150
143151 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+ }
151202 }
152203
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 ;
181206 }
182207
183208 if ( this . parameters . stepping ) {
@@ -186,7 +211,11 @@ export class Testbed {
186211 }
187212
188213 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+ ) ;
190219 this . gui . stats . end ( ) ;
191220
192221 requestAnimationFrame ( ( ) => this . run ( ) ) ;
0 commit comments