December 04, 2020, 11:24:27 AM

Author Topic: [bb] 2D movement timing via deltatime by Rob [ 1+ years ago ]  (Read 452 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 2D movement timing via deltatime
Author : Rob
Posted : 1+ years ago

Description : Simple example of how time elapsed since the last frame can help you position your sprites, and indeed - 3D objects the same across all framerates and hardware.

Code :
Code: BlitzBasic
  1. ;Robs simple movement timing in 2D
  2. Global deltatime#,oldmillisecs#,thistime#,x#,xspd#
  3.  
  4. xspd=.5
  5.  
  6. ;setup
  7. Graphics 640,480,16,1
  8. SetBuffer BackBuffer()
  9.  
  10.  
  11. oldmillisecs=MilliSecs()
  12. While Not KeyHit(1)
  13.         ;get time elapsed since last frame
  14.        
  15.         thistime# = MilliSecs()
  16.         deltatime#=thistime#-oldmillisecs#
  17.         oldmillisecs#=thistime#
  18.         ;graphics with timing
  19.         Cls
  20.        
  21.         x=x + deltatime*xspd   ; this is movement by time elapsed.
  22.  
  23.         If x>640 Then x=0
  24.         Rect x,y,16,16,1
  25.        
  26.         ;uncomment for testing. movement will reach a to b in the same time despite how much is on screen
  27.         ;For i=0 To 1000
  28.         ;       Rect 100,100,100,100
  29.         ;Next
  30.        
  31.         Flip
  32.                
  33. Wend
  34. End


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal