Using Delta time to get accurate frame timing for the updateworld command?

Blitz3D Forums/Blitz3D Programming/Using Delta time to get accurate frame timing for the updateworld command?

Craig H. Nisbet(Posted 2004) [#1]
Does anyone have a code example of how to use Delta time to limit the speed of the Updateworld command? What I have isn't really working so good.


Rogue Vector(Posted 2004) [#2]
Did you mean this?

Repeat

;coding on time for equal FPS 
If dt=0 Then dt=MilliSecs() ;multiply all movements with DeltaT
DeltaT=(MilliSecs()-dt)/1000
dt=MilliSecs()
 
;basic interaction 
If KeyDown(200)=True Then MoveEntity Cam,0,0,300*DeltaT ; Up
If KeyDown(208)=True Then MoveEntity Cam,0,0,-300*DeltaT ; Down
If KeyDown(205)=True Then MoveEntity Cam,300*DeltaT,0,0 ; Right (Sidestep)
If KeyDown(203)=True Then MoveEntity Cam,-300*DeltaT,0,0 ; Left (Sidestep)

If KeyDown(76)=True Then TurnEntity Cam,-EntityPitch#(Cam),0,-EntityRoll#(Cam) ; center look


Mx#=Mx#*.9+MouseXSpeed()
My#=My#*.9+MouseYSpeed()
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2

TurnEntity Cam,+(My#*2)*DeltaT,-(Mx#*2)*DeltaT,0


Roll#=EntityRoll#(Cam)
If Roll#<>0 Then TurnEntity Cam,0,0,-Roll#
 
; Restriction looking up
Pitch#=EntityPitch#(Cam)
	If Pitch#>50
	Pitch#=Pitch#-50
	TurnEntity Cam,-Pitch#*DeltaT,0,0
EndIf 
 
; Restriction looking down
If Pitch#<-75
	Pitch#=Pitch#+75
	TurnEntity Cam,-Pitch#*DeltaT,0,0
EndIf
FlushMouse

UpdateWorld()

RenderWorld()

Flip False

Until KeyHit(1)=True



Regards,

Rogue Vector


jfk EO-11110(Posted 2004) [#3]
Quick Help says: UpdateWorld(elapsed_time#)

Maybe try this :

walkspeed#=1.2
animate harry ; animate a mesh

dt=MilliSecs()
vwait

Repeat
  DeltaT#=(MilliSecs()-dt) / 16.667
  dt=MilliSecs()
  ; on a machine with 60 FPS deltaT is exactly 1.0
  ; on lower Framerates deltaT is higher = steps are bigger

  If KeyDown(200)=True Then MoveEntity Cam,0,0,walkspeed#*DeltaT# ; Up
  ; etc...

  UpdateWorld(DeltaT#)
  RenderWorld()
  Flip
Until KeyHit(1)=True


you might have to limit DeltaTs min and max to prevent megajumps when the machine pauses for some reason. Also, when you pause the game by a options manu or something, you need to set "dt" to millisecs()-17 before you continue the gameloop.


_PJ_(Posted 2004) [#4]
For my spaceship-game/project I use delta time (primarily to keep the acceleration etc. in standard units, but also as I believe it will be important ffor the multiplayer version. The principle is the same as above:

DeltaT=(MilliSecs()-dt)/1000
dt=MilliSecs()