Fixed Rate Logic and Tweening

BlitzMax Forums/BlitzMax Programming/Fixed Rate Logic and Tweening

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.

To illustrate what i was talking about above, here's my modified sample. UPDATE_FREQUENCY is set to 1, this is as low as it gets, I am clipping the circle to the edges so it does not defeat it's boundary. You can notice why the tweened circle slows down at the end, because the real circle also slows down. Try changing frequency to 10, 30, and 60Hz, the last tick becomes un-noticeable.
Graphics 640, 480, 0

Global UPDATE_FREQUENCY = 1 ' times per second
Global update_time = 1000 / UPDATE_FREQUENCY
Global t , dt , execution_time = 0

' boucing ball
Global X# , oldX#
Global dirX# = 1

t = MilliSecs()
While Not KeyDown(KEY_ESCAPE)
	dt = MilliSecs() - t
	t = MilliSecs()
	execution_time:+ dt
	' fixed interval update loop    		
        While execution_time >= update_time		
		Update()
		execution_time:- update_time
	Wend
	' calculate the remainder for motion interpolation
	Local et# = execution_time
	Local ut# = update_time
	Local tween# = et / ut
	Render(tween)
Wend

Function Update()
	' time independent speed
	Local Speed# = 150.0 / (1000.0 / Float(update_time)) ' 150.0 pixels per second
	' record the old position for tweening
	oldX = X
	' move the ball
	X:+ (Speed * dirX)
	' reverse directions if ball is out of screen bounds
	If X < 0 Or X > 640 Then dirX = - dirX
	If x < 0  x = 0
	If x > 640 x = 640	
End Function

Function Render(tween#)
	Cls
	' interpolate between old and actual positions
	Local tx# = X * tween + OldX * (1.0 - tween)	
	' draw bouncing ball with interpolated values
	DrawOval tx - 16 , 50 , 32 , 32
	' draw second bouncing ball WITHOUT tweening
	DrawOval X - 16 , 200 , 32 , 32
	Flip
End Function