Moving Background Starfield

Blitz3D Forums/Blitz3D Beginners Area/Moving Background Starfield

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.

Quite bit of coding for you...

You might want to play with the random position of the star, to give more depth you need to increase the z start position, rand(10,500) looks pretty good, although because you are further away you also need to increase the x and y so it fills vertically and horizontally... basically just play with it...

And obviously have the start Z position ie the 10 part of rand(10,100) to be behind your action.

Graphics3D 800,600,32,2


Global StarTex = CreateStar(32)
Global camera = CreateCamera()

Type Star
	Field Mesh
End Type

Function NewStar()
	S.Star = New Star
	S\Mesh = CreateSprite()
	EntityTexture s\mesh,startex
	PositionEntity s\mesh,Rand(-100,100),Rand(-100,100),Rand(10,100) ; Random position of the star.
	EntityFX s\mesh,1
	EntityBlend s\mesh,3
	
	; if you can't see the star throw it away
	If EntityInView(s\mesh,camera) = False Then FreeEntity s\mesh:Delete s
End Function

Function UpdateStars()
	For S.Star = Each Star
		; if it goes off the left hand edge jump it to the right hand edge
		If EntityInView(s\mesh,camera) = False 
			TranslateEntity s\mesh,(EntityX(camera)-EntityX(s\mesh))*2,0,0
		EndIf
	Next
End Function

Function CreateStar(Size)
	; Create a star texture
	I = CreateImage(Size,Size)
	T = CreateTexture(Size,Size)
	LockBuffer ImageBuffer(i)
	
	S = Size / 2
	
	Xpos = S
	YPos = S
	
	For x=-S To S
	For y=-S To S
		r# = ((x*x)+(y*y))
		If r<(s) Then
			c# = 255.0 - ((255.0 / (s)) * r)
			argb=(c Or (c Shl 8) Or (c Shl 16) Or ($ff000000))
			WritePixelFast x+xpos,y+ypos,argb,ImageBuffer(i)
		EndIf
	Next
	Next
	
	UnlockBuffer ImageBuffer(i)
	CopyRect 0,0,size,size,0,0,ImageBuffer(i),TextureBuffer(t)
	FreeImage i
	Return t
End Function

For n=1 To 5000
	NewStar()
Next


; move the camera to the right
Repeat
	MoveEntity camera,.5,0,0
	UpdateStars()
	RenderWorld
	Flip
Until KeyHit(1)