Ooops
November 28, 2020, 10:33:48 AM

Author Topic: [bb] Simple particles by H. T. U. [ 1+ years ago ]  (Read 763 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Simple particles by H. T. U. [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Simple particles
Author : H. T. U.
Posted : 1+ years ago

Description : A list of particle functions (examples here: <a href="../Community/posts28e2.html?topic=82290" target="_blank">http://blitzbasic.com/Community/posts.php?topic=82290[/url] ).  Uses mostly Blitz commands for manipulating particles and emitters.  Great for beginners and simple effects.

Limitations:  For Blitz commands to be compatible with this system, each particle is a separate entity (and consequently surface) and requires For/Next statements to select each particle and emitter.  Therefore it is best for simple effects only.

Well, enjoy!


Code :
Code: BlitzBasic
  1. Global totalparticles;Total number of particles ever created
  2.  
  3.  
  4.  
  5. Type emitter
  6.        
  7.         Field pt
  8.         Field mx#,my#,mz#
  9.         Field rmx#,rmy#,rmz#
  10.         Field pitch#,yaw#,roll#
  11.         Field rpitch#,ryaw#,rroll#
  12.         Field fade
  13.        
  14. End Type
  15.  
  16. Type particle
  17.        
  18.         Field sprite
  19.         Field emitter
  20.         Field mx#,my#,mz#
  21.         Field pitch#,yaw#,roll#
  22.         Field alpha#
  23.         Field fade
  24.         Field life
  25.         Field ctime
  26.         Field id
  27.        
  28. End Type
  29.  
  30.  
  31.  
  32.  
  33.  
  34. Function createemitter(mx#,my#,mz#,pitch#,yaw#,roll#,rm#=0,rr#=0,fade=True);Create an emitter
  35.  
  36.        
  37.         e.emitter=New emitter
  38.         ept=CreatePivot();emitter entity
  39.         ;particle motion and rotation(including random amounts)
  40.         emx=mx
  41.         emy=my
  42.         emz=mz
  43.         e
  44. mx=rm
  45.         e
  46. my=rm
  47.         e
  48. mz=rm
  49.         epitch=pitch
  50.         eyaw=yaw
  51.         e
  52. oll=roll
  53.         e
  54. pitch=rr
  55.         e
  56. yaw=rr
  57.         e
  58. roll=rr
  59.         efade=fade;particle fading
  60.  
  61.  
  62.        
  63.         Return ept
  64.        
  65. End Function
  66.  
  67. Function createparticle(emitter,life#);Create a particle
  68.         ;get the emitter
  69.         For e.emitter=Each emitter
  70.                 If ept=emitter
  71.                         a=Handle(e.emitter)
  72.                 EndIf
  73.         Next
  74.         emtr.emitter=Object.emitter(a)
  75.        
  76.         If emtr.emitter=Null RuntimeError "Emitter doesn't exist";stop the program if the emitter doesn't exist
  77.                        
  78.                         totalparticles=totalparticles+1;update the particle count
  79.                        
  80.                         TFormVector emtrmx,emtrmy,emtrmz,emtrpt,0;keep the particle on track no matter what the emitter's rotation
  81.                                                
  82.                         p.particle=New particle
  83.                         psprite=CreateSprite()
  84.                         pemitter=emitter
  85.                         ;particle movement and rotation values 
  86.                         pmx=TFormedX()+Rnd(-(emtr
  87. mx),emtr
  88. mx)
  89.                         pmy=TFormedY()+Rnd(-(emtr
  90. my),emtr
  91. my)
  92.                         pmz=TFormedZ()+Rnd(-(emtr
  93. mz),emtr
  94. mz)
  95.                         ppitch=emtrpitch+Rnd(-(emtr
  96. pitch),emtr
  97. pitch)
  98.                         pyaw=emtryaw+Rnd(-(emtr
  99. yaw),emtr
  100. yaw)
  101.                         p
  102. oll=emtr
  103. oll+Rnd(-(emtr
  104. roll),emtr
  105. roll)
  106.                         palpha=1
  107.                         ;particle fading and life values
  108.                         pfade=emtrfade
  109.                         plife=life
  110.                         pctime=MilliSecs()
  111.                         pid=totalparticles;used for getparticle
  112.  
  113.                         PositionEntity psprite,EntityX(emtrpt),EntityY(emtrpt),EntityZ(emtrpt)
  114.                
  115.                         Return psprite
  116.  
  117. End Function
  118.  
  119. Function particlealpha(particle,alpha#);changes the particles base alpha (required for proper fading)
  120.  
  121.         For p.particle=Each particle
  122.                 If psprite=particle palpha=alpha;set the particle's base alpha to alpha
  123.         Next
  124.        
  125. End Function
  126.  
  127. Function updateparticles();update every particle's movement, rotation, and fading
  128.  
  129.         For p.particle=Each particle
  130.                 If p.particle<>Null
  131.                
  132.                         TranslateEntity psprite,pmx,pmy,pmz
  133.                         TurnEntity psprite,ppitch,pyaw,p
  134. oll
  135.                
  136.                         If pfade=True;if the particle is supposed to fade out of existence
  137.                        
  138.                                 alpha#=palpha-(palpha*(MilliSecs()-pctime)/plife)
  139.  
  140.                         EndIf
  141.                         EntityAlpha psprite,alpha
  142.                        
  143.                         If alpha<=0;if the particle's alpha is zero or less
  144.                                 freeparticle(psprite)
  145.                         EndIf
  146.                                        
  147.                 EndIf
  148.         Next
  149.        
  150. End Function
  151.  
  152. Function countparticles(emitter);returns the number of particles produced by an emitter currently in existence
  153.        
  154.         For p.particle=Each particle
  155.                 If pemitter=emitter;if emitter is the particle's emitter
  156.                         cnt=cnt+1
  157.                 EndIf                  
  158.         Next
  159.        
  160.         Return cnt
  161.                                
  162. End Function
  163.  
  164. Function getparticle(id);
  165.  
  166.         For p.particle=Each particle
  167.                 If p.particle<>Null And pid=id;if the particle exists and it's id equals id                    
  168.                         Return psprite
  169.                 EndIf
  170.         Next                   
  171.  
  172. End Function
  173.  
  174. Function partmovex#(particle);returns a particle's x movement
  175.                
  176.         For p.particle=Each particle
  177.                 If p.particle<>Null And psprite=particle;if the particle exists and it's sprite is the supplied particle
  178.                         Return pmx
  179.                 EndIf
  180.         Next
  181.        
  182. End Function
  183.  
  184. Function partmovey#(particle);returns a particle's y movement
  185.                
  186.         For p.particle=Each particle
  187.                 If p.particle<>Null And psprite=particle;if the particle exists and it's sprite is the supplied particle
  188.                         Return pmy
  189.                 EndIf
  190.         Next
  191.        
  192. End Function
  193.  
  194. Function partmovez#(particle);returns a particle's z movement
  195.                
  196.         For p.particle=Each particle
  197.                 If p.particle<>Null And psprite=particle;if the particle exists and it's sprite is the supplied particle
  198.                         Return pmz
  199.                 EndIf
  200.         Next
  201.        
  202. End Function
  203.  
  204. Function partpitch#(particle);returns a particle's pitch rotation
  205.                
  206.         For p.particle=Each particle
  207.                 If p.particle<>Null And psprite=particle;if the particle exists and it's sprite is the supplied particle
  208.                         Return ppitch
  209.                 EndIf
  210.         Next
  211.        
  212. End Function
  213.  
  214. Function partyaw#(particle);returns a particle's yaw rotation
  215.                
  216.         For p.particle=Each particle
  217.                 If p.particle<>Null And psprite=particle;if the particle exists and it's sprite is the supplied particle
  218.                         Return pyaw
  219.                 EndIf
  220.         Next
  221.        
  222. End Function
  223.  
  224. Function partroll#(particle);returns a particle's roll rotation
  225.                
  226.         For p.particle=Each particle
  227.                 If p.particle<>Null And psprite=particle;if the particle exists and it's sprite is the supplied particle
  228.                         Return p
  229. oll
  230.                 EndIf
  231.         Next
  232.        
  233. End Function
  234.  
  235. Function modifyparticle(particle,mx#,my#,mz#,pitch#=0,yaw#=0,roll#=0,life=1000,fade=True,rel=False);changes a particle after it's created
  236.  
  237.         For p.particle=Each particle
  238.                 If p.particle<>Null And psprite=particle;if the particle exists and it's sprite is the supplied particle
  239.                         If rel=False;if the change isn't relative
  240.                                 pmx=mx
  241.                                 pmy=my
  242.                                 pmz=mz
  243.                                 ppitch=pitch
  244.                                 pyaw=yaw
  245.                                 p
  246. oll=roll
  247.                                 plife=life
  248.                                 pfade=fade
  249.                         Else;if it is
  250.                                 pmx=pmx+mx
  251.                                 pmy=pmy+my
  252.                                 pmz=pmz+mz
  253.                                 ppitch=ppitch+pitch
  254.                                 pyaw=pyaw+yaw
  255.                                 p
  256. oll=p
  257. oll+roll
  258.                                 plife=plife+life
  259.                                 If fade=False pfade=Not pfade
  260.                         EndIf
  261.                 EndIf
  262.         Next
  263.        
  264. End Function
  265.  
  266. Function freeparticle(particle);removes a particle
  267.  
  268.         For p.particle=Each particle
  269.                 If psprite=particle
  270.                         FreeEntity psprite
  271.                         Delete p
  272.                 EndIf
  273.         Next
  274.  
  275. End Function
  276.  
  277. Function modifyemitter(emitter,mx#,my#,mz#,pitch#=0,yaw#=0,roll#=0,rm#=0,rr#=0,fade=True,rel=False);changes an emitter after it's created
  278.         ;get the emitter
  279.         For e.emitter=Each emitter             
  280.                 If ept=emitter
  281.                         a=Handle(e.emitter)
  282.                 EndIf
  283.         Next
  284.         emtr.emitter=Object.emitter(a)
  285.  
  286.         If emtr.emitter=Null RuntimeError "Emitter doesn't exist";stop the program if the emitter doesn't exist
  287.  
  288.         If rel=False;if the change isn't relative
  289.                 emtrmx=mx
  290.                 emtrmy=my
  291.                 emtrmz=mz
  292.                 emtr
  293. mx=rm
  294.                 emtr
  295. my=rm
  296.                 emtr
  297. mz=rm
  298.                 emtrpitch=pitch
  299.                 emtryaw=yaw
  300.                 emtr
  301. oll=roll
  302.                 emtr
  303. pitch=rr
  304.                 emtr
  305. yaw=rr
  306.                 emtr
  307. roll=rr
  308.                 emtrfade=fade
  309.         Else;if it is
  310.                 emtrmx=emtrmx+mx
  311.                 emtrmy=emtrmy+my
  312.                 emtrmz=emtrmz+mz
  313.                 emtr
  314. mx=emtr
  315. mx+rm
  316.                 emtr
  317. my=emtr
  318. my+rm
  319.                 emtr
  320. mz=emtr
  321. mz+rm
  322.                 emtrpitch=emtrpitch+pitch
  323.                 emtryaw=emtryaw+yaw
  324.                 emtr
  325. oll=emtr
  326. oll+roll
  327.                 emtr
  328. pitch=emtr
  329. pitch+rr
  330.                 emtr
  331. yaw=emtr
  332. yaw+rr
  333.                 emtr
  334. roll=emtr
  335. roll+rr
  336.                 If fade=True emtrfade=Not emtrfade
  337.         EndIf  
  338. End Function
  339.  
  340. Function modifyemitterrnd(emitter,rmx#,rmy#,rmz#,rpitch#,ryaw#,rroll#);change an emitter's random values in depth
  341.         ;get the emitter
  342.         For e.emitter=Each emitter             
  343.                 If ept=emitter
  344.                         a=Handle(e.emitter)
  345.                 EndIf
  346.         Next
  347.         emtr.emitter=Object.emitter(a)
  348.        
  349.         emtr
  350. mx=rmx
  351.         emtr
  352. my=rmy
  353.         emtr
  354. mz=rmz
  355.         emtr
  356. pitch=rpitch
  357.         emtr
  358. yaw=ryaw
  359.         emtr
  360. roll=rroll
  361.        
  362. End Function
  363.  
  364. Function freeemitter(emitter);removes an emitter
  365.        
  366.         For e.emitter=Each emitter
  367.                 If ept=emitter
  368.                         FreeEntity ept
  369.                         Delete e
  370.                 EndIf
  371.         Next
  372.        
  373. End Function
  374.  
  375. Function clearparticlesystem();removes the entire particle system
  376.  
  377.         For e.emitter=Each emitter
  378.                 If e<>Null
  379.                         If ept=True FreeEntity ept
  380.                         Delete e
  381.                 EndIf
  382.         Next
  383.        
  384.         For p.particle=Each particle
  385.                 If p<>Null
  386.                         If psprite=True FreeEntity psprite
  387.                         Delete p
  388.                 EndIf
  389.         Next
  390.  
  391. End Function


Comments :


BlitzSupport(Posted 1+ years ago)

 Hello HTU,I suspect that the reason you're having problems is simply because (I assume) you don't realise you can return any type from a function, including user-defined types such as your 'entity' and 'particle' types.For instance:
Code: [Select]
Function createemitter (mx#,my#,mz#,pitch#,yaw#,roll#,rm#=0,rr#=0,fade=True);Create an emitter

e.emitter=New emitter
ept=CreatePivot();emitter entity

; etc

Return ept

End Function
Here, you're creating e.emitter in your function, then returning its pivot field, instead of just returning e.emitter. You should modify this to return the e.emitter you've created, then pass 'e.emitter' to your other functions -- note that the function name has the type appended below, ie .emitter:
Code: [Select]
Function createemitter<b>.emitter</b> (mx#,my#,mz#,pitch#,yaw#,roll#,rm#=0,rr#=0,fade=True);Create an emitter

e.emitter=New emitter
ept=CreatePivot();emitter entity

; etc

Return <b>e</b> ; Not ept

End Function
Note that the call would have to be made as myemitter.emitter = createemitter (x,y,z), ie. using the .emitter type to receive the result.Then, for any functions that use the returned value from createemitter...Old:
Code: [Select]
Function freeemitter(emitter);removes an emitter

For e.emitter=Each emitter
If ept=emitter
FreeEntity ept
Delete e
EndIf
Next

End Function
New:
Code: [Select]
Function freeemitter(e.emitter);removes an emitter

If e
Delete e
EndIf

End Function
Note that the parameter's type is defined as an emitter, ie. you should pass the .emitter type you received from createemitter ().As you'll see, this removes all the For/Next checks too. You probably won't need the Object/Handle calls either if you do this.The same goes for your particle type and its associated functions.


H. T. U.(Posted 1+ years ago)

 Thanks, but I returned the entity on purpose so that it would be easy for beginners, without having to deal with particle types in their main code.  This makes it somewhat slow, but it's more of a intro to particles.


Chroma(Posted 1+ years ago)

 Heck you dont' want to teach them bad habits now do you? :)I agree on returning the type.


Jeppe Nielsen(Posted 1+ years ago)

 Your could return the the entity (pivot) and set the name to the type handle:
Code: [Select]

Function createemitter (mx#,my#,mz#,pitch#,yaw#,roll#,rm#=0,rr#=0,fade=True);Create an emitter

e.emitter=New emitter
ept=CreatePivot();emitter entity
        NameEntity ept,Handle(e); <-- notice this

; etc

Return ept

End Function
And to delete:
Code: [Select]

Function freeemitter(emitter);removes an emitter

        e.emitter = Object.emitter(EntityName(emitter))

FreeEntity emitter
Delete e

End Function

thus removing the foreach loop also.This is what I do in my particle systems in blitz3d [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal