News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Timer Based Movement Star Field

Started by kevin, March 09, 2011, 10:00:06 AM

Previous topic - Next topic

kevin

  Timer Based Movement Star Field

    This example creates a simple star field with one subtle difference, the stars movement is plotted in pixels per second, against time in other words .  So the effect isn't attached from the frame rate (for the most part).

    Each star is given a starting position / start time and a speed in pixels per second (1000 milliseconds). The render routine works out how far the star has moved since it's start time (creation time), then plots it in at this current position. If the star has left the screen, it's given a new starting coordinate and it's start time is reset.  So the star is reborn making the effect infinite.  


PlayBASIC Code: [Select]
   Type tStars
; XY coordinate of this star
X#,Y#
; Speed in pixels per second
Speed#
; Time this star was created in milliseconds
STartTime
EndType


StartTime=Timer()

Dim Star as tStars list

For lp=0 to 2000
Star=New tStars
Star.x#=rnd(GetSurfaceWidth())
Star.y#=rnd(GetSurfaceHeight())
// Speed in pixels per second
Star.SPeed#=rndrange(100,500)/1000.0
Star.StartTime=StartTime
next




Do
cls


CurrentTime=Timer()
lockbuffer
For each Star()
TimePast=CurrentTime-Star.StartTime
x#=STar.x#+(TimePast*Star.Speed)

dot x#,star.y#

; Check if the star is off the screen (right hand)
if X#>800
; if it is, reset it's starting X coordinate
Star.x#=-10
; Randomize it's Y coordinate
Star.y#=rnd(Getsurfaceheight())
; set it's starting time to now.
Star.StartTime=CurrentTime
endif

next
lockbuffer

Sync
loop






 Related Articles

   Timer Based Movement
    Vsync / Frame Rate Scaling
   Timer Based Movement with Vsync Tweening
   Handling Multiple Animations using Timer()
   Consistent Game Timing