Home
Products
Community
Manuals
Contact
Login or Signup

Code archives/Graphics/2D movement timing via deltatime

This code has been declared by its author to be Public Domain code.

Download source code

2D movement timing via deltatime by Rob(Posted 1+ years ago)
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.
;Robs simple movement timing in 2D
Global deltatime#,oldmillisecs#,thistime#,x#,xspd#

xspd=.5

;setup
Graphics 640,480,16,1
SetBuffer BackBuffer()


oldmillisecs=MilliSecs()
While Not KeyHit(1)
	;get time elapsed since last frame
	
	thistime# = MilliSecs()
	deltatime#=thistime#-oldmillisecs#
	oldmillisecs#=thistime#
	;graphics with timing
	Cls
	
	x=x + deltatime*xspd   ; this is movement by time elapsed.

	If x>640 Then x=0
	Rect x,y,16,16,1
	
	;uncomment for testing. movement will reach a to b in the same time despite how much is on screen
	;For i=0 To 1000
	;	Rect 100,100,100,100
	;Next 
	
	Flip
		
Wend
End

Comments

None.

Code Archives Forum