Fluid/Path-building thingy-jig

Blitz3D Forums/Blitz3D Programming/Fluid/Path-building thingy-jig

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.

I love the bottom texture, looks like some kind of wood grain or rope.

I've done a little altering of the code. Nothing major, just something so you can turn the grid on/off and fiddled with the particle color code.

SeedRnd MilliSecs()
Graphics 800, 600, 0, 1

Const TRAILS=0

If Not trails Then SetBuffer BackBuffer()




Global MAXX	= GraphicsWidth()
Global MAXY	= GraphicsHeight()

Const GRIDX			= 40
Const GRIDY			= 30
Const CELL			= 20
Const MIDCELL		= 10

Const NUM_PARTICLES = 5000
Const PARTICLE_TURN = 15
Const PARTICLE_SPEED= 2
Const GRID_TURN 	= 1

Const RECT_SIZE     = 50
Const RECT_SPEED    = 2


Dim grid(GRIDX, GRIDY)

For y = 0 To GRIDY - 1: For x = 0 To GRIDX - 1
	grid(x, y) = Rand(360)
Next: Next


Type p_dat
	Field x#, y#
	Field ang
	Field r, g, b, dec_col
End Type

Global p.p_dat

For n=0 To NUM_PARTICLES - 1
	p.p_dat = New p_dat
		p\x = Rnd(0,MAXX-1)
		p\y = Rnd(0,MAXY-1)
		p\ang = Rnd(360)
		p\r = 64
		p\g = 64
		p\b = Rnd(64) + 192
		p\dec_col = (p\r * $FF00) + (p\g * $FF) + p\b
Next

Repeat
	If Not trails Then Flip:Cls


	; Move particles
	For p.p_dat = Each p_dat
		gx = Int(p\x / CELL)
		gy = Int(p\y / CELL)
		
		g=grid(gx, gy)
		p\ang=anglemoveto(p\ang,g,PARTICLE_TURN)
		
		;only change the grid angle if it is at least 30 degrees different from the particle's angle
		If Abs(angledif(g,p\ang))>30 Then
			grid(gx, gy)=anglemoveto(g,p\ang,GRID_TURN)
		EndIf

		
		h# = Sin(p\ang) * PARTICLE_SPEED
		v# = Cos(p\ang) * PARTICLE_SPEED
		
		p\x = p\x + h#
		p\y = p\y + v#
		
		If p\x < 0 Then
			p\x = p\x + MAXX
		ElseIf p\x >= MAXX Then
			p\x = p\x - MAXX
		EndIf
		If p\y < 0 Then
			p\y = p\y + MAXY
		ElseIf p\y >= MAXY Then
			p\y = p\y - MAXY
		EndIf
	Next



	If trails Then
		LockBuffer BackBuffer()
		For p = Each p_dat
			WritePixel p\x, p\y, p\dec_col
		Next
		UnlockBuffer BackBuffer()

		For p = Each p_dat
			movex=Sin(p\ang)*Rnd(1,RECT_SPEED)
			movey=Cos(p\ang)*Rnd(1,RECT_SPEED)
			size=Rand(2,rect_size)
			x=p\x-size/2
			y=p\y-size/2
			CopyRect x,y,size,size,x+movex,y+movey
		Next
		
	Else
		count = MilliSecs()
		
		LockBuffer BackBuffer()

		; Draw particles
		For p = Each p_dat
			WritePixel p\x, p\y, p\dec_col
		Next
		
		; Draw grid
		If grid_switch Then
			Color 44, 44, 44
			For y = 0 To GRIDY - 1: For x = 0 To GRIDX - 1
				x1 = x * CELL + MIDCELL
				y1 = y * CELL + MIDCELL
				
				x2 = x1 + Sin(grid(x, y)) * MIDCELL
				y2 = y1 + Cos(grid(x, y)) * MIDCELL
			
				Line x1, y1, x2, y2
			
			Next: Next
		End If
		
		UnlockBuffer BackBuffer()

		count = MilliSecs() - count




		
		Color 255, 255, 255

		If KeyHit(34) Then grid_switch = Not grid_switch

		If KeyHit(59) Then showhelp = Not showhelp

		If showhelp Then
			Text 10, 10, "Render time: " + count + " m/sec"
			Text 10, 30, "G   - Show/Hide Grid"
			Text 10, 46, "R   - Randomise all squares"
			Text 10, 62, "F   - Flip direction of grid"
			Text 10, 78, "Esc - quit"
		Else
			Text 10, 10, "F1 - help"
		End If
	
		If KeyHit(33) Then
			Cls
			For y = 0 To GRIDY - 1: For x=0 To GRIDX - 1
				grid(x, y) = turn * 90
			Next: Next
			turn = turn + 1
			If turn>3 Then turn=0
		End If

		If KeyHit(19) Then
			Cls
			For y = 0 To GRIDY - 1: For x=0 To GRIDX - 1
				grid(x, y) = Rnd(360)
			Next: Next
		End If	
	
	EndIf
	
Until KeyHit(1)
End




;makes the angle a little closer to the target angle and returns it
Function AngleMoveTo#(anglenow#,angletar#,amount#)

	amount=amount*Sgn(angletar-anglenow)

	If Abs(angletar-anglenow)>180 Then
		anglenow=anglenow-amount
	Else
		anglenow=anglenow+amount
	EndIf
	
	If anglenow<0 Then
		anglenow=anglenow+360
	ElseIf anglenow>360 Then
		anglenow=anglenow-360
	EndIf

	Return anglenow
End Function



;how far away two angles are, assuming they can skip over the end of anglemax to get to 0
;gives positive and negative return values
Function AngleDif#(anglenow#,angletar#,anglemax=360)
	Local temp#

	temp#=angletar-anglenow
	If temp>+anglemax/2 Then Return temp-anglemax
	If temp<-anglemax/2 Then Return temp+anglemax
	Return temp
End Function