Code archives/Algorithms/time dependant movements and rotations

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

Download source code

time dependant movements and rotations by Kostik2002
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,"fps: "+Framecounter_framerate
		Text 400,310,"brake: "+brake
		Text 0,0,"Use Arrow Up/Down to increase/decrease FPS"
		Text 0,20,"DeltaT: "+DeltaT
		Text 500,400,"Time: "+CurrentTime$()
		;--------------------------------------------------------
	Flip
	Cls 
Until KeyHit(1)
End

Comments

n8r2k2007
this is excellent code but when i use it to make a sphere jump i cant get the jump to be constant for every fps, the jump gets further the more i slow the fps down can anyone help me


n8r2k2007
oops double post


TaskMaster2007
Wow, this is old...

There has been a lot of discussion of this topic in the last few months with some great solutions. Search for tweening...


n8r2k2007
hehe yah all i wanted was an fps counter but this did more, thanks


Code Archives Forum