Particle Fountain

Started by bplus, August 14, 2023, 17:08:17

Previous topic - Next topic

bplus

Hey this works pretty well with 1/2 width screen, eg .5 * xmax
Rem SmallBASIC
Rem created: 14/08/2023
'_Title "Particle Fountain" 'b+ 2020-08-27
Const nP = 50000
Dim p(1 To nP)

Sub new (i)
    p(i).x = xmax / 4  + Rnd * 20 - 10
    p(i).y = ymax + Rnd * 5
    p(i).dx = Rnd * 1 - .5
    p(i).dy = -10
    p(i).r = Rnd * 3
    p(i).c = RGB(50 * Rnd + 165, 50 * Rnd + 165, 255)
End

For i = 1 To nP
    new i
Next
Color rgb(0,0,0), rgb(0, 36, 0)
repeat
    Cls
    If lp < nP Then lp = lp + 100
    For i = 1 To lp
        p(i).dy = p(i).dy + .1
        p(i).x = p(i).x + p(i).dx
        p(i).y = p(i).y + p(i).dy
        If p(i).x < 0 Or p(i).x > xmax/2 Then new i
        If p(i).y > ymax And p(i).dy > 0 Then
            p(i).dy = -.75 * p(i).dy: p(i).y = ymax - 5
        End If
        Circle p(i).x, p(i).y, p(i).r Color p(i).c
    Next
    showpage
 Until 1 = 0

Port this to your favorite Basic, it's only a few lines of code, you will luv it!
1 person likes this

bplus

#1
Oh dang I posted this here already, I couldn't find it in my sb files so I redid it slightly different today, it probably runs faster now.

But NOT in SmallBASICi.exe use the one outside the FLTK version.
1 person likes this

RemiD

#2
i am trying to convert this code example...

can you clarify what are these values :

Quotep(i).x = xmax / 4  + Rnd * 20 - 10
'xmax' ( max x of graphics window ?)

Quotep(i).y = ymax + Rnd * 5
'ymax'  ( max y of graphics window ?)

Quotep(i).dx = Rnd * 1 - .5
'Rnd' ( random value between ? and ? )

QuoteFor i = 1 To nP
    new i
Next

QuoteThen new i
'New i' ( ? )


thanks

SToS

Quote from: RemiD on August 15, 2023, 09:11:11i am trying to convert this code example...

can you clarify what are these values :

Quotep(i).x = xmax / 4  + Rnd * 20 - 10
'xmax' ( max x of graphics window ?)

Quotep(i).y = ymax + Rnd * 5
'ymax'  ( max y of graphics window ?)

Quotep(i).dx = Rnd * 1 - .5
'Rnd' ( random value between ? and ? )

QuoteFor i = 1 To nP
    new i
Next

QuoteThen new i
'New i' ( ? )


thanks

For self enlightenment, a good place to start is here...
https://smallbasic.github.io/pages/reference.html


bplus

#4
Yes xmax and ymax are the size of screen you have open for sb, I work with maximized for something constant or I never mess with resizing screen while using sb after I have it at nicely re-sized.

rnd returns number between 0 and not quite 1.0000

new i  ' simple calls up the subroutine "new" with i as it's argument. here "new" creates a new particle for index number i

A note about sb's scope: all is global unless you declare a "local" in a sub or function, xmax and ymax are "constants" read by sb when run it, there are a couple more of these constants (they should have a different color than normal text). All variables are of variable Type meaning they are numbers, maps, pathnames, (or else) variable length strings according to usage or what they contain in strings like a slash for path.
1 person likes this

bplus

Maybe Chris will put this one in his On-line samples, it's pretty clever for so little code.
1 person likes this

bplus

#6
If circles run too slow try pixels (pset for most Basics).
1 person likes this

bplus

This releases new particles more gradually for slightly improved more consistent performance, just tweaked one number:
Rem SmallBASIC
Rem created: 14/08/2023
'_Title "Particle Fountain" 'b+ 2020-08-27
Const nP = 50000
Dim p(1 To nP)

Sub new (i)
    p(i).x = xmax / 4  + Rnd * 20 - 10
    p(i).y = ymax + Rnd * 5
    p(i).dx = Rnd * 1 - .5
    p(i).dy = -10
    p(i).r = Rnd * 3
    p(i).c = RGB(50 * Rnd + 165, 50 * Rnd + 165, 255)
End

For i = 1 To nP
    new i
Next
Color rgb(0,0,0), rgb(0, 36, 0)
repeat
    Cls
    If lp < nP Then lp = lp + 10
    For i = 1 To lp
        p(i).dy = p(i).dy + .1
        p(i).x = p(i).x + p(i).dx
        p(i).y = p(i).y + p(i).dy
        If p(i).x < 0 Or p(i).x > xmax/2 Then new i
        If p(i).y > ymax And p(i).dy > 0 Then
            p(i).dy = -.75 * p(i).dy: p(i).y = ymax - 5
        End If
        Circle p(i).x, p(i).y, p(i).r Color p(i).c
    Next
    showpage
 Until 1 = 0

Funny how you wake up in middle of night with these things :)
1 person likes this