Timing movements in seconds?

Blitz3D Forums/Blitz3D Beginners Area/Timing movements in seconds?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Instead of moving the object every 250 milliseconds, you need to move the object based on the number of miilisecs since the last update. Like this:

; Set 3D graphics mode
Graphics3D 640,480,16,0
SetBuffer BackBuffer()

camera=CreateCamera()
light=CreateLight()

cone=CreateCone( 32 )
PositionEntity cone,-3,0,5

distance#     = 6.5 ;blitz units
time#         = 3.0 ;seconds
speed#        = Float(distance / time);result is blitz units per second
speedpertick# = Float(speed#/1000);result is blitz units per tick

currentpos# = 0.0 

clock = MilliSecs()
starttime = clock


While Not KeyDown( 1 )
	
	ticks = MilliSecs() - clock
	clock = MilliSecs()

	If (currentpos < distance)
		distancemovedthisupdate# = speedpertick# * ticks
		MoveEntity(cone, distancemovedthisupdate, 0, 0)
		currentpos = currentpos + distancemovedthisupdate
	Else
		Text 2, 60, "Took " + (MilliSecs() - StartTime) + " milliseconds"
		Flip
		WaitKey
		End
	End If
		
	UpdateWorld()
	RenderWorld()
		
	Text 2, 5, CurrentTime$()
	Text 2, 30, currentpos

	Flip
	
Wend

ClearWorld()

End


That way it always moves at the same speed regardless of the framerate.

Edit: sorry, I didn't notice the significance of currentpos at first :-)

Edit: also, it'll never be *exactly* 6.5 units, or 3000 milliseconds because of the vagaries of PC performance, (my PC was doing a frame about every 17 milliseconds, which just doesn't divide cleanly into 3000) but it should be accurate enough for most purposes.