Particles

Blitz3D Forums/Blitz3D Programming/Particles

Ok man, sure. I'll post this though for you:

Small example. Left and right arrow keys to move the emittor:

Graphics3D 800,600
SetBuffer BackBuffer()

cam=CreateCamera()
MoveEntity cam,0,0,-30


Type particle
	Field entity
	Field time
	Field timer
	Field alive
	Field speed#
End Type


Global emittor = CreateSphere()
Global emittor_time
Global emittor_timer

Global number_of_particles = 40

For loop = 1 To number_of_particles
	p.particle = New particle
		p\entity = CreateSprite()
		p\alive=0
Next

While Not KeyHit(1)



	If KeyDown(205) Then MoveEntity emittor,0.1,0,0
	If KeyDown(203) Then MoveEntity emittor,-0.1,0,0
	
	update_emittor()
	update_particles()
	
	UpdateWorld
	RenderWorld
	Flip
Wend
End

Function update_particles()
	For p.particle = Each particle
		MoveEntity p\entity,0,p\speed,0
		
		If MilliSecs() > p\time + p\timer Then
			p\alive = 0
		End If
	Next
End Function

Function update_emittor()
	If MilliSecs() > emittor_time + emittor_timer Then ; check to see if the time has elapsed
		emittor_timer = MilliSecs() ; reset the spawn timer
		emittor_time = Rand(10,70) ; randomize the time the next one is generated
		respawn_particle() ; call the respawn particle function
	End If
End Function

Function respawn_particle()
	For p.particle = Each particle
		If p\alive = 0 Then ; check to see if a particle is dead. If so, then randomize it's fields and reset it
			PositionEntity p\entity,EntityX(emittor)+Rnd(-1,1),EntityY(emittor),EntityZ(emittor)+Rnd(-1,1)
			; the line above /\ position the particle next to the emittor but randomize it a little
			p\time = Rand(500,900)
			p\timer = MilliSecs()
			p\alive = 1 ; particle is now ALIVE
			p\speed = Rnd(0.05,0.1)
		End If
	Next
End Function