How do you make fire?

Blitz3D Forums/Blitz3D Beginners Area/How do you make fire?

Crazy4Code(Posted 2005) [#1]
And no, I don't mean with sticks. But, how do you make effects such as Fire and Smoke and things like that? It's not sprites is it?


David819(Posted 2005) [#2]
well in particles such as fire and smoke you can use sprites but also meshes, to make them you need to use types and functions (well that's the best way), and just make one function to create the particles and another to update the movement of them. and example of some code below:

FlushKeys()
Print "Press the up arrow to generate particles."
Print
Print "press any key to continue!"
WaitKey()


Graphics3D 800,600
SetBuffer BackBuffer()

Global cam=CreateCamera()
PositionEntity cam,0,5,-50

Global sprite=LoadSprite("s1.png",4)
HideEntity sprite



Global no_parts=0


Type part
Field entity
Field entity1
Field time#
Field timer
Field speed#
End Type

While Not KeyHit(1)

; limit the number of particles made
If KeyDown(200) And no_parts<100 Then create_part(0,0,0,Rnd(-110,-70),Rnd(-180,180),Rnd(0.1,0.3))

update_parts() ; update the particles
UpdateWorld
RenderWorld
Flip
Wend
End

Function create_part(x#,y#,z#,pitch#,yaw#,speed#)
no_parts=no_parts+1 ; keep count of the particles
p.part=New part
p\entity=CopyEntity(sprite) ; copy the main sprite, into the type field
ScaleSprite p\entity,2,3 ; scale the particle
PositionEntity p\entity,x,y,z ; position the particle to the passed co-ords
RotateEntity p\entity,pitch#,yaw#,0 ; rotate the particles, so they have slightly different directions
EntityBlend p\entity,3 ; important for the glow effect. Set the blend mode fro the entity

p\time=Rand(1000,1000); give the particle a life span
p\timer=MilliSecs() ; take note of the value of millisecs()

p\speed=speed ; copy the speed to the type field
End Function

Function update_parts()
For p.part=Each part
MoveEntity p\entity,0,0,p\speed
EntityAlpha p\entity,(1-((MilliSecs()-p\timer)/p\time))*0.2; work out the alpha, based on the entitys remaining life span
; /\ the 0.2 makes the alpha start at a low value, and fade out. 0.2 because the blend effect is too much, with lower alpha values
If MilliSecs()>p\time+p\timer Then; if particle is nearing the end of its life

FreeEntity p\entity; free the entity

Delete p.part; delete the particle type reference
no_parts=no_parts-1 ; keep track of the number of entitys

End If
Next
End Function

Hope this helps.


Crazy4Code(Posted 2005) [#3]
Woa, that's really cool. It looks just like fire! Hopefully I'll figure out what it all means :)


_PJ_(Posted 2005) [#4]
What you have there is a simple "particle engine". 'Particles' are objects (usually Sprites) that follow set patterns for set periods of time and can often be required to continue this pattern regardless of he player input/state of gameplay. Things like Fire, rain, lightning and chimney smoke are all classic examples where particle engines may be used.

Once you have your particle image, you need to define the path that it is going to take (i.e. with rain, straight down to the floor may be enough). It is useful to 'store' particle data as Types, because no doubt you will end up creating hundreds of them, and if they are in a Type format, it is easier to 'recycle' them (i.e. when the raindrops hit the floor, they disappear and a new one begins to fall from the sky)

Hope this helps! :)


Rook Zimbabwe(Posted 2005) [#5]
I use an animated sprite usually... Or my Zippo