Author Topic: Spiral Colored Balls  (Read 4002 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Spiral Colored Balls
« on: October 29, 2019, 05:37:57 pm »
Happy Diwali, time to give your CPU a workout ;-))

Code: QB64: [Select]
  1. _TITLE "Spiral Colored Balls" 'B+ 2019-10-29 mod from
  2. ' 2019-10-29 mod be able to draw these balls anywhere
  3. ' Rotate colors on a sphere.txt for JB v2.0 bplus 2018-04-16
  4. ' from (extremely simplified) Double spiral.bas  SmallBASIC 0.12.8 [B+=MGA] 2017-03-28
  5.  
  6. CONST xmax = 1200, ymax = 700, pi = _PI, nBalls = 5, ww = 3.14159 / 2, gravity = 2
  7. TYPE ball
  8.     x AS SINGLE
  9.     y AS SINGLE
  10.     a AS SINGLE
  11.     dx AS SINGLE
  12.     dy AS SINGLE
  13.     sc AS SINGLE
  14.     pr AS SINGLE
  15.     pg AS SINGLE
  16.     pb AS SINGLE
  17.     pn AS SINGLE
  18.  
  19. DIM SHARED b(1 TO nBalls) AS ball
  20.  
  21. SCREEN _NEWIMAGE(xmax, ymax, 32)
  22. _SCREENMOVE 100, 20 'this fits my screen yours may be different
  23.  
  24. DIM i AS INTEGER, j AS INTEGER, b AS INTEGER, power, sky AS LONG
  25.  
  26. FOR i = 1 TO nBalls
  27.     newBall i
  28. sky = _NEWIMAGE(xmax, ymax, 32)
  29. _DEST sky&
  30. FOR i = 0 TO ymax
  31.     LINE (0, i)-(xmax, i), _RGB32(100 + i / ymax * 40, 100 + i / ymax * 50, 160 + i / ymax * 95)
  32. WHILE _KEYDOWN(27) = 0
  33.     _PUTIMAGE , sky&, 0
  34.     FOR i = 1 TO nBalls
  35.         'ready for collision
  36.         b(i).a = _ATAN2(b(i).dy, b(i).dx)
  37.         power = (b(i).dx ^ 2 + b(i).dy ^ 2) ^ .5
  38.         FOR j = i + 1 TO nBalls
  39.             IF SQR((b(i).x - b(j).x) ^ 2 + (b(i).y - b(j).y) ^ 2) < 200 * (b(i).sc + b(j).sc) THEN
  40.                 b(i).a = _ATAN2(b(i).y - b(j).y, b(i).x - b(j).x)
  41.                 b(j).a = _ATAN2(b(j).y - b(i).y, b(j).x - b(i).x)
  42.                 EXIT FOR
  43.             END IF
  44.         NEXT
  45.         b(i).dx = power * COS(b(i).a)
  46.         b(i).dy = power * SIN(b(i).a)
  47.         b(i).dy = b(i).dy + gravity
  48.         b(i).x = b(i).x + b(i).dx
  49.         b(i).y = b(i).y + b(i).dy '+ 2 * gravity
  50.         IF b(i).x < -200 * b(i).sc OR b(i).x > xmax + 200 * b(i).sc THEN
  51.             newBall i
  52.         END IF
  53.         IF b(i).y + 220 * b(i).sc > ymax THEN
  54.             b(i).y = ymax - 220 * b(i).sc
  55.             b(i).dy = b(i).dy * -.8
  56.             IF b(i).dx = 0 THEN b(i).dx = rdir ELSE b(i).dx = b(i).dx * 1.03
  57.         END IF
  58.         drawBall i
  59.     NEXT
  60.     _DISPLAY
  61.  
  62. SUB drawBall (i)
  63.     DIM w, r, e, tmp, p, x, y, lc AS LONG
  64.     w = ww 'fix
  65.     FOR r = 190 TO 0 STEP -.25
  66.         e = w - pi / 4 / (490 - 300)
  67.         tmp = e: e = w: w = tmp
  68.         FOR p = 0 TO pi STEP pi / 144
  69.             e = INT((COS(w) * 380) / 2)
  70.             x = b(i).x + e * b(i).sc * COS(p * 2)
  71.             y = b(i).y - 90 * b(i).sc + e * b(i).sc * SIN(p * 2) + r * b(i).sc
  72.             lc = lc + 1
  73.             IF lc MOD 25 = 0 THEN
  74.                 fcirc x, y, 6 * b(i).sc, plasma~&(i)
  75.             END IF
  76.         NEXT
  77.     NEXT
  78.  
  79.  
  80. SUB newBall (i)
  81.     b(i).x = RND * xmax - 40 + 20
  82.     b(i).y = RND * -200
  83.     b(i).dx = rand(1, 3) * rdir
  84.     b(i).dy = rand(-5, 5)
  85.     b(i).sc = rand(2, 5) / 10
  86.     setRGB i
  87.  
  88. SUB setRGB (i)
  89.     b(i).pr = RND ^ 3: b(i).pg = RND ^ 3: b(i).pb = RND ^ 3: b(i).pn = 1
  90.  
  91. FUNCTION plasma~& (i)
  92.     b(i).pn = b(i).pn + .05
  93.     plasma~& = _RGBA32(127 + 127 * SIN(b(i).pr * b(i).pn), 127 + 127 * SIN(b(i).pg * b(i).pn), 127 + 127 * SIN(b(i).pb * b(i).pn), 255)
  94.  
  95. FUNCTION rand (lo, hi)
  96.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  97.  
  98. FUNCTION rdir ()
  99.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  100.  
  101. 'from Steve Gold standard
  102. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  103.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  104.     DIM X AS INTEGER, Y AS INTEGER
  105.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  106.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  107.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  108.     WHILE X > Y
  109.         RadiusError = RadiusError + Y * 2 + 1
  110.         IF RadiusError >= 0 THEN
  111.             IF X <> Y + 1 THEN
  112.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  113.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  114.             END IF
  115.             X = X - 1
  116.             RadiusError = RadiusError - X * 2
  117.         END IF
  118.         Y = Y + 1
  119.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  120.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  121.     WEND
  122.  
  123.  

Update: I changed the alpha to full opacity but you all might like to try this alpha level too:
Code: QB64: [Select]
  1. FUNCTION plasma~& (i)
  2. FUNCTION plasma~& (i)
  3.     b(i).pn = b(i).pn + .05
  4.     plasma~& = _RGBA32(127 + 127 * SIN(b(i).pr * b(i).pn), 127 + 127 * SIN(b(i).pg * b(i).pn), 127 + 127 * SIN(b(i).pb * b(i).pn), 40)
  5.     'plasma~& = _RGBA32(127 + 127 * SIN(b(i).pr * b(i).pn), 127 + 127 * SIN(b(i).pg * b(i).pn), 127 + 127 * SIN(b(i).pb * b(i).pn), 255)
  6.  
  7.  
« Last Edit: October 29, 2019, 11:56:44 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Spiral Colored Balls
« Reply #1 on: October 29, 2019, 09:01:36 pm »
LOL way cool! Awesome designs. That kind of graphics would be amazing if someone could make a Pinball game with QB64. I've never seen one I think. But the geometry would be a bit hard for me.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Spiral Colored Balls
« Reply #2 on: October 29, 2019, 11:41:26 pm »
Thanks Ken

You know I debated myself doing it this way or the way above (which should probably NOT use alpha either):
Code: QB64: [Select]
  1. _TITLE "Spiral Colored Balls 2" 'B+ 2019-10-29 mod from
  2. ' 2019-10-29 post 2nd way to display balls
  3. ' 2019-10-29 mod be able to draw these balls anywhere
  4. ' Rotate colors on a sphere.txt for JB v2.0 bplus 2018-04-16
  5. ' from (extremely simplified) Double spiral.bas  SmallBASIC 0.12.8 [B+=MGA] 2017-03-28
  6.  
  7. CONST xmax = 1200, ymax = 700, pi = _PI, nBalls = 5, ww = 3.14159 / 2, gravity = 2
  8. TYPE ball
  9.     x AS SINGLE
  10.     y AS SINGLE
  11.     a AS SINGLE
  12.     dx AS SINGLE
  13.     dy AS SINGLE
  14.     sc AS SINGLE
  15.     pr AS SINGLE
  16.     pg AS SINGLE
  17.     pb AS SINGLE
  18.     pn AS SINGLE
  19.  
  20. DIM SHARED b(1 TO nBalls) AS ball
  21.  
  22. SCREEN _NEWIMAGE(xmax, ymax, 32)
  23. _SCREENMOVE 100, 20 'this fits my screen yours may be different
  24.  
  25. DIM i AS INTEGER, j AS INTEGER, b AS INTEGER, power, sky AS LONG
  26.  
  27. FOR i = 1 TO nBalls
  28.     newBall i
  29. sky = _NEWIMAGE(xmax, ymax, 32)
  30. _DEST sky&
  31. FOR i = 0 TO ymax
  32.     LINE (0, i)-(xmax, i), _RGB32(100 + i / ymax * 40, 100 + i / ymax * 50, 160 + i / ymax * 95)
  33. WHILE _KEYDOWN(27) = 0
  34.     _PUTIMAGE , sky&, 0
  35.     FOR i = 1 TO nBalls
  36.         'ready for collision
  37.         b(i).a = _ATAN2(b(i).dy, b(i).dx)
  38.         power = (b(i).dx ^ 2 + b(i).dy ^ 2) ^ .5
  39.         FOR j = i + 1 TO nBalls
  40.             IF SQR((b(i).x - b(j).x) ^ 2 + (b(i).y - b(j).y) ^ 2) < 200 * (b(i).sc + b(j).sc) THEN
  41.                 b(i).a = _ATAN2(b(i).y - b(j).y, b(i).x - b(j).x)
  42.                 b(j).a = _ATAN2(b(j).y - b(i).y, b(j).x - b(i).x)
  43.                 EXIT FOR
  44.             END IF
  45.         NEXT
  46.         b(i).dx = power * COS(b(i).a)
  47.         b(i).dy = power * SIN(b(i).a)
  48.         b(i).dy = b(i).dy + gravity
  49.         b(i).x = b(i).x + b(i).dx
  50.         b(i).y = b(i).y + b(i).dy '+ 2 * gravity
  51.         IF b(i).x < -200 * b(i).sc OR b(i).x > xmax + 200 * b(i).sc THEN
  52.             newBall i
  53.         END IF
  54.         IF b(i).y + 220 * b(i).sc > ymax THEN
  55.             b(i).y = ymax - 220 * b(i).sc
  56.             b(i).dy = b(i).dy * -.8
  57.             IF b(i).dx = 0 THEN b(i).dx = rdir ELSE b(i).dx = b(i).dx * 1.03
  58.         END IF
  59.         drawBall i
  60.     NEXT
  61.     _DISPLAY
  62.  
  63. SUB drawBall (i)
  64.     DIM w, r, e, tmp, p, x, y, lc AS LONG
  65.     w = ww 'fix
  66.     FOR r = 190 TO 0 STEP -.25
  67.         e = w - pi / 4 / (490 - 300)
  68.         tmp = e: e = w: w = tmp
  69.         FOR p = 0 TO pi STEP pi / 144
  70.             e = INT((COS(w) * 380) / 2)
  71.             x = b(i).x + e * b(i).sc * COS(p * 2)
  72.             y = b(i).y - 90 * b(i).sc + e * b(i).sc * SIN(p * 2) + r * b(i).sc
  73.             lc = lc + 1
  74.             IF lc MOD 40 = 0 THEN
  75.                 fcirc x, y, 6 * b(i).sc, plasma~&(i)
  76.             END IF
  77.         NEXT
  78.     NEXT
  79.  
  80.  
  81. SUB newBall (i)
  82.     b(i).x = RND * xmax - 40 + 20
  83.     b(i).y = RND * -200
  84.     b(i).dx = rand(1, 3) * rdir
  85.     b(i).dy = rand(-5, 5)
  86.     b(i).sc = rand(2, 5) / 10
  87.     setRGB i
  88.  
  89. SUB setRGB (i)
  90.     b(i).pr = RND ^ 3: b(i).pg = RND ^ 3: b(i).pb = RND ^ 3: b(i).pn = 1
  91.  
  92. FUNCTION plasma~& (i)
  93.     b(i).pn = b(i).pn + .05
  94.     plasma~& = _RGBA32(127 + 127 * SIN(b(i).pr * b(i).pn), 127 + 127 * SIN(b(i).pg * b(i).pn), 127 + 127 * SIN(b(i).pb * b(i).pn), 255)
  95.  
  96. FUNCTION rand (lo, hi)
  97.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  98.  
  99. FUNCTION rdir ()
  100.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  101.  
  102. 'from Steve Gold standard
  103. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  104.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  105.     DIM X AS INTEGER, Y AS INTEGER
  106.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  107.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  108.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  109.     WHILE X > Y
  110.         RadiusError = RadiusError + Y * 2 + 1
  111.         IF RadiusError >= 0 THEN
  112.             IF X <> Y + 1 THEN
  113.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  114.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  115.             END IF
  116.             X = X - 1
  117.             RadiusError = RadiusError - X * 2
  118.         END IF
  119.         Y = Y + 1
  120.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  121.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  122.     WEND
  123.  
  124.  

So you all could see more how these balls are constructed.

Which do you all like more?
« Last Edit: October 29, 2019, 11:46:09 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Spiral Colored Balls
« Reply #3 on: October 30, 2019, 12:02:04 am »
Might be fun if the balls had some spin as they bounce Plus the little balls shouldn't be able to have so much force to push the big balls around so much.

Oh yeah and the speed, wonder if it could be revved up? So could do 25 or so balls.

Oh! Dewali day was Sunday, already too late. ;(
« Last Edit: October 30, 2019, 12:03:22 am by bplus »

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: Spiral Colored Balls
« Reply #4 on: October 30, 2019, 03:59:08 am »
That was cool! Nice job with that B+!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Spiral Colored Balls
« Reply #5 on: October 30, 2019, 08:22:13 am »
Aww man... Here I am thinking that all my optical receptors had been destroyed from some of your 'colourful' examples from the past, then these champions pop up on the screen, and found the last surviving sensors and blew 'them' out of the water as well.... Brilliant examples none the less.... and my seeing-eye dog agrees... lol
Logic is the beginning of wisdom.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Spiral Colored Balls
« Reply #6 on: October 30, 2019, 10:42:06 am »
Wow! Bplus! Its beautiful! Love it!
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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