fire

BlitzPlus Forums/BlitzPlus Programming/fire

SSS(Posted 2003) [#1]
here is a little fire thing i made, it doesnt work amasingly well but im gunna make it into a set of functions but wanted people just to tell me it this is something they think i should work on :)

Graphics 640,480

Global i = CreateImage(100,100)
Dim cooling(100,100)
For x = 0 To 100
For y = 0 To 100
	cooling(x,y)=3
Next
Next 
cooling(50,80) = 4

SetBuffer ImageBuffer(i)
Color 255,0,0
Rect 50,97,10,3
SetBuffer BackBuffer()

While Not KeyDown(1)
SetBuffer ImageBuffer(i)
Color 255,0,0
Rect 50,97,10,3
SetBuffer BackBuffer()
LockBuffer ImageBuffer(i)
For x = 2 To 98
 	For y = 2 To 98
		top = ReadPixelFast(x,y-1,ImageBuffer(i))
		bot = ReadPixelFast(x,y+1,ImageBuffer(i))
		lleft = ReadPixelFast(x+1,y,ImageBuffer(i))
		rright = ReadPixelFast(x-1,y,ImageBuffer(i))
		If GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright)
		WritePixelFast x,y-1,Combine((GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright))/4-cooling(x,y),0,0),ImageBuffer(i)
		EndIf
	Next
Next
UnlockBuffer ImageBuffer(i)
DrawImage i,0,0
Flip
Wend
		
		
		
		
		
		
Function GetRed(col)	
Return (col Shr 16) And $FF 
End Function 
Function GetGreen(col)
Return  (col Shr 8) And $FF
End Function 
Function GetBlue(col)
Return col And $FF 
End Function 

Function Combine(r,g,b)
Return (b Or (g Shl 8) Or (r Shl 16) Or (255 Shl 24)) 
End Function 



Valgar(Posted 2003) [#2]
I think that with a bit of "tweaking" the effect is very good!
If you are able to add some "noise"effects (like changing wind)at the particles and change the color of the flame,then the code is good to put in an rpg game :)


SSS(Posted 2003) [#3]
i have tried to impliment noise but and move it but it does not seem to have any effect, if you could see what was wrong with my code i would be very gratefull
Graphics 640,480

Global img = CreateImage(100,100)
Dim cooling(100,100)
For x# = 0 To 1 Step 0.01
For y# = 0 To 1 Step 0.01
	cooling(x#*100,y#*100) = PerlinNoise_2D#(x#, y#)/700
	Next
Next 

SetBuffer ImageBuffer(img)
Color 255,0,0
Rect 50,98,10,2
SetBuffer BackBuffer()
frame = 0
While Not KeyDown(1)
SetBuffer ImageBuffer(img)
Color 255,0,0
Rect 50,98,10,2
SetBuffer BackBuffer()
Cls
LockBuffer ImageBuffer(img)
For x = 2 To 98
 	For y = 2 To 98
		top = ReadPixelFast(x,y-1,ImageBuffer(img))
		bot = ReadPixelFast(x,y+1,ImageBuffer(img))
		lleft = ReadPixelFast(x+1,y,ImageBuffer(img))
		rright = ReadPixelFast(x-1,y,ImageBuffer(img))
		If GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright) > 0
		WritePixelFast x,y-1,Combine((GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright))/4-cooling(x,y),0,0),ImageBuffer(img)
		EndIf
	Next
Next
UnlockBuffer ImageBuffer(img)

For x = 0 To 100
	For y = 0 To 100
		temp1 = cooling(x,y)
		cooling(x,y) = temp
		temp = temp1
	Next
Next
cooling(0,0) = cooling(99,99)

frame = frame+1 Mod 5
		 
DrawImage img,0,0
Text 10,10,cooling(60,60)
Flip
Wend
		
		
		
		
		
		
Function GetRed(col)	
Return (col Shr 16) And $FF 
End Function 
Function GetGreen(col)
Return  (col Shr 8) And $FF
End Function 
Function GetBlue(col)
Return col And $FF 
End Function 

Function Combine(r,g,b)
Return (b Or (g Shl 8) Or (r Shl 16) Or (255 Shl 24)) 
End Function


 Function Noise#(x, y)
    n = x + y * 57
SeedRnd n
  Return Rand(255)
End Function

Function tNoise#(x, y) 
n = x + y * 57
n = (n And 13) ^ n;
Return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) ) / 1073741824.0)
End Function 

  Function SmoothNoise_1#(x#, y#)
    corners# = ( Noise(x#-1, y#-1)+Noise(x#+1, y#-1)+Noise(x#-1, y#+1)+Noise(x#+1, y#+1) ) / 16
    sides#   = ( Noise(x#-1, y#)  +Noise(x#+1, y#)  +Noise(x#, y#-1)  +Noise(x#, y#+1) ) /  8
    center # =  Noise(x#, y#) / 4
    Return corners# + sides# + center#
  End Function

  Function Interpolate2D#(x#, y#)

      integer_X    = Int(x)
      fractional_X# = x# - integer_X

      integer_Y    = Int(y)
      fractional_Y# = y# - integer_Y

      v1# =SmoothNoise_1#(integer_X,     integer_Y)
      v2# = SmoothNoise_1#(integer_X + 1, integer_Y)
      v3# = SmoothNoise_1#(integer_X,     integer_Y + 1)
      v4# =SmoothNoise_1#(integer_X + 1, integer_Y + 1)

      i1# = Interpolate(v1# , v2 #, fractional_X#)
      i2# = Interpolate(v3# , v4 #, fractional_X#)

      Return Interpolate#(i1# , i2# , fractional_Y#)

  End Function


  Function PerlinNoise_2D#(x#, y#)

      total# = 0
      p# = 10
      n #= 10 - 1

      For  i = 0 To n

          frequency #= 2*i
          amplitude# = Pi

          total #= total# + Interpolate2D#(x# * frequency#, y# * frequency#) * amplitude#

      Next

      Return total#

  End Function


Function Interpolate#(a,b,x#)
	ft# = x#*Pi
	f# = (1-Cos(ft#))*0.5
	
	Return a*(1-x)+b*f#
End Function




SSS(Posted 2003) [#4]
i have tried to impliment noise but and move it but it does not seem to have any effect, if you could see what was wrong with my code i would be very gratefull
Graphics 640,480

Global img = CreateImage(100,100)
Dim cooling(100,100)
For x# = 0 To 1 Step 0.01
For y# = 0 To 1 Step 0.01
	cooling(x#*100,y#*100) = PerlinNoise_2D#(x#, y#)/700
	Next
Next 

SetBuffer ImageBuffer(img)
Color 255,0,0
Rect 50,98,10,2
SetBuffer BackBuffer()
frame = 0
While Not KeyDown(1)
SetBuffer ImageBuffer(img)
Color 255,0,0
Rect 50,98,10,2
SetBuffer BackBuffer()
Cls
LockBuffer ImageBuffer(img)
For x = 2 To 98
 	For y = 2 To 98
		top = ReadPixelFast(x,y-1,ImageBuffer(img))
		bot = ReadPixelFast(x,y+1,ImageBuffer(img))
		lleft = ReadPixelFast(x+1,y,ImageBuffer(img))
		rright = ReadPixelFast(x-1,y,ImageBuffer(img))
		If GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright) > 0
		WritePixelFast x,y-1,Combine((GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright))/4-cooling(x,y),0,0),ImageBuffer(img)
		EndIf
	Next
Next
UnlockBuffer ImageBuffer(img)

For x = 0 To 100
	For y = 0 To 100
		temp1 = cooling(x,y)
		cooling(x,y) = temp
		temp = temp1
	Next
Next
cooling(0,0) = cooling(99,99)

frame = frame+1 Mod 5
		 
DrawImage img,0,0
Text 10,10,cooling(60,60)
Flip
Wend
		
		
		
		
		
		
Function GetRed(col)	
Return (col Shr 16) And $FF 
End Function 
Function GetGreen(col)
Return  (col Shr 8) And $FF
End Function 
Function GetBlue(col)
Return col And $FF 
End Function 

Function Combine(r,g,b)
Return (b Or (g Shl 8) Or (r Shl 16) Or (255 Shl 24)) 
End Function


 Function Noise#(x, y)
    n = x + y * 57
SeedRnd n
  Return Rand(255)
End Function

Function tNoise#(x, y) 
n = x + y * 57
n = (n And 13) ^ n;
Return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) ) / 1073741824.0)
End Function 

  Function SmoothNoise_1#(x#, y#)
    corners# = ( Noise(x#-1, y#-1)+Noise(x#+1, y#-1)+Noise(x#-1, y#+1)+Noise(x#+1, y#+1) ) / 16
    sides#   = ( Noise(x#-1, y#)  +Noise(x#+1, y#)  +Noise(x#, y#-1)  +Noise(x#, y#+1) ) /  8
    center # =  Noise(x#, y#) / 4
    Return corners# + sides# + center#
  End Function

  Function Interpolate2D#(x#, y#)

      integer_X    = Int(x)
      fractional_X# = x# - integer_X

      integer_Y    = Int(y)
      fractional_Y# = y# - integer_Y

      v1# =SmoothNoise_1#(integer_X,     integer_Y)
      v2# = SmoothNoise_1#(integer_X + 1, integer_Y)
      v3# = SmoothNoise_1#(integer_X,     integer_Y + 1)
      v4# =SmoothNoise_1#(integer_X + 1, integer_Y + 1)

      i1# = Interpolate(v1# , v2 #, fractional_X#)
      i2# = Interpolate(v3# , v4 #, fractional_X#)

      Return Interpolate#(i1# , i2# , fractional_Y#)

  End Function


  Function PerlinNoise_2D#(x#, y#)

      total# = 0
      p# = 10
      n #= 10 - 1

      For  i = 0 To n

          frequency #= 2*i
          amplitude# = Pi

          total #= total# + Interpolate2D#(x# * frequency#, y# * frequency#) * amplitude#

      Next

      Return total#

  End Function


Function Interpolate#(a,b,x#)
	ft# = x#*Pi
	f# = (1-Cos(ft#))*0.5
	
	Return a*(1-x)+b*f#
End Function




Valgar(Posted 2003) [#5]
I'm sorry but i'm at too low level for coding stuff like yours :)
I try to explain it in words.
Actually your flame have the particles that go only up,if you make them go left and right (to simulate a sort of wind,like if you lit a torch and put it near a source of wind and then you move the torch).
I think that a repetitive movement from left to right and so on would be perfect.
Did you try to use only the pixel command(to draw the pixel)with the rnd function at a lower seed and make each particle with different "life"?(how long a particle stay visible...)


SSS(Posted 2003) [#6]
YES i got it looking pritty good with nothing fancy :)

i actually think it looks ok now please check it out
Graphics 640,480

img = CreateImage(100,100)
SetBuffer ImageBuffer(img)
Color 255,0,0
Rect 50,98,10,2
SetBuffer BackBuffer()
frame = 0
While Not KeyDown(1)
SetBuffer ImageBuffer(img)
Color 255,0,0
Rect 50,98,10,2
SetBuffer BackBuffer()
Cls
LockBuffer ImageBuffer(img)
For x = 2 To 98
 	For y = 2 To 98
		top = ReadPixelFast(x,y-1,ImageBuffer(img))
		bot = ReadPixelFast(x,y+1,ImageBuffer(img))
		lleft = ReadPixelFast(x+1,y,ImageBuffer(img))
		rright = ReadPixelFast(x-1,y,ImageBuffer(img))
		If (GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright))/4-2 > 0
		WritePixelFast x+Rand(-1,1),y-1,Combine((GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright))/4-2,0,0),ImageBuffer(img)
		EndIf
	Next
Next
UnlockBuffer ImageBuffer(img)



frame = frame+1 Mod 5
		 
DrawImage img,0,0

Flip
Wend
		
		
		
		
		
		
Function GetRed(col)	
Return (col Shr 16) And $FF 
End Function 
Function GetGreen(col)
Return  (col Shr 8) And $FF
End Function 
Function GetBlue(col)
Return col And $FF 
End Function 

Function Combine(r,g,b)
Return (b Or (g Shl 8) Or (r Shl 16) Or (255 Shl 24)) 
End Function



Valgar(Posted 2003) [#7]
I take this code from the code archive with my blitz3d package....
If you use numpad 4&6 you change the wind direction!
I think that this effect is ok for your particle flame!

;Blitz rain effect
;written by Ed Upton

;play with this for light or heavy rain!!!
Global drop_number=200

Graphics 640,480
SetBuffer BackBuffer()

Type drop
Field x,y
Field angle,col
End Type

Global wind

placedrops()

While Not KeyDown(1)
Cls
update()
If KeyDown(75)=1 Then wind=wind-25
If KeyDown(77)=1 Then wind=wind+25
If wind>3000 Then wind=3000
If wind<-3000 Then wind=-3000
Color 255,255,255
Text 0,0,"Wind speed:"+wind
Text 320,460,"Numpad 4 and 6 adjust wind speed",1
Flip
Wend
End

;place drops on the screen
Function placedrops()
For n=0 To drop_number
drip.drop = New drop
al=Rnd(2048)
drip\x=al-704
drip\y=Rnd(380)
drip\angle=0 ;straight down (This is modified by wind)
drip\col=Rnd(4)
Next
End Function

Function update()
;adjust the 12 at the end of the Sin and Cos lines for faster rain
For drip.drop = Each drop
xx=Sin((2*(drip\angle+wind))*Pi/360)*12
yy=Cos((2*(drip\angle+wind))*Pi/360)*12
If drip\col=0 Then Color 200,200,200 Else
If drip\col=1 Then Color 150,150,150 Else
If drip\col=2 Then Color 100,100,100 Else
If drip\col=3 Then Color 50,50,50
Line drip\x,drip\y,drip\x+(xx/2),drip\y+(yy/2)
drip\x=drip\x+xx
drip\y=drip\y+yy
If drip\y>430
percent=Rnd(100)
If percent<50 Or drip\y>475
al=Rnd(2048)
drip\x=al-704
drip\y=Rnd(100)
drip\angle=0 ;straight down (This is modified by wind)
EndIf
EndIf
Next
End Function


Valgar(Posted 2003) [#8]
I see your last effect and is very cooooool!
I think that the speed of the effect increase also...
Now the last tweak possible is to paint some pixel yellow!
Good work,i think it's very good for a type of "animated tile" to put on a map :)


SSS(Posted 2003) [#9]
here is one last version before i go to sleep, 1 am

hold down the mouse in the white box to draw fire, its pritty cool, if you draw at the very bottom the fire stays and keeps going so you can make neet effects, its been transformed to 3d which is making it MUCH faster :)

have fun with it
il make a function set for it 2maro

Graphics3D 640,480

img = CreateTexture(100,100)

SetBuffer BackBuffer()
frame = 0
cooling = 2

entity = CreateSprite()
camera = CreateCamera()
PositionEntity camera,0,0,-1
EntityTexture entity,img
While Not KeyDown(1)

SetBuffer BackBuffer()
Cls
If MouseDown(1) 

SetBuffer TextureBuffer(img)
Color 255,255,255
Rect MouseX()-5,MouseY()-5,5,5
SetBuffer BackBuffer()
EndIf
LockBuffer TextureBuffer(img)
For x = 2 To 98
 	For y = 2 To 98
		top = ReadPixelFast(x,y-1,TextureBuffer(img))
		bot = ReadPixelFast(x,y+1,TextureBuffer(img))
		lleft = ReadPixelFast(x+1,y,TextureBuffer(img))
		rright = ReadPixelFast(x-1,y,TextureBuffer(img))
		If (GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright))/4-cooling  > 0
		WritePixelFast x+Rand(-1,1),y-1,Combine((GetRed(top)+GetRed(bot)+GetRed(lleft)+GetRed(rright))/4-cooling ,0,0),TextureBuffer(img)
		EndIf
	Next
Next
UnlockBuffer TextureBuffer(img)

If frame = 100
	frame = 0
	cooling = Rand(1,5)
EndIf
frame = frame+1
	 

RenderWorld()
Rect 0,0,100,100

Flip
Wend
		
		
		
		
		
		
Function GetRed(col)	
Return (col Shr 16) And $FF 
End Function 
Function GetGreen(col)
Return  (col Shr 8) And $FF
End Function 
Function GetBlue(col)
Return col And $FF 
End Function 

Function Combine(r,g,b)
Return (b Or (g Shl 8) Or (r Shl 16) Or (255 Shl 24)) 
End Function



Valgar(Posted 2003) [#10]
I like the previous version :)
But i think the last it's better for dinamic stuff.