Main Menu

2D Particles Example

Started by kevin, November 12, 2007, 07:46:28 PM

Previous topic - Next topic

kevin

 2D Particles Example

 This example is showing the PBFX's (wip version) built in particle system.  

 
  Note: Requires revision PBFX V1.70c  




Openscreen 800,600,32,PBScreenMode_Fullscreen
ScreenVsync on

Constant Use_3D_Sprites =true
Constant Test_UV_Mapping =false
Constant Test_Apply_Force =true


LoadFont  "Arial",1,74,0


Dim Part as PB_Particle POinter
Part = new PB_Particle

Dim ThisPart as PB_Particle POinter




#if Use_3D_Sprites=true
Image=Make3DParticle(32,rgb(255,255,255))
#else
Image=MakeFxParticle(32,rgb(255,255,255))
#endif


max=2

Dim ColourTable(Max)

NumbOfParticles=500


; ======================================
; CReate the Particles sprites
; ======================================
For lp=1 to max
Spr=lp
CreateParticleSprite lp,NumbOfParticles

; createSprite lp
PositionSprite lp,0,0 ;rnd(400),rnd(300)

; Set the texture of this particle sprite
SpriteImage lp,Image
PositionSpriteZ spr,10
SpriteDrawmode spr,2 +16

ColourTable(lp)=rndrgb()

SpriteFaceQuantity lp,NumbOfParticles ;+100

For PartLP=0 to NumbOfParticles
SetFace(lp,PartLP,PartLP*4,rndrgb())
next PartLP

SpriteTransparent Spr,off
; SpriteBackFace Spr,off ;(Hide faces that are pointing away from the camera)

next


Mask=Rgb(20,50,60)

#if Use_3D_Sprites=false
CreateFXImage 1000,GetSCreenWidth(),GetSCreenHeight()
ImageMaskcolour 1000,Mask
#endif


Do

#if Use_3D_Sprites=true
rendertoscreen
cls mask


#else
rendertoIMage 1000
Cls Mask ;rgb(20,40,60)
#endif



For lp=1 to max

Edges=rndRange(10,50)

Part.FrameCounter =0
Part.MaxFrameCounter =100 ;rndrange(10,150)
Part.Colour =ColourTable(lp)

AngleStep#=360.0/Edges
BaseAngle#=rnd(360) ;GetSpriteAngle(lp)+70+Rnd(40)

Part.Size.X =rndrange#(30.0,40.0)
Part.Size.Y =rndrange#(30.0,40.0)
Part.Size.Z =30.0 ;rndrange#(1.0,5.0)

; Offset
Part.Handle.X =0.0 ;rndrange#(1.0,5.0)
Part.Handle.Y =0.0 ;rndrange#(1.0,5.0)
Part.Handle.Z =0.0 ;rndrange#(1.0,5.0)

; Particles Scale in
Part.Scale.X =1.0 ;rndrange#(1.0,5.0)
Part.Scale.Y =1.0 ;rndrange#(1.0,5.0)
Part.Scale.Z =1.0

; Scale Modifier lets us change the scaler each update
Part.ScaleMod.Mode = 7
Part.ScaleMod.X=-0.015
Part.ScaleMod.Y=0.015
Part.ScaleMod.Y=0.015

SetPos(Part.ScaleMod.Min,-10,-10,0)
SetPos(Part.ScaleMod.Max,10,10,0)

; Rotation Modifiers allows us to the change the Rotation (3axis) each update
Part.AngleMod.X=rndrange#(0,4)
Part.AngleMod.Y=rndrange#(0,3)
Part.AngleMod.Z=rndrange#(0,2)

SetPos(Part.AngleMod.Min,-10000,-10000,0)
SetPos(Part.AngleMod.Max,10000,10000,0)


;  Force (applied to particles position)
Part.Force.Mode = Test_Apply_Force
Part.Force.X =0.0
Part.Force.Y =0.0
Part.Force.Z =0.0

SetPos(Part.Force.Accel,0,0.015,0)
SetPos(Part.Force.Max,0,20,0)
SetPos(Part.Force.Min,0,0,0)

MaxSpeed#=50
Part.Speed.Mode=0
Part.Speed.Min.X =-(MaxSpeed#/2)
Part.Speed.Min.Y =-MaxSpeed#
; Part.Speed.Min.Z =-MaxSpeed# ; Don't need to change it's Z in 2D

Part.Speed.Max.X =MaxSpeed#
Part.Speed.Max.Y =MaxSpeed#*2
; Part.Speed.Max.Z =MaxSpeed# ; Dont need Z, were in 2D


; Set Particle TYpe to clipped 2D + ColourFade Over time
Part.TypeID=PB_ParticleType_ClipRect_XY  | PB_ParticleType_ColourFade

pad#=50
Part.ClipMin.x=0.0-Pad#
Part.ClipMin.y=0.0-pad#

Part.ClipMax.x=GetSCreenWidth()+pad#
Part.ClipMax.y=GetScreenHeight()+pad#


For MakeParts=0 to Edges-1

Part.Colour=rndrgb() ;and $ff0000

angle#=rnd#(360)
Xpos#=mousex()
Ypos#=Mousey()

Part.Pos.x =Xpos#
Part.Pos.Y =Ypos#
Part.Pos.Z =100.0 ; Important - When rotating on 3 axis, any face where a vertex have a
; negative  Z will not be drawn (Since it's behind the camera)

; Set the Constant Speed of this Particle
Speed#=rndrange#(1,6)

Part.Speed.Mode = true ; Apply force
Part.Speed.X=CosRadius(Angle#,Speed#)
Part.Speed.Y=SinRadius(Angle#,Speed#)
; Part.Speed.Z=CosRadius(Angle#,Speed#)

Angle2#=angle#+rnd(180)
Speed2#=Speed#/Part.MaxFrameCounter

Part.Speed.Accel.X=CosRadius(Angle2#,Speed2#)
Part.Speed.Accel.Y=SinRadius(Angle2#,Speed2#)
Part.Speed.Accel.Z=CosRadius(Angle2#,Speed#)


; Add a NEW particle and return it's Index.  
Index=NewSpriteParticle(lp,part)

#if Test_UV_Mapping =true

; if it's a legal index (the particle handler might be full!_
if index>0
U1#=rnd#(0.5)
V1#=rnd#(0.5)
U2#=rndrange#(u1#,1)
V2#=rndrange#(v1#,1)

PokeSpriteFaceUV lp,index,0,u1#,v1#
PokeSpriteFaceUV lp,index,1,u2#,v1#
PokeSpriteFaceUV lp,index,2,u2#,v2#
PokeSpriteFaceUV lp,index,3,u1#,v2#


; Get a pointer to the this particle in the sprite
ThisPart=GetSpriteParticlePtr(lp,Index)

; Change it's colour to 255
ThisPart.colour =255

endif
#endif
next


UpdateSpriteParticles lp

next


  DrawAllSprites

ink $ffffff


#if Use_3D_Sprites=false
rendertoscreen
drawimage 1000,0,0,false
#endif

FaceCount=0  
For lp=1 to Max
FaceCount=FaceCount+GetSpritefaceQuantity(lp)
  next

CenterText GetScreenWidth()/2,GetScreenHeight()*0.40,"2D Particles"

Sync
loop


Function SetPos(me as PBposition Pointer,x#,y#,z#)
me.x=x#
me.y=y#
me.z=z#
EndFunction




Psub SetFace(ThisSprite,ThisFace,VertexOffset,ThisColour)
PokeSpriteFaceVerts ThisSprite,ThisFace,4
PokeSpriteface ThisSprite,ThisFace,0,VertexOffset+0,0,0,ThisColour
PokeSpriteface ThisSprite,ThisFace,1,VertexOffset+1,1,0,ThisColour
PokeSpriteface ThisSprite,ThisFace,2,VertexOffset+2,1,1,ThisColour
PokeSpriteface ThisSprite,ThisFace,3,VertexOffset+3,0,1,ThisColour
EndPsub



Function Make3DParticle(Size,Col)
ThisImage=New3DImage(Size,Size)
RenderPhongImage  ThisImage,Size/2,Size/2,col,255,260/(size/2)
EndFunction ThisImage


Function MakeFXParticle(Size,Col)
ThisImage=NewFXImage(Size,Size)
RenderPhongImage  ThisImage,Size/2,Size/2,col,255,260/(size/2)
EndFunction ThisImage