FAO JFK - Particle System

Blitz3D Forums/Blitz3D Programming/FAO JFK - Particle System

semar(Posted 2004) [#1]
Hello jfk,
I'm referring here to your nice particle code you have posted in the code archive : http://www.blitzbasic.com/codearcs/codearcs.php?code=577

I need to parent the sprite to an entity, so to have a fired bullet with some smoke attached to it.

To do so, I've modified your CSP_LaunchParticle function in a way it accepts an entity where to attach the sprite to:

;see first parameter
Function CSP_LaunchParticle(ent = 0,fx=0, texture, nof_sprite=2, x#=0, y#=0, z#=0, min_size#=1, max_size#=3, speed#=0.1, livespan=3,blend=3,rand_rot=1,lift#=0,alpha#=1.0)


Then in the function:
If CSPfire(i)=0 Then  ; using this sprite the first time
    CSPfire(i)=CreateSprite()
    EntityParent CSPfire(i),ent ;parent the sprite to the entity ent
    CSPfire_handle(i)=CSPfire(i)
EndIf

;the rest is unchanged
;..
;..

The problem is that the prog crashes after some call to the particle function. If you want to reproduce the error, try this:
ent = createcube()
for n = 1 to 100
CSP_LaunchParticle(ent, + all the params to make smoke)
next
CSP_LaunchParticle(0,  + all the params to make fire)

On my system, it crashes after the call to "CSP_LaunchParticle(0, + all the params to make fire)", the statement right after the end of the loop.

I do the for..next loop here 100 times, to force the bug to come soon. Usually I do not use any for..next, but then the bug comes later.

If I do not parent the sprite to any entity, it works fine.

I have also tryed to parent the sprite to a pivot, and moved the entityparent statement right after the statement "CSPfire_handle(i)=CSPfire(i)", but it crashes the same. No fun also if I use simply "CSPfire(i)=CreateSprite(ent)"

Do you have any though on why this happens ? Apparently, there's no problem to parent a sprite to an entity; I've dig a bit in the function, but I don't get where is the problem. Sometime I have 'Entity does not exist' error on the statement "ShowEntity CSPfire(i)", sometime I have different errors, but all related to the "CSPfire(i)" sprite.

How would you do, if you want to parent the sprite to an entity in this particular case ?

Any help would be appreciated.

Sergio.


jfk EO-11110(Posted 2004) [#2]
Hi Sergio

Luckily I just got the source on the other monitor, so I only have to copy it quickly over a floppy disk...done.

I use the following Function, you see, the optional parent is at the end of the parameters
Function CSP_LaunchParticle(fx=0, texture, nof_sprite=2, x#=0, y#=0, z#=0, min_size#=1, max_size#=3, speed#=0.1, livespan=3,blend=3,rand_rot=1,lift#=0,alpha#=1.0, align=0, parent=0)
 min_size=min_size*(eyeheight_orig)/2
 max_size=max_size*(eyeheight_orig)/2
 ;CSP_LaunchParticle(fx, texture, n_sprites, x,y,z, min_size, max_size, speed, livespan,blendmode,random_rotation,heat-lift,main_alpha, aligntovector)
 If nof_sprite>99 Then nof_sprite=99 
 If fx=0 ; mush fx
  For i=CSP_FX_base To CSP_FX_base+nof_sprite
   If CSPfire(i)=0 Then 
    CSPfire(i)=CreateSprite()
;    SpriteViewMode CSPfire(i),2
   EndIf
   If parent<>0
    PositionEntity CSPfire(i),EntityX(parent),EntityY(parent),EntityZ(parent),1
    EntityParent CSPfire(i),parent
   Else
    PositionEntity CSPfire(i),x,y,z,1
   EndIf
   MoveEntity CSPfire(i),Rnd(-0.1,0.1),Rnd(-0.1,0.1),Rnd(-0.1,0.1)
   If align=1
    SpriteViewMode CSPfire(i),2
    AlignToVector CSPfire(i), PickedNX(), PickedNY(), PickedNZ(),3,1
    MoveEntity CSPfire(i),0,0,.1
    TurnEntity CSPfire(i),0,180,0
   Else
 ;   PointEntity CSPfire(i),player
    SpriteViewMode CSPfire(i),1
   EndIf
   CSPfire_s#(i)=1
   CSPfire_s_min#(i)=min_size
   CSPfire_s_max#(i)=max_size
   CSPfire_s_mid#(i)=CSPfire_s_min#(i)+(CSPfire_s_max#(i)-CSPfire_s_min#(i))/2
   If rand_rot<>0 Then CSPfire_a#(i)=Rand(360)
   CSPfire_al#(i)=0.0
   EntityTexture CSPfire(i),texture
   EntityBlend CSPfire(i),blend
   CSPfire_s(i)=.1+((CSPfire_s_max#(i)-CSPfire_s_min#(i))/(nof_sprite+1))*(i-CSP_FX_base)
   RotateSprite CSPfire(i),CSPfire_a(i)
   EntityAlpha CSPfire(i),0
   CSPfire_speed#(i)=speed#
   CSPfire_livespan(i)=livespan
   CSPfire_lift#(i)=lift#
   CSPfire_mainalpha#(i)=alpha#
   CSPfire_fx(i)=fx
  Next
 EndIf
 ;If fx=1 ; ?
 ; ...other initialisations + FX...your job...
 ;EndIf
 CSP_FX_base=CSP_FX_base+nof_sprite+1
 If CSP_FX_base>CSPfire_max-100 Then CSP_FX_base=0
End Function


I never experienced any problems with it. Some time ago I had true bluescreens from time to time when an app was running in Fullscreen with flip 0, probably because some top riority tasks of the system needed to "breath" from time to time. After adding a Delay 1, this was fixed.


jfk EO-11110(Posted 2004) [#3]
uh, btw:
min_size=min_size*(eyeheight_orig)/2
max_size=max_size*(eyeheight_orig)/2

this seems to be specific to my engine, to autmaticly scale them to the player size that is representative for the world scale.


semar(Posted 2004) [#4]
Thank you very much jfk !

I wanted to contact you via e-mail, but in your profile there isn't any address, that's why I've posted here.

I'm actually copying the above function, and will try today night.

[EDIT]
By teh way, I see in the above function that you have added new param "align" to the param list; what is this parameter for ? I guess the valid values are only 0 or 1 - it's a flag, isn't it ?

Will this function be compatible also with the old CSP_UpdateParticle function ?

Again, thank you very much for your kind effort and support, and congrats for such an useful and effective particle system !

:)

Sergio.


jfk EO-11110(Posted 2004) [#5]
oh yes, the align thing is used with the latest linepick from the players weapon. The reason why I use this is: when I produce a whiff of smoke when a bullet hits a wall, the smoke sprite must be aligned to the wall unless I want it to intersect the wall as soon as I don't stand in a 90°degree angle towards the wall, so freely flying particles such as energy clouds etc. are normally non-aligned (per default aligned to the camera). Sprites that are located near a mesh or on the surface of a mesh will be aligned to them. Yes, PickedNX etc. can only be used after a crosshair camerapick.

I am not sure if there are any further modifications that will make this function incompatible to the other parts of the code :) So maybe you better only use the parent part that is relative easy to extract.

BTW: Of course, when you delete the parent entity, the sprites will be killed too! This can result in an error since they are only created once, when used the first time. So you first need to unparent them befre you delete the parent mesh. For this purpose I made a special UNParent Function that I use whenever I have to delete an Entity. Using Countchildren and Getchild will help to unconnect the children. I guess that was the way I did it.


semar(Posted 2004) [#6]
Thanks a lot, that make completely sense to me.

:)

Sergio.


ryan scott(Posted 2004) [#7]
i had a similar issue with particle candy. parent/unparenting... i thought it would be better to parent than to just move it along with the object each time i moved the object.

if you don't unparent, but you delete the original object, it causes a problem.

but that's not all - i realized last night that since i've got collision detection running on the object with the trails that the emitter, and possibly all the particles!, were being included in the collision detection!

at least I think thats how it works. it depends on the internals of the particle program.

anyway, if that's the case, you definitely don't want that happening. it'll slow down your software bigtime. don't parent emitters, but move them along with the object they are supposed to be emitting from.



Function matchPOSITION(whotomove,whotocopy)
PositionEntity whotomove,EntityX(whotocopy,1),EntityY(whotocopy,1),EntityZ(whotocopy,1),1
End Function

simple enough to call this, too when you move your bullet.


jfk EO-11110(Posted 2004) [#8]
Never thought of that. Do Sprites collide at all? Anyway, I guess you can also set entitytype of the children to zero right after parenting them to something? But I agree, care should be taken.