Fireworks effect

Previous topic - Next topic

Crystal Noir

Hi there  :happy:

I'm happy to start here with a small snippet. Here's a fireworks effect that use alpha blend and sprites :)

To start with you will need to download this picture :



Here's the code !

Code (glbasic) Select



// Variables setup
GLOBAL MonSprite = 0
GLOBAL Vitesse = 0.1
GLOBAL parax
GLOBAL paray
GLOBAL paraz
GLOBAL coulr
GLOBAL coulg
GLOBAL coulb
GLOBAL angle1
GLOBAL angle2

GLOBAL switche = 0
GLOBAL nbfeu = 300

GLOBAL temps = 0
GLOBAL quit = 0
GLOBAL posx
GLOBAL posy

// Types
TYPE FeuArtifice
x
y
z
vy
xd
yd
zd
r
g
b
alpha
factaille
taillex
tailley
ENDTYPE

GLOBAL MaListe[] AS FeuArtifice


// Sprites
SETTRANSPARENCY RGB(0,0,0)
LOADSPRITE "flare2.png",MonSprite

// Main Loop

WHILE quit = 0
IF temps = 0
parax = RND(200) - 100
    paray = RND(200) - 100
    paraz = 200

    coulr = RND(255)
    coulg = RND(255)
    coulb = RND(255)

    IF switche = 0
    temps = 60
    ENDIF

    FOR i = 1 TO nbfeu
    NewFeu()
    NEXT
    ENDIF

AfficheFeu()

temps = temps - 1

IF KEY(01)
quit = 1
ENDIF

SHOWSCREEN
WEND


END



FUNCTION NewFeu:
GLOBAL Liste AS FeuArtifice

Angle1 = RND(360)
  Angle2 = RND(360)
  Vitesse = 0.1

Liste.x = parax
Liste.y = paray
Liste.z = paraz
Liste.r = coulr
Liste.g = coulg
Liste.b = coulb

Liste.xd = COS(Angle1) * COS(Angle2) * Vitesse
Liste.yd = COS(Angle1) * SIN(Angle2) * Vitesse
Liste.zd = SIN(Angle1) * Vitesse

Liste.alpha = 3
Liste.factaille = -1

DIMPUSH MaListe[],Liste

ENDFUNCTION


FUNCTION AfficheFeu:
FOREACH Liste IN MaListe[]
IF Liste.alpha > 0
Liste.x = Liste.x + Liste.xd * 10.0
    Liste.y = Liste.y + Liste.yd * 10.0
    Liste.z = Liste.z + Liste.zd * 10.0
    Liste.y = Liste.y + Liste.vy
    Liste.vy = Liste.vy + 0.02

    posx = (800/2) + ((Liste.x / Liste.z) * 350)
    posy = (600/2) + ((Liste.y / Liste.z) * 350)

Liste.alpha = Liste.alpha - 0.02

    ALPHAMODE Liste.alpha

IF Liste.factaille < 0
    Liste.taillex = ((139*10)/Liste.z)
    Liste.tailley = ((143*10)/Liste.z)
    Liste.factaille = Liste.factaille + 1
    ELSEIF Liste.taillex > 0 AND Liste.tailley  > 0
    Liste.taillex = Liste.taillex - Liste.factaille
    Liste.tailley = Liste.tailley - Liste.factaille
    Liste.factaille = Liste.factaille + 0.001
    ENDIF
    STARTPOLY MonSprite
    POLYVECTOR posx,posy,0,0,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx,posy+Liste.tailley,0,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy+Liste.tailley,139,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy,139,0,RGB(Liste.r,Liste.g,Liste.b)
    ENDPOLY

ELSE
DELETE Liste

ENDIF
NEXT

ENDFUNCTION



Ian Price

That's lovely that is :)

Cheers.
I came. I saw. I played.

Quentin

nice, looks great

Moru

Nice work, clear code! Sadly my computer seems a bit too slow to run it. Every time it creates a new firework after the second one, it pauses for a brief moment. I guess this is due to the many DIMPUSH that is needed at the same time during that frame so the animation misses a few frames.

A few french? words in the variables but easy to guess what they do anyway :-)

Crystal Noir

thx :)

The firework create 300 sprites each time with alpha blend. It may be a little slower, you can try to set 200 to NbFeu.

You can also put up the temps value to 100 instead of 60.

On my PC I don't have pause times but it depends of a lot of factors :)

CrystalNoir

Hi,

I've updated the code here to make it work with earlier version of GLBasic.

To simplify, I've made a zip file that contains the sources and medias.

Just extract the zip file and open the project, and compile it to launch.

Happy GLB !

[attachment deleted by admin]

Kitty Hello

very good. I updated the code so it renders the whole firework in one batch (a lot faster on mobile phones).
Code (glbasic) Select

FUNCTION AfficheFeu:
STARTPOLY MonSprite, 2
FOREACH Liste IN MaListe[]
IF Liste.alpha > 0
Liste.x = Liste.x + Liste.xd * 10.0
    Liste.y = Liste.y + Liste.yd * 10.0
    Liste.z = Liste.z + Liste.zd * 10.0
    Liste.y = Liste.y + Liste.vy
    Liste.vy = Liste.vy + 0.02

    posx = (800/2) + ((Liste.x / Liste.z) * 350)
    posy = (600/2) + ((Liste.y / Liste.z) * 350)

Liste.alpha = Liste.alpha - 0.02

    ALPHAMODE Liste.alpha

IF Liste.factaille < 0
    Liste.taillex = ((139*10)/Liste.z)
    Liste.tailley = ((143*10)/Liste.z)
    Liste.factaille = Liste.factaille + 1
    ELSEIF Liste.taillex > 0 AND Liste.tailley  > 0
    Liste.taillex = Liste.taillex - Liste.factaille
    Liste.tailley = Liste.tailley - Liste.factaille
    Liste.factaille = Liste.factaille + 0.001
    ENDIF
   
    POLYVECTOR posx,posy,0,0,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx,posy+Liste.tailley,0,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy,139,0,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy+Liste.tailley,139,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYNEWSTRIP
ELSE
DELETE Liste

ENDIF
NEXT
ENDPOLY

ENDFUNCTION


CrystalNoir