stuck on particle engine
Blitz3D Forums/Blitz3D Beginners Area/stuck on particle engine
| ||
Help :) I finaly have something decent going with this particle engine thing. Can someone advise on what to do or apply next to get different effects? the code: Graphics3D 800, 600, 32, 2 SetBuffer BackBuffer() SeedRnd MilliSecs() Global camera = CreateCamera() PositionEntity camera, 0, 0, -20; move camera back a bit Type T_Particle Field ent ;entity Field x#, y#, z# Field scale# End Type While Not KeyDown ( 1 ) If MouseDown(1) Then makeParticles("particleImage.bmp",10 ,2, 2, 4, 2 );image,totalparticles,x,y,z,scale If MouseDown(2) Then deleteParticles() moveParticles(6); make the particles move around UpdateWorld RenderWorld Text 0,0, MouseX() Text 0,10, MouseY() Flip Wend End ;; make the particles Function makeParticles(img$,numOf,x#,y#,z#,scale#) For i = 1 To numOf par.T_Particle = New T_Particle par\ent = LoadSprite (img$) par\x# = Rnd(x#) : par\y# = Rnd(y#) : par\z# = Rnd(z#) Next For par.T_Particle = Each T_Particle ScaleSprite par\ent, scale#, scale# PositionEntity par\ent, par\x#, par\y#, par\z# Next End Function ;move particles around Function moveParticles(density#) For par.T_Particle = Each T_Particle PositionEntity par\ent, par\x#, par\y#, par\z# HandleSprite par\ent, Rnd(density#), Rnd(density#) TranslateEntity par\ent, 0,0, .05 Next End Function Function deleteParticles() End Function |
| ||
eh, be back in a while |
| ||
righto here's the code Graphics3D 800, 600, 32, 2 SetBuffer BackBuffer() SeedRnd MilliSecs() Global camera = CreateCamera() PositionEntity camera, 0, 0, -20; move camera back a bit sprite=LoadSprite("particleImage.bmp",2); load sprite and apply the alpha flag to it PositionEntity sprite,50,0,0 Type T_Particle Field ent ;entity Field x#, y#, z# Field scale# Field speed# Field distance# Field maxdistance# Field alpha# End Type While Not KeyDown ( 1 ) If MouseDown(1) Then Gosub makeParticles;create a particle If MouseDown(2) Then deleteParticles(); not in use Gosub moveParticles; make the particles move around UpdateWorld RenderWorld Text 0,0, MouseX() Text 0,10, MouseY() Text 0,20," no.parts="+nopart Flip Wend End .makeParticles ;; make the particles x#=0:y#=0:z#=0:scale#=2 par.T_Particle = New T_Particle par\ent = CopyEntity(sprite); changed to copy entity instead od loadsprite par\x# = Rnd(-3,4) : par\y# = y# : par\z# =z# par\speed=Rnd(0.01,0.4) par\distance=0 par\maxdistance=Rnd(2,7) par\alpha=1 EntityAlpha par\ent,1 ScaleSprite par\ent, scale#, scale# PositionEntity par\ent, par\x#, par\y#, par\z# nopart=nopart+1 Return .moveParticles For par.T_Particle = Each T_Particle EntityAlpha par\ent,1-(par\distance/par\maxdistance); work out transparency regarding distance par\y#=par\y#+par\speed#; move particle at it own speed par\distance=par\distance+par\speed PositionEntity par\ent, par\x#, par\y#, par\z# If par\distance>par\maxdistance Then FreeEntity par\ent:Delete par.T_Particle:nopart=nopart-1 Next Return Function deleteParticles() End Function i couldn't get the code with the functions in it to work so i changed them to gosub's (sorry!!!).I've added in maxdistance, distance, alpha and speed. this means that each particle travels at it own speed and for a certain distance, different for each particle. the code also works out how long the particle has to go until it dies and fades it accordingly. soryy for changing big bit of your code, but it was the only way i could get it to work. if you don't understand any bits please post back! :D |
| ||
Hey Joker, thanks that is very nice compared to what I had :) I am going to study what you have done for a little while and comparte the differences. I guess my goal is to make a generic engine that can produce 10+ effects,I just dont know where to begin. One of them I would like to have like a water fountain, particles start at a certain point, umbrella outward in the x and z direction, and as they approach a certain y value, they get wider, and begin to fall and fade. I know this is a complicated project for me since its my first project :) but you learn a lot taking on a big project, know what I mean? Also, you may note joker, that I had x, y coordinates at the top left. Last night I was trying to get the particles to go where the mouse was clicked, but no luck with that. I wanted to understand where the x,y mouse were, but it seems like a sprite is located at different x,y cooridinates (probably the 3d world) which makes me very confused as to how to do that feat :) Thank you for your help, if you have any suggestions on how to produce multiple effects I would appreciate it (yours is nice btw). Also, changin my code is not a problem, it is not nobel prize winning heh. If you have suggestions, you dont have to code it, you can suggest the logic behind it and I could try it myself (so I could learn and get stuck, and keep posting till Im a particle master). -charlie |
| ||
one other thing, how do people post code in the *black box* format? |
| ||
well, you could have wind blowing your particles left or right. the wind variable would be changing all the time tho so it would be good to have a set value, unless when you pressed the left or right key you increased the wind affecting the object. remember to keep things a wee bit random tho. say the wind will effect every particle but it will affect some more than others, not much tho. Another idea is to turn the particles as they rise. this would be ideal for smoke and maybe fire. Regarding your water fountain idea, i don't image it would be that hard. a couple of ideas for that. First one being an animated texture of a water particle slow spreading out, again using the distance, maxdistance to work out how long till the particle dies. the second way you could do it ( i think it would be a fair bit harder too) would be to generate say 5-10 particles at a time and slow increase the distance between them. Also for the particles that are heavier than air, like water, have a gravity variable. you would substract this from the speed to have the particle rise into the air then fall due to gravity. i'll think up more ideas for ya if u want. cheers ross |
| ||
thanks joker! Sure, if you can come up with anything from simple to complex, I would appreciate it. Like I said, I want to make this engine handle a bunch of different effects for a game Id like to start working on soon. Im not going to get in specifics of the game until its done (you understand :) )-- but basically you will have 7 characters, each with 7 different spells. I will need a particle engine (preferably my code and not a pre-written particle engine) that can create these 49 spells. Im not sure of how much variation each spell will need, but If the engine is done right, possibilites would be many. One of the spells I have visioned would be a circular translucent sphere pulsating around the character as it was being cast, then i guess after or as it was cast particles will fly from the hand of the caster towards the target. The water fountain idea would be is a character was holding out his/her hand particles will begin from the palm, go upward in a narrow cone, spread, and fall. Many of these things are preliminary, but it all boils down to making a diverse engine :) So, any suggestions you may have would be appreciated. Also, I was thinking, would it be better to make separate functions for the *engine*? Meaning a function for each spell? i.e. Function castWaterFall() , Function castFireball()?? Let me know your thoughts. I am also interested in a radial particle effect opposed to the *square* look. in your example, you may notice that the bottom line is very crisp and clean and can be very distinguished. Is there a way to have particles start in a sphere and work their way to the center point and then die? Hope Im not making this seem to complicated, but maybe it is :) -charlie |
| ||
Also, on a previous post I had on a particle engine, someone suggested to create a new sprite instead of copy an existing one, what are the advantages/disadvantages? |
| ||
the point you mentioned about coming from a centre point, and the particles get bigger as they go out. About the sprites working their way out from the centre, easy just take out speed and put in par\xspeed and par\yspeed then say par\yspeed=rnd(-1,1) par\xspeed=rnd(-1,1) meaning that the particles can go in any direction, although it will be random. About the sprites being copyed and such. If you are copying a sprite and freeing it, it causes memory fragmentation, a bit like harddisc fragmentation(i think) and can slow down performance. i think the way ppl are doing it now is to recycle the sprites, say have a flag and set it to 0 if it is not in use. would take a bit of reworking, but do that last. get all the effects you want first. i'll send you this little thing. check your mail. i know you wanted to do this yerself but i'm not too good at explaining things. :D |
| ||
Hey Joker-- can you explain this line to me? par\distance=par\distance+Abs((par\xspeed*par\xspeed)+(par\yspeed*par\yspeed)) |
| ||
it basically works out how far the object has travelled.Say an object moves left 3 and up 4, it would work out the distance from before it was moved. pythagoris therom(spelling is rubbish) ...... ..E... ..^... ..^... ..<<<<S the distance between s and e = (4*4)+(3*3) |
| ||
well the pythagorian therom is this a^2 + b^2 = c^ , so the formula would be dist = sqr#( (a*a)+(b*b) ). My question is, if you throw a z value in there :) how do you figure that out *grin* -- coding for thought :) |
| ||
oh yeah, sorry. forgot about taking the square root of that. all you do z^2+x^2+y^2 all squared :) |
| ||
so to get the distance for x,y,z , you would say distance = sqr#( (x*x)+(y*y)+(z*z) ) ? |
| ||
yep |