Code archives/Algorithms/time dependant movements and rotations

This code has not been declared by its author to be Public Domain code, and therefore should not be assumed to be so.

Download source code

time dependant movements and rotations by Kostik
(Posted 25 years ago)
This is a technique that let you do all kind of movements in dependence of time, not fps. This Demo shows a moving circle 640 Pixel per second, you can slow down the framerate but anyway the circle will move the same speed. Just take a look and you will see what i mean, professional Games are written using this technique (or something like that)


	Graphics 640,480,16

	;Set t and DeltaT global as float, this MUST be done, trust me!
	Global t#,DeltaT#

	;Create an image so that we have something on the screen
	image1=CreateImage(30,30)
	
	;Draw a green circle on the ImageBuffer 
	SetBuffer ImageBuffer(image1)
	Color 0,160,0
	Oval 0,0,30,30,1
	
	
	;Set the Buffer back to the whole screen
	SetBuffer BackBuffer()



	Repeat
	
	
		;This is the "time" trick---------------------------------------------
		If T=0 Then T=MilliSecs()
		DeltaT=(MilliSecs()-T)/1000
		T=MilliSecs()
		
		
		;The Speed of the circle is calculated with DeltaT,
		;it's the passed time since the Last frame
		;Let's say, the circle should move 640 pixel per second
		;besides, the coords variables MUST be flaot!
		x# = x# + (640 * DeltaT)
		y=200
		
		;Put the circle back
		If x>GraphicsWidth() Then x=0
		;Draw our circle
		DrawImage image1,x,y 
		;---------------------------------------------------------------------




		;This code slows down the framerate
		;you have to play around with this to
		;achieve the slow down, this is for an
		;Athlon 1.2 Ghz, GeForce2MX
 		If KeyDown(200) Then brake=brake+1
		If KeyDown(208)	Then brake=brake-1
		If brake<1 Then brake=1
		For i=1 To brake*10000
		Next

		;Framecounter
		Framecounter_counter=Framecounter_counter+1
		If Framecounter_time=0 Then Framecounter_time=MilliSecs()
		If Framecounter_time+1001 <MilliSecs() Then
			Framecounter_framerate=Framecounter_counter
			Framecounter_counter=0
			Framecounter_time=MilliSecs()
		EndIf
		
		;Some text
		Text 400,300,&quot;fps: &quot;+Framecounter_framerate
		Text 400,310,&quot;brake: &quot;+brake
		Text 0,0,&quot;Use Arrow Up/Down to increase/decrease FPS&quot;
		Text 0,20,&quot;DeltaT: &quot;+DeltaT
		Text 500,400,&quot;Time: &quot;+CurrentTime$()
		;--------------------------------------------------------
	Flip
	Cls 
Until KeyHit(1)
End