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

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Particles - just for fun
« on: June 05, 2018, 05:08:11 pm »
Click to create an electromagnetic repeller field.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2. _TITLE "Particles"
  3.  
  4. TYPE PARTICLE
  5.     x AS SINGLE
  6.     xs AS SINGLE
  7.     y AS SINGLE
  8.     ys AS SINGLE
  9.     w AS SINGLE
  10.     h AS SINGLE
  11.     a AS SINGLE
  12.     r AS SINGLE
  13.     c AS _UNSIGNED LONG
  14.  
  15. DIM particle(100) AS PARTICLE
  16.  
  17. createParticles particle()
  18.  
  19.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 50), BF
  20.     updateAndShowParticles particle()
  21.         IF r < 150 THEN r = r + 1 ELSE r = 0
  22.         CIRCLE (_MOUSEX, _MOUSEY), r, _RGBA32(255, 255, 0, 150 - r)
  23.     END IF
  24.     _DISPLAY
  25.     _LIMIT 30
  26.  
  27. SUB createParticles (this() AS PARTICLE)
  28.     FOR i = 0 TO UBOUND(this)
  29.         this(i).x = RND * _WIDTH
  30.         this(i).xs = 2 + RND * 3
  31.         this(i).y = RND * _HEIGHT
  32.         this(i).ys = 2 + RND * 3
  33.         this(i).w = RND * (_WIDTH / 4)
  34.         this(i).h = RND * (_WIDTH / 6)
  35.         this(i).a = RND * _PI(2)
  36.         this(i).r = RND * 3 + 1
  37.         this(i).c = _RGBA32(RND * 255, RND * 255, RND * 255, RND * 150 + 155)
  38.     NEXT
  39.  
  40. SUB updateAndShowParticles (this() AS PARTICLE)
  41.     FOR i = 0 TO UBOUND(this)
  42.         this(i).a = this(i).a + .1
  43.         IF this(i).a > _PI(2) THEN this(i).a = this(i).a - _PI(2)
  44.  
  45.         IF _MOUSEBUTTON(1) THEN
  46.             IF this(i).y > _MOUSEY THEN this(i).y = this(i).y + r / 15
  47.             IF this(i).y < _MOUSEY THEN this(i).y = this(i).y - r / 15
  48.             IF this(i).x > _MOUSEX THEN this(i).x = this(i).x + r / 15
  49.             IF this(i).x < _MOUSEX THEN this(i).x = this(i).x - r / 15
  50.         ELSE
  51.             r = 0
  52.             IF this(i).y > _MOUSEY THEN this(i).y = this(i).y - this(i).ys
  53.             IF this(i).y < _MOUSEY THEN this(i).y = this(i).y + this(i).ys
  54.             IF this(i).x > _MOUSEX THEN this(i).x = this(i).x - this(i).xs
  55.             IF this(i).x < _MOUSEX THEN this(i).x = this(i).x + this(i).xs
  56.         END IF
  57.  
  58.         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
  59.     NEXT
  60.  
  61. SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG)
  62.     x0 = R
  63.     y0 = 0
  64.     e = 0
  65.     DO WHILE y0 < x0
  66.         IF e <= 0 THEN
  67.             y0 = y0 + 1
  68.             LINE (x - x0, y + y0)-(x + x0, y + y0), C, BF
  69.             LINE (x - x0, y - y0)-(x + x0, y - y0), C, BF
  70.             e = e + 2 * y0
  71.         ELSE
  72.             LINE (x - y0, y - x0)-(x + y0, y - x0), C, BF
  73.             LINE (x - y0, y + x0)-(x + y0, y + x0), C, BF
  74.             x0 = x0 - 1
  75.             e = e - 2 * x0
  76.         END IF
  77.     LOOP
  78.     LINE (x - R, y)-(x + R, y), C, BF
  79.  
particles.PNG
* particles.PNG (Filesize: 326.33 KB, Dimensions: 688x695, Views: 364)
« Last Edit: June 05, 2018, 05:12:50 pm by FellippeHeitor »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Particles - just for fun
« Reply #1 on: June 05, 2018, 05:41:44 pm »
That's cool how they form orbits around mouse position. :)

Marked as best answer by on Yesterday at 08:25:05 pm

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Particles - just for fun
« Reply #2 on: June 05, 2018, 10:14:01 pm »
  • Undo Best Answer
  • 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.

    FellippeHeitor

    • Guest
    Re: Particles - just for fun
    « Reply #3 on: June 05, 2018, 11:31:33 pm »
    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.

    Offline Ashish

    • Forum Resident
    • Posts: 630
    • Never Give Up!
      • View Profile
    Re: Particles - just for fun
    « Reply #4 on: June 06, 2018, 10:28:44 am »
    Nice effect!
    if (Me.success) {Me.improve()} else {Me.tryAgain()}


    My Projects - https://github.com/AshishKingdom?tab=repositories
    OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

    FellippeHeitor

    • Guest
    Re: Particles - just for fun
    « Reply #5 on: June 06, 2018, 04:12:06 pm »
    Flies all over.
    * flies.bas (Filesize: 241.94 KB, Downloads: 382)
    « Last Edit: June 06, 2018, 04:13:46 pm by FellippeHeitor »

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: Particles - just for fun
    « Reply #6 on: June 06, 2018, 07:59:06 pm »
    Inspiring! :-))

    Offline KingRat

    • Newbie
    • Posts: 16
      • View Profile
    Re: Particles - just for fun
    « Reply #7 on: June 18, 2018, 05: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.