QB64.org Forum

Active Forums => Programs => Topic started by: FellippeHeitor on June 05, 2018, 05:08:11 pm

Title: Particles - just for fun
Post by: FellippeHeitor 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.  
Title: Re: Particles - just for fun
Post by: bplus on June 05, 2018, 05:41:44 pm
That's cool how they form orbits around mouse position. :)
Title: Re: Particles - just for fun
Post by: johnno56 on June 05, 2018, 10: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
Title: Re: Particles - just for fun
Post by: FellippeHeitor 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.
Title: Re: Particles - just for fun
Post by: Ashish on June 06, 2018, 10:28:44 am
Nice effect!
Title: Re: Particles - just for fun
Post by: FellippeHeitor on June 06, 2018, 04:12:06 pm
Flies all over.
Title: Re: Particles - just for fun
Post by: bplus on June 06, 2018, 07:59:06 pm
Inspiring! :-))
Title: Re: Particles - just for fun
Post by: KingRat 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.