News:

Function Finder  Find all the functions within source code files

Main Menu

Particles Demo Using Sprites

Started by kevin, April 23, 2016, 11:36:18 AM

Previous topic - Next topic

kevin

   Particles Demo Using Sprites

    This is an older code snippet that was originally written by Crystal Noir, it creates a type of 3D particle explosion using animated 2d sprites.    I've tweaking the code and fixed some logic errors and this was the end result.  


  Particle Demo Video




  Particle Source Code

PlayBASIC Code: [Select]
; PROJECT : Artifice
; AUTHOR : Crystal Noir & Kevin Picone
; CREATED : 2/23/2010
; EDITED : 4/23/2016
; ---------------------------------------------------------------------

Setfps 60
#include "blitimage"

//Variables
GLOBAL Speed#
GLOBAL parax#
GLOBAL paray#
GLOBAL paraz#
GLOBAL Angle1#
GLOBAL Angle2#

GLOBAL switch = 0
GLOBAL NbFeu = 400
GLOBAL Temps = 0


//Types

TYPE FeuArtifice
SpriteIndex
x#
y#
z#
vy#
xd#
yd#
zd#
size#
ENDTYPE

DIM Pouf AS FeuArtifice LIST


ParticleImage=MakeBall()

Screen=NewIMage(GetScreenWidth(),GetScreenHeight(),2)

// Boucle principale
REPEAT
RendertoIMage Screen


IF Temps = 0
parax# = RNDrange(-100,100)
paray# = RNDRange(-100,100)
paraz# = 200

IF swith = 0
Temps = rndrange(20,120)
ENDIF

; pick a colour for this batch
ThisRGB=RNDRGB()
FOR i = 1 TO NbFeu
NewFeu(parax#,paray#,paraz#,ParticleImage,THisRGB)
NEXT

ENDIF

t=timer()
FOR EACH Pouf()
if AfficheFeu()=0
DELETESPRITE Pouf.SpriteIndex
Pouf = null
endif
NEXT
tt1#+=(timer()-t)
Temps--


; ender sprites
DRAWALLSPRITES


; render screen to video memory
rendertoscreen
BlitImageAlpha50Colour(Screen,0,0,$000000)


SYNC
UNTIL ESCKEY() = 1



end





//Sub & Functions
PSUB NewFeu(x#,y#,z#,ThisIMage,ThisRGB)

; Allocate new list element
Pouf = NEW FeuArtifice

; assign the size of the sprite
Pouf.size# = rnd#(0.20)*4


; CReate a sprite offset the screen
ThisSprite=NEWSPRITE(-150,-150,ThisIMage)

; init the drawmode
SPRITEDRAWMODE ThisSprite,02+16
SPRITETINT ThisSprite,ThisRGB

; store sprite Id in our list
Pouf.SpriteIndex = ThisSprite

; movement angles + speed
Angle1# = RND#(360)
Angle2# = RND#(360)
Speed# = 0.1

; Init it's position in 3d space
Pouf.x# = X#
Pouf.y# = Y#
Pouf.z# = Z#

Pouf.xd# = COS(Angle1#) * COS(Angle2#) * Speed#
Pouf.yd# = COS(Angle1#) * SIN(Angle2#) * Speed#
Pouf.zd# = SIN(Angle1#) * Speed#

Pouf.xd# *= 10.0
Pouf.yd# *= 10.0
Pouf.zd# *= 10.0

ENDPSUB



PSUB AfficheFeu()

Pouf.size# -= 0.0020
IF Pouf.size# >0

Pouf.x# += Pouf.xd#
Pouf.z# += Pouf.zd#
Pouf.y# += Pouf.yd#+Pouf.vy#
Pouf.vy# += 0.02
Login required to view complete source code




   Related Examples:

      * New Years Eve 2020 -  Firework   Particle   Animation - Source Code