Fixed Steps and Tweening

BlitzMax Forums/BlitzMax Programming/Fixed Steps 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.

Made a reply to another post, and thought maybe this could be useful to some who won't get to look there. Example code of fixed logic steps running at low frequency and motion interpolation to make it smooth.

Enjoy if you find this useful :P

Graphics(640, 480, 32)

Global UPDATE_FREQUENCY = 10 ' 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
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(1)
End Function