Author Topic: Particles - just for fun  (Read 222 times)

Online FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Particles - just for fun
« on: June 05, 2018, 06:08:11 PM »
Click to create an electromagnetic repeller field.

Code: [Select]
SCREEN _NEWIMAGE(600, 600, 32)
_DELAY .1
_TITLE "Particles"

TYPE PARTICLE
    x AS SINGLE
    xs AS SINGLE
    y AS SINGLE
    ys AS SINGLE
    w AS SINGLE
    h AS SINGLE
    a AS SINGLE
    r AS SINGLE
    c AS _UNSIGNED LONG
END TYPE

DIM particle(100) AS PARTICLE
DIM SHARED r AS SINGLE

RANDOMIZE TIMER
createParticles particle()

DO
    LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 50), BF
    updateAndShowParticles particle()
    IF _MOUSEBUTTON(1) THEN
        IF r < 150 THEN r = r + 1 ELSE r = 0
        CIRCLE (_MOUSEX, _MOUSEY), r, _RGBA32(255, 255, 0, 150 - r)
    END IF
    _DISPLAY
    _LIMIT 30
LOOP

SUB createParticles (this() AS PARTICLE)
    FOR i = 0 TO UBOUND(this)
        this(i).x = RND * _WIDTH
        this(i).xs = 2 + RND * 3
        this(i).y = RND * _HEIGHT
        this(i).ys = 2 + RND * 3
        this(i).w = RND * (_WIDTH / 4)
        this(i).h = RND * (_WIDTH / 6)
        this(i).a = RND * _PI(2)
        this(i).r = RND * 3 + 1
        this(i).c = _RGBA32(RND * 255, RND * 255, RND * 255, RND * 150 + 155)
    NEXT
END SUB

SUB updateAndShowParticles (this() AS PARTICLE)
    WHILE _MOUSEINPUT: WEND
    FOR i = 0 TO UBOUND(this)
        this(i).a = this(i).a + .1
        IF this(i).a > _PI(2) THEN this(i).a = this(i).a - _PI(2)

        IF _MOUSEBUTTON(1) THEN
            IF this(i).y > _MOUSEY THEN this(i).y = this(i).y + r / 15
            IF this(i).y < _MOUSEY THEN this(i).y = this(i).y - r / 15
            IF this(i).x > _MOUSEX THEN this(i).x = this(i).x + r / 15
            IF this(i).x < _MOUSEX THEN this(i).x = this(i).x - r / 15
        ELSE
            r = 0
            IF this(i).y > _MOUSEY THEN this(i).y = this(i).y - this(i).ys
            IF this(i).y < _MOUSEY THEN this(i).y = this(i).y + this(i).ys
            IF this(i).x > _MOUSEX THEN this(i).x = this(i).x - this(i).xs
            IF this(i).x < _MOUSEX THEN this(i).x = this(i).x + this(i).xs
        END IF

        CircleFill this(i).x + COS(this(i).a) * this(i).w, this(i).y + SIN(this(i).a) * this(i).h, this(i).r, this(i).c
    NEXT
END SUB

SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG)
    x0 = R
    y0 = 0
    e = 0
    DO WHILE y0 < x0
        IF e <= 0 THEN
            y0 = y0 + 1
            LINE (x - x0, y + y0)-(x + x0, y + y0), C, BF
            LINE (x - x0, y - y0)-(x + x0, y - y0), C, BF
            e = e + 2 * y0
        ELSE
            LINE (x - y0, y - x0)-(x + y0, y - x0), C, BF
            LINE (x - y0, y + x0)-(x + y0, y + x0), C, BF
            x0 = x0 - 1
            e = e - 2 * x0
        END IF
    LOOP
    LINE (x - R, y)-(x + R, y), C, BF
END SUB
« Last Edit: June 05, 2018, 06:12:50 PM by FellippeHeitor »

Re: Particles - just for fun
« Reply #1 on: June 05, 2018, 06:41:44 PM »
That's cool how they form orbits around mouse position. :)
B = B + ...
QB64 x 64 v1.2 2018 0228/86 git b30af92
QB64 v1.2 2018 0228/86 git b30af92
QB64 v1.1 2017 1106/82

Offline johnno56

  • Live long and prosper.
Re: Particles - just for fun
« Reply #2 on: June 05, 2018, 11:14:01 PM »
Cool... Reminds me of blowflies following a dunny (sewage) truck... Not the programming... The movement of the particles... Stone the flammin' crows! Dodged a bullet there...

J
Logic is the beginning of wisdom.

Online FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Particles - just for fun
« Reply #3 on: June 06, 2018, 12:31:33 AM »
Well, I gotta admit: my first naming choice for my variables was DIM fly(100) AS FLY, but then I decided to go with the more generic 'particle()', but I'm guilty as charged. They have been flies all along. 🙃

Glad you guys enjoy it.

Offline Ashish

  • The joy of coding is endless.
Re: Particles - just for fun
« Reply #4 on: June 06, 2018, 11:28:44 AM »
Nice effect! 🙂
if (Me.success) {Me.improve()} else {Me.tryAgain()}


aKFrameWork - http://bit.ly/aKFrameWork
Menu System - http://bit.ly/guiMenuBar
p5.js in QB64 - http://bit.ly/p5jsbas

@KingOfCoders

Online FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Particles - just for fun
« Reply #5 on: June 06, 2018, 05:12:06 PM »
Flies all over.
« Last Edit: June 06, 2018, 05:13:46 PM by FellippeHeitor »

Re: Particles - just for fun
« Reply #6 on: June 06, 2018, 08:59:06 PM »
Inspiring! :-))
B = B + ...
QB64 x 64 v1.2 2018 0228/86 git b30af92
QB64 v1.2 2018 0228/86 git b30af92
QB64 v1.1 2017 1106/82

Re: Particles - just for fun
« Reply #7 on: June 18, 2018, 06:36:46 AM »
I like these kind of effects... good job!

I saw the guy who programmed "Black Annex" doing something like this on youtube.