Function DeleteParticles()
	For p.Particle = Each Particle
		If p\entity Then FreeEntity p\entity
		Delete p
	Next
End Function

Function MakeParticle.Particle(template, name$="", parent=0, maxticks#=1, Fx#=0, Fy#=0, Fz#=0, position=1)
	pt.Particle = New Particle
		pt\entity = CopyEntity(template)
		If name <> "" Then NameEntity pt\entity,name : pt\name = name
		EntityParent pt\entity, parent
		RotateEntity pt\entity, 0,0,0
		If position <> 0 Then
			If position = 1 Then position = template
			MoveToEntity(pt\entity,position,False)
;			PointEntity pt\entity,position,EntityRoll(position,True)
		EndIf
		pt\Fx = Fx : pt\Fy = Fy : pt\Fz = Fz
		pt\maxticks = maxticks * c_gameRate
		pt\ticks = 0
	Return pt
End Function

Function MakePixel.Particle(r#, g#, b#, name$, maxticks#=1, Fx#=0, Fy#=0, Fz#=0, position=0)
	pt.Particle = New Particle
		pt\r = r : pt\g = g : pt\b = b
		pt\name = name
		If position <> 0 Then
			pt\x = EntityX(position, True) : pt\y = EntityY(position, True) : pt\z = EntityZ(position, True)
		EndIf
		pt\Fx = Fx : pt\Fy = Fy : pt\Fz = Fz
		pt\maxticks = maxticks * c_gameRate
		pt\ticks = 0
	Return pt
End Function

Function DrawPixels()
	Color 100,100,200
	For pt.Particle = Each Particle
		If pt\entity = 0 Then
			visibility# = Float(pt\maxticks - pt\ticks) / Float(pt\maxticks)	; varies from 1.0 to 0.0
			If visibility < 0.25 Then
				Delete pt : Return		; eliminate particle
			EndIf
			
			r# = pt\r ;* visibility
			g# = pt\g ;* visibility
			b# = pt\b ;* visibility
			
			;Color Int(r),Int(g),Int(b)
			Oval3d(pt\x, pt\y, pt\z, 3, cam)
		EndIf
	Next
End Function

Function UpdateParticle(p.Particle)
	p\ticks = p\ticks + 1

	dt#   = 1.0
	ax#   = p\Fx
	ay#   = p\Fy
	az#   = p\Fz ;; F = ma, a = F/m, assume m = 1 (for now)
	
	p\Vx# = p\Vx# + (dt# * ax#)
	p\Vy# = p\Vy# + (dt# * ay#)
	p\Vz# = p\Vz# + (dt# * az#)		; velocity ~ v[t+dt] = v[t] + dt * a[t]
	
	p\x#  = p\x# + (dt# * p\Vx#)
	p\y#  = p\y# + (dt# * p\Vy#)
	p\z#  = p\z# + (dt# * p\Vz#)	; position ~ s[t+dt] = s[t] + dt * v[t]
	; MoveEntity And TranslateEntity don't work with non-entity particles
	
	p\Fx# = 0 : p\Fy# = 0 : p\Fz# = 0 	; clear the force accumulators for the next update

	If archer\state <> AT_SPACE And p\name = "helix" Then p\ticks = p\maxticks + 1

	If p\entity Then
		visibility# = Float(p\maxticks - p\ticks) / Float(p\maxticks)	; varies from 1.0 to 0.0
		If visibility# < 0.0 Then
			FreeEntity p\entity : Delete p : Return		; eliminate particle
		EndIf
		EntityAlpha p\entity,visibility/2
		
		TranslateEntity p\entity, p\Vx, p\Vy, p\Vz
		;PositionEntity p\entity, p\x#, p\y#, p\z#, True
		;DebugLog whereis$(p\entity)
	ElseIf p\ticks > p\maxticks Then
		Delete p : Return
	EndIf
End Function