[bb] Star field by Matty B [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Star field
Author : Matty B
Posted : 1+ years ago

Description : Basic 3d Stars  

Code :
Code (blitzbasic) Select
Const MAX_STAR=5000,STAR_SPEED=2,WIDTH=640,HEIGHT=480

Dim star_x(MAX_STAR),star_y(MAX_STAR),star_z(MAX_STAR)

Graphics WIDTH,HEIGHT
SetBuffer BackBuffer()

setup_stars()

While Not KeyDown(1)
Cls
UpdateStar()
Flip
Wend
End

Function setup_stars()
  For c=0 To MAX_STAR
star_x(c)=Rnd(-(WIDTH/2),(WIDTH/2))Shl 8
star_y(c)=Rnd(-(HEIGHT/2),(HEIGHT/2))Shl 8
    star_z(c)=Rnd(STAR_SPEED,255)
Next
End Function

Function UpdateStar()
For c=0 To MAX_STAR
star_z(c)=star_z(c)-STAR_SPEED
If star_z(c)<=STAR_SPEED Then star_z(c)=255
s_x=(star_x(c)/star_z(c))+(WIDTH/2)
s_y=(star_y(c)/star_z(c))+(HEIGHT/2)
col=255-star_z(c)
Color col,col,col
Plot s_x,s_y
Next
End Function


Comments :


Tim(Posted 1+ years ago)

 anybody knows how to add the stars to a camera with movement ? tried it many times with projectedx but i didn't got it..


CyBeRGoth(Posted 1+ years ago)

 Nice :)Try replacing:Plot s_x,s_yin the UpdateStar function with:Rect s_x,s_y,col/50,col/50give you a nice sense of depth :)


Rob Farley(Posted 1+ years ago)

 For a bit of performance boost...
Function UpdateStar()
LockBuffer BackBuffer()
For c=0 To MAX_STAR
star_z(c)=star_z(c)-STAR_SPEED
If star_z(c)<=STAR_SPEED Then star_z(c)=255
s_x=(star_x(c)/star_z(c))+(WIDTH/2)
s_y=(star_y(c)/star_z(c))+(HEIGHT/2)
col=255-star_z(c)
If s_x>-1 And s_x<width And s_y>-1 And s_y<height
argb=(col Or (col Shl 8) Or (col Shl 16) Or ($ff000000))
WritePixelFast s_x,s_y,argb,BackBuffer()
EndIf

Next
UnlockBuffer BackBuffer()
End Function
You'll be able to tell the difference if you change your flip to flip false. [/i]