Well, first of all, a particle is a low poly mesh, or a sprite, or a quad. Each particle has it's own X,Y,Z co-ords, it's own speed and life time etc etc.
So your best to set up a type collection to hold all this data in.
Type particle
Field x#,y#,z ; the x ,y ,z co-ords of the particle
Field life_time ; how long the particle will "live" for
Field timer; keeps track of how long the entity has lived
Field speed# ; the speed the particle will travel at
End Type
Ok, so now you have set up a type to store the particles, you new some code to handle them
Run the code below. Press the up arrow to generate particles :)
Please note that this is a verrrry basic example :)
Graphics3d 800,600
setbuffer backbuffer()
global cam=createcamera()
positionentity cam,0,1,-10
global light=createlight()
global cube_emittor=createcube()
global main_particle=createsphere(3)
Type particle
Field entity
Field x#,y#,z ; the x ,y ,z co-ords of the particle
Field life_time ; how long the particle will "live" for
Field timer; keeps track of how long the entity has lived
Field speed# ; the speed the particle will travel at
End Type
while not keyhit(1)
If keydown(200) then; if the up arrow is pressed then
; \/ pass across the cube_emittors X,Y,Z co-ords, a speed value, and a timer value, to create a new particle.
create_particle(entityx(cube_emittor),entityy(cube_emittor),entityz(cube_emittor),rnd(0.1,0.3),rnd(10,300))
End If
update_particles(); calling this, updates the particle, by moving it, and checks to see if it's life_time has run out.
UpdateWorld
RenderWorld
Flip
Wend
End
Function create_particle(x#,y#,z#,speed#,life_time)
p.particle=new particle
p\entity=copyentity(main_particle); copy the main_particle entity to p\entity, so the type object can use a copy of it
EntityColor p\entity,Rnd(100,250),Rnd(100,250),Rnd(100,250); give each particle a random colour
entityalpha p\entity,0.4 ; set the particle to be see thru
p\x=rnd(x-1,x+1); random x position, based on cube emittors position
p\y=y; y position, based on cube emittors position
p\z=rnd(z-1,z+1); random z position, based on cube emittors position
PositionEntity p\entity,p\x,p\y,p\z; position the entity
p\speed=speed; set speed to the speed passed across to the function
p\life_time=life_time; set life_time to the speed passed across to the function
p\timer=millisecs(); set the timer to the current value of millisecs
End Function
Function update_particles()
for p.particle=each particle
moveentity p\entity,0,p\speed,0; move the entity along the Y axis, using the speed variable
if millisecs()>p\life_time+p\timer then; check to see if the objects life_time, has ran out
freeentity p\entity; if so, then free the entity
delete p.particle; delete the type object
end if
next
End function
|