QB64.org Forum

Active Forums => Programs => Topic started by: bplus on March 31, 2018, 12:54:05 am

Title: Dropping Balls
Post by: bplus on March 31, 2018, 12:54:05 am
Misunderstanding what Make71 wanted, I reworked my ball handling for bounces and collisions. Not bad for zero vectors or dot products... are you a fake physics fan?
Code: QB64: [Select]
  1. _TITLE "Dropping Balls by bplus 2018-03-31"
  2. CONST xmax = 800
  3. CONST ymax = 600
  4.  
  5. SCREEN _NEWIMAGE(xmax, ymax, 32)
  6.  
  7. gravity = 1
  8. balls = 8
  9. DIM x(balls), y(balls), r(balls), c(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  10. FOR i = 1 TO balls
  11.     r(i) = rand(15, 20)
  12.     x(i) = rand(r(i), xmax - r(i))
  13.     y(i) = rand(r(i), ymax - r(i))
  14.     c(i) = rand(1, 15)
  15.     dx(i) = rand(1, 3) * rdir
  16.     dy(i) = rand(10, 20)
  17.     rr(i) = rand(200, 255)
  18.     gg(i) = rand(200, 255)
  19.     bb(i) = rand(200, 255)
  20.     CLS
  21.     FOR i = 1 TO balls
  22.         'ready for collision
  23.         a(i) = _ATAN2(dy(i), dx(i))
  24.         power = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  25.         FOR j = i + 1 TO balls
  26.             IF SQR((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2) < r(i) + r(j) THEN
  27.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  28.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  29.                 EXIT FOR
  30.             END IF
  31.         NEXT
  32.         dx(i) = power * COS(a(i))
  33.         dy(i) = power * SIN(a(i))
  34.         dy(i) = dy(i) + gravity
  35.         x(i) = x(i) + dx(i)
  36.         y(i) = y(i) + dy(i) '+ 2 * gravity
  37.         IF x(i) < -r(i) OR x(i) > xmax + r(i) THEN
  38.             x(i) = xmax / 2
  39.             y(i) = 0
  40.             dx(i) = rand(1, 3) * rdir
  41.             dy(i) = 0
  42.         END IF
  43.         IF y(i) + r(i) > ymax THEN y(i) = ymax - r(i): dy(i) = dy(i) * -.8
  44.         FOR rad = r(i) TO 1 STEP -1
  45.             COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  46.             fcirc x(i), y(i), rad
  47.         NEXT
  48.     NEXT
  49.     _DISPLAY
  50.     _LIMIT 20
  51.  
  52. FUNCTION rand (lo, hi)
  53.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  54.  
  55. FUNCTION rdir ()
  56.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  57.  
  58. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  59. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  60.     DIM subRadius AS LONG, RadiusError AS LONG
  61.     DIM X AS LONG, Y AS LONG
  62.  
  63.     subRadius = ABS(R)
  64.     RadiusError = -subRadius
  65.     X = subRadius
  66.     Y = 0
  67.  
  68.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  69.  
  70.     ' Draw the middle span here so we don't draw it twice in the main loop,
  71.     ' which would be a problem with blending turned on.
  72.     LINE (CX - X, CY)-(CX + X, CY), , BF
  73.  
  74.     WHILE X > Y
  75.         RadiusError = RadiusError + Y * 2 + 1
  76.         IF RadiusError >= 0 THEN
  77.             IF X <> Y + 1 THEN
  78.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  79.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  80.             END IF
  81.             X = X - 1
  82.             RadiusError = RadiusError - X * 2
  83.         END IF
  84.         Y = Y + 1
  85.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  86.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  87.     WEND
  88.  
  89.  

Title: Re: Dropping Balls
Post by: bplus on March 31, 2018, 09:54:40 am
OK most of flaws mostly fixed.
Code: QB64: [Select]
  1. _TITLE "Dropping Balls 2 by bplus 2018-03-31"
  2. ' attempt to fix
  3. CONST xmax = 800
  4. CONST ymax = 600
  5.  
  6. SCREEN _NEWIMAGE(xmax, ymax, 32)
  7. _SCREENMOVE 360, 60
  8.  
  9. gravity = 1
  10. balls = 8
  11. DIM x(balls), y(balls), r(balls), c(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  12. FOR i = 1 TO balls
  13.     r(i) = rand(15, 20)
  14.     x(i) = rand(r(i), xmax - r(i))
  15.     y(i) = rand(r(i), ymax - r(i))
  16.     c(i) = rand(1, 15)
  17.     dx(i) = rand(0, 3) * rdir
  18.     dy(i) = rand(10, 20)
  19.     rr(i) = rand(200, 255)
  20.     gg(i) = rand(200, 255)
  21.     bb(i) = rand(200, 255)
  22.     CLS
  23.     FOR i = 1 TO balls
  24.         'ready for collision
  25.         dy(i) = dy(i) + gravity
  26.         a(i) = _ATAN2(dy(i), dx(i))
  27.         power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  28.         imoved = 0
  29.         FOR j = i + 1 TO balls
  30.             IF SQR((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2) < r(i) + r(j) THEN
  31.                 imoved = 1
  32.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  33.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  34.                 'update new dx, dy for i and j balls
  35.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  36.                 power = .7 * (power1 + power2) / 2
  37.                 dx(i) = power * COS(a(i))
  38.                 dy(i) = power * SIN(a(i))
  39.                 dx(j) = power * COS(a(j))
  40.                 dy(j) = power * SIN(a(j))
  41.                 x(i) = x(i) + dx(i)
  42.                 y(i) = y(i) + dy(i)
  43.                 x(j) = x(j) + dx(j)
  44.                 y(j) = y(j) + dy(j)
  45.                 EXIT FOR
  46.             END IF
  47.         NEXT
  48.         IF imoved = 0 THEN
  49.             x(i) = x(i) + dx(i)
  50.             y(i) = y(i) + dy(i)
  51.         END IF
  52.         IF x(i) < -r(i) OR x(i) > xmax + r(i) THEN
  53.             x(i) = xmax / 2 + rand(0, 100) * rdir
  54.             y(i) = 0
  55.             dx(i) = rand(0, 3) * rdir
  56.             dy(i) = 1
  57.         END IF
  58.         IF y(i) + r(i) > ymax THEN y(i) = ymax - r(i): dy(i) = dy(i) * -.7: x(i) = x(i) + .1 * dx(i)
  59.         FOR rad = r(i) TO 1 STEP -1
  60.             COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  61.             fcirc x(i), y(i), rad
  62.         NEXT
  63.     NEXT
  64.     _DISPLAY
  65.     _LIMIT 20
  66.  
  67. FUNCTION rand (lo, hi)
  68.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  69.  
  70. FUNCTION rdir ()
  71.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  72.  
  73. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  74. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  75.     DIM subRadius AS LONG, RadiusError AS LONG
  76.     DIM X AS LONG, Y AS LONG
  77.  
  78.     subRadius = ABS(R)
  79.     RadiusError = -subRadius
  80.     X = subRadius
  81.     Y = 0
  82.  
  83.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  84.  
  85.     ' Draw the middle span here so we don't draw it twice in the main loop,
  86.     ' which would be a problem with blending turned on.
  87.     LINE (CX - X, CY)-(CX + X, CY), , BF
  88.  
  89.     WHILE X > Y
  90.         RadiusError = RadiusError + Y * 2 + 1
  91.         IF RadiusError >= 0 THEN
  92.             IF X <> Y + 1 THEN
  93.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  94.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  95.             END IF
  96.             X = X - 1
  97.             RadiusError = RadiusError - X * 2
  98.         END IF
  99.         Y = Y + 1
  100.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  101.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  102.     WEND
  103.  
  104.  
Title: Re: Dropping Balls
Post by: bplus on March 31, 2018, 12:57:00 pm
Sound effects added, but a little off when lots of sound all at once, still it's fun to listen to:
Code: QB64: [Select]
  1. _TITLE "Dropping Balls 2 w sound by bplus 2018-03-31"
  2. ' attempt to fix
  3. CONST xmax = 800
  4. CONST ymax = 600
  5.  
  6. SCREEN _NEWIMAGE(xmax, ymax, 32)
  7. _SCREENMOVE 360, 60
  8.  
  9. gravity = 1
  10. balls = 8
  11. DIM x(balls), y(balls), r(balls), c(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  12. FOR i = 1 TO balls
  13.     r(i) = rand(15, 20)
  14.     x(i) = rand(r(i), xmax - r(i))
  15.     y(i) = rand(r(i), ymax - r(i))
  16.     c(i) = rand(1, 15)
  17.     dx(i) = rand(0, 3) * rdir
  18.     dy(i) = rand(10, 20)
  19.     rr(i) = rand(200, 255)
  20.     gg(i) = rand(200, 255)
  21.     bb(i) = rand(200, 255)
  22.     CLS
  23.     FOR i = 1 TO balls
  24.         'ready for collision
  25.         dy(i) = dy(i) + gravity
  26.         a(i) = _ATAN2(dy(i), dx(i))
  27.         power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  28.         imoved = 0
  29.         FOR j = i + 1 TO balls
  30.             IF SQR((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2) < r(i) + r(j) THEN
  31.                 imoved = 1
  32.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  33.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  34.                 'update new dx, dy for i and j balls
  35.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  36.                 power = .7 * (power1 + power2) / 2
  37.                 dx(i) = power * COS(a(i))
  38.                 dy(i) = power * SIN(a(i))
  39.                 dx(j) = power * COS(a(j))
  40.                 dy(j) = power * SIN(a(j))
  41.                 x(i) = x(i) + dx(i)
  42.                 y(i) = y(i) + dy(i)
  43.                 x(j) = x(j) + dx(j)
  44.                 y(j) = y(j) + dy(j)
  45.                 snd 120 + r(i) * 250, r(j) * .15
  46.                 EXIT FOR
  47.             END IF
  48.         NEXT
  49.         IF imoved = 0 THEN
  50.             x(i) = x(i) + dx(i)
  51.             y(i) = y(i) + dy(i)
  52.         END IF
  53.         IF x(i) < -r(i) OR x(i) > xmax + r(i) THEN
  54.             x(i) = xmax / 2 + rand(0, 100) * rdir
  55.             y(i) = 0
  56.             dx(i) = rand(0, 3) * rdir
  57.             dy(i) = 1
  58.         END IF
  59.         IF y(i) + r(i) > ymax + gravity THEN snd (y(i) + r(i) - (ymax + gravity)) * 100 + r(i) * 20, 6 'only when hits floor, not for rolling balls
  60.         IF y(i) + r(i) > ymax THEN y(i) = ymax - r(i): dy(i) = dy(i) * -.7: x(i) = x(i) + .1 * dx(i)
  61.  
  62.         FOR rad = r(i) TO 1 STEP -1
  63.             COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  64.             fcirc x(i), y(i), rad
  65.         NEXT
  66.     NEXT
  67.     _DISPLAY
  68.     _LIMIT 20
  69.  
  70. FUNCTION rand (lo, hi)
  71.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  72.  
  73. FUNCTION rdir ()
  74.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  75.  
  76. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  77. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  78.     DIM subRadius AS LONG, RadiusError AS LONG
  79.     DIM X AS LONG, Y AS LONG
  80.  
  81.     subRadius = ABS(R)
  82.     RadiusError = -subRadius
  83.     X = subRadius
  84.     Y = 0
  85.  
  86.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  87.  
  88.     ' Draw the middle span here so we don't draw it twice in the main loop,
  89.     ' which would be a problem with blending turned on.
  90.     LINE (CX - X, CY)-(CX + X, CY), , BF
  91.  
  92.     WHILE X > Y
  93.         RadiusError = RadiusError + Y * 2 + 1
  94.         IF RadiusError >= 0 THEN
  95.             IF X <> Y + 1 THEN
  96.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  97.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  98.             END IF
  99.             X = X - 1
  100.             RadiusError = RadiusError - X * 2
  101.         END IF
  102.         Y = Y + 1
  103.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  104.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  105.     WEND
  106.  
  107. SUB snd (frq, dur)
  108.     SOUND frq / 2.2, dur * .01
  109.  
Title: Re: Dropping Balls
Post by: FellippeHeitor on April 01, 2018, 07:19:26 pm
Love the sound synthesis with SOUND!
Title: Re: Dropping Balls
Post by: bplus on April 03, 2018, 05:39:05 pm
Dynamically built pyramid pile, thanks to STxAxTIC's help:
Code: QB64: [Select]
  1. _TITLE "Dropping Balls pile attempt bplus 2018-04-03"
  2. 'attempt to build pile by adjusting drop rate, elasticity, gravity
  3. ' remove sound and adjust dropping to center of screen
  4.  
  5.  
  6.  
  7. ' built from Dropping balls 4 w snd and STATIC created 2018-04-3
  8. ' add STATIC's moving ball before figuring bounce from collision
  9. ' which was a mod of Dropping Balls 2 w sound posted 2018-03-31
  10. CONST xmax = 800
  11. CONST ymax = 600
  12.  
  13. SCREEN _NEWIMAGE(xmax, ymax, 32)
  14. _SCREENMOVE 360, 60
  15. elastic = .5
  16. gravity = .5
  17. balls = 160
  18. DIM x(balls), y(balls), r(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  19. FOR i = 1 TO balls
  20.     r(i) = 15
  21.     x(i) = xmax / 2 + (i MOD 2) * 8 - 4
  22.     y(i) = 0
  23.     dx(i) = 0
  24.     dy(i) = 3
  25.     rr(i) = rand(200, 255)
  26.     gg(i) = rand(200, 255)
  27.     bb(i) = rand(200, 255)
  28. maxBall = 0
  29.     CLS
  30.     loopCnt = loopCnt + 1
  31.     IF loopCnt MOD 17 = 0 THEN
  32.         IF maxBall < balls THEN maxBall = maxBall + 1
  33.     END IF
  34.     COLOR _RGB32(255, 255, 255)
  35.     _PRINTSTRING (100, 10), "Balls:" + STR$(maxBall)
  36.  
  37.     FOR i = 1 TO maxBall
  38.         'ready for collision
  39.         dy(i) = dy(i) + gravity
  40.         a(i) = _ATAN2(dy(i), dx(i))
  41.  
  42.         imoved = 0
  43.         FOR j = i + 1 TO maxBall
  44.  
  45.             ' The following is STATIC's adjustment of ball positions if overlapping
  46.             ' before calcultion of new positions from collision
  47.             ' Displacement vector and its magnitude.  Thanks STxAxTIC !
  48.             nx = x(j) - x(i)
  49.             ny = y(j) - y(i)
  50.             nm = SQR(nx ^ 2 + ny ^ 2)
  51.             IF nm < 1 + r(i) + r(j) THEN
  52.                 nx = nx / nm
  53.                 ny = ny / nm
  54.  
  55.                 ' Regardless of momentum exchange, separate the balls along the lone connecting them.
  56.                 DO WHILE nm < 1 + r(i) + r(j)
  57.                     flub = .001 '* RND
  58.  
  59.                     x(j) = x(j) + flub * nx
  60.                     y(j) = y(j) + flub * ny
  61.  
  62.                     x(i) = x(i) - flub * nx
  63.                     y(i) = y(i) - flub * ny
  64.  
  65.                     nx = x(j) - x(i)
  66.                     ny = y(j) - y(i)
  67.                     nm = SQR(nx ^ 2 + ny ^ 2)
  68.                     nx = nx / nm
  69.                     ny = ny / nm
  70.                 LOOP
  71.  
  72.  
  73.                 imoved = 1
  74.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  75.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  76.  
  77.                 'update new dx, dy for i and j balls
  78.                 power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  79.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  80.                 power = elastic * (power1 + power2) / 2
  81.                 dx(i) = power * COS(a(i))
  82.                 dy(i) = power * SIN(a(i))
  83.                 dx(j) = power * COS(a(j))
  84.                 dy(j) = power * SIN(a(j))
  85.                 x(i) = x(i) + dx(i)
  86.                 y(i) = y(i) + dy(i)
  87.                 x(j) = x(j) + dx(j)
  88.                 y(j) = y(j) + dy(j)
  89.                 'EXIT FOR
  90.             END IF
  91.         NEXT
  92.         IF imoved = 0 THEN
  93.             x(i) = x(i) + dx(i)
  94.             y(i) = y(i) + dy(i)
  95.         END IF
  96.         IF x(i) < -r(i) OR x(i) > xmax + r(i) THEN
  97.             x(i) = xmax / 2 + (i MOD 2) * 4 * r(i) - 2 * r(i)
  98.             y(i) = 0
  99.             dx(i) = 0
  100.             dy(i) = 3
  101.         END IF
  102.  
  103.         IF y(i) + r(i) > ymax THEN y(i) = ymax - r(i): dy(i) = -dy(i) * elastic '???: x(i) = x(i) + .1 * dx(i)
  104.  
  105.         FOR rad = r(i) TO 1 STEP -1
  106.             COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  107.             fcirc x(i), y(i), rad
  108.         NEXT
  109.     NEXT
  110.     _DISPLAY
  111.     _LIMIT 20
  112.  
  113. FUNCTION rand (lo, hi)
  114.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  115.  
  116. FUNCTION rdir ()
  117.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  118.  
  119. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  120. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  121.     DIM subRadius AS LONG, RadiusError AS LONG
  122.     DIM X AS LONG, Y AS LONG
  123.  
  124.     subRadius = ABS(R)
  125.     RadiusError = -subRadius
  126.     X = subRadius
  127.     Y = 0
  128.  
  129.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  130.  
  131.     ' Draw the middle span here so we don't draw it twice in the main loop,
  132.     ' which would be a problem with blending turned on.
  133.     LINE (CX - X, CY)-(CX + X, CY), , BF
  134.  
  135.     WHILE X > Y
  136.         RadiusError = RadiusError + Y * 2 + 1
  137.         IF RadiusError >= 0 THEN
  138.             IF X <> Y + 1 THEN
  139.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  140.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  141.             END IF
  142.             X = X - 1
  143.             RadiusError = RadiusError - X * 2
  144.         END IF
  145.         Y = Y + 1
  146.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  147.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  148.     WEND
  149.  
  150. SUB snd (frq, dur)
  151.     SOUND frq / 2.2, dur * .01
  152.  
  153.  
Title: Re: Dropping Balls
Post by: bplus on April 03, 2018, 11:19:54 pm
For perfect pyramids, I offer this:
Code: QB64: [Select]
  1. _TITLE "pyramid of circles 2 by bplus 2018-04-03"
  2. CONST xmax = 800
  3. CONST ymax = 600
  4.  
  5. SCREEN _NEWIMAGE(xmax, ymax, 32)
  6. _SCREENMOVE 360, 60
  7. DIM SHARED px(154), py(154), rr(154), gg(154), bb(154)
  8. FOR i = 1 TO 153
  9.     rr(i) = (RND * 55 + 200)
  10.     gg(i) = RND * 55 + 200 * INT(RND * 2)
  11.     bb(i) = RND * 55 + 200
  12. 'let n = number of circles at base of pile
  13. n = 10
  14.  
  15. 'let r = radius of each circle
  16. r = 20
  17.  
  18. 'let base be total length of pile
  19. baseLength = 2 * r * n
  20.  
  21. ' center pyramid in middle of screen
  22. startx = (xmax - baseLength) / 2
  23.  
  24. 'stacking circles that form equilateral triangles at their origins have a height change of
  25. deltaHeight = r * 3 ^ .5 'r times the sqr(3)
  26.  
  27. FOR row = n TO 1 STEP -1
  28.     IF row = n THEN y = ymax - r - 1 ELSE y = y - deltaHeight
  29.     FOR col = 1 TO row
  30.         x = startx + col * 2 * r - r
  31.         index = index + 1
  32.         target x, y, index
  33.     NEXT
  34.     startx = startx + r
  35.  
  36. SUB target (x, y, i)
  37.     nx = x: ny = y
  38.     ra = _PI(1 / (INT(RND * 7) + 4) + 1 / 2)
  39.     dx = 10 * COS(ra)
  40.     dy = 10 * SIN(ra)
  41.     bounce = 0: rb = INT(RND * 7) + 3
  42.     WHILE bounce < rb
  43.         IF nx + dx > xmax - 20 OR nx + dx < 20 THEN dx = -dx: bounce = bounce + 1
  44.         IF ny + dy > ymax - 20 OR ny + dy < 20 THEN dy = -dy: bounce = bounce + 1
  45.         nx = nx + dx: ny = ny + dy
  46.     WEND
  47.     dx = -dx: dy = -dy
  48.     WHILE bounce > 0
  49.         IF nx + dx > xmax - 20 OR nx + dx < 20 THEN dx = -dx: bounce = bounce - 1
  50.         IF ny + dy > ymax - 20 OR ny + dy < 20 THEN dy = -dy: bounce = bounce - 1
  51.         nx = nx + dx: ny = ny + dy
  52.         CLS
  53.         FOR j = 1 TO i - 1
  54.             FOR rad = 20 TO 1 STEP -1
  55.                 COLOR _RGB32(rr(j) - 10 * rad, gg(j) - 10 * rad, bb(j) - 10 * rad)
  56.                 fcirc px(j), py(j), rad
  57.             NEXT
  58.         NEXT
  59.         FOR rad = 20 TO 1 STEP -1
  60.             COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  61.             fcirc nx, ny, rad
  62.         NEXT
  63.         _DISPLAY
  64.         _LIMIT 200
  65.     WEND
  66.     'last bit
  67.     WHILE SQR((nx - x) ^ 2 + (ny - y) ^ 2) > 20
  68.         nx = nx + dx
  69.         ny = ny + dy
  70.         CLS
  71.         FOR j = 1 TO i - 1
  72.             FOR rad = 20 TO 1 STEP -1
  73.                 COLOR _RGB32(rr(j) - 10 * rad, gg(j) - 10 * rad, bb(j) - 10 * rad)
  74.                 fcirc px(j), py(j), rad
  75.             NEXT
  76.         NEXT
  77.         FOR rad = 20 TO 1 STEP -1
  78.             COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  79.             fcirc nx, ny, rad
  80.         NEXT
  81.         _DISPLAY
  82.         _LIMIT 200
  83.     WEND
  84.  
  85.     CLS
  86.     FOR j = 1 TO i - 1
  87.         FOR rad = 20 TO 1 STEP -1
  88.             COLOR _RGB32(rr(j) - 10 * rad, gg(j) - 10 * rad, bb(j) - 10 * rad)
  89.             fcirc px(j), py(j), rad
  90.         NEXT
  91.     NEXT
  92.     FOR rad = 20 TO 1 STEP -1
  93.         COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  94.         fcirc x, y, rad
  95.     NEXT
  96.     _DISPLAY
  97.     _DELAY .25
  98.     px(i) = x: py(i) = y
  99.  
  100.  
  101. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  102. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  103.     DIM subRadius AS LONG, RadiusError AS LONG
  104.     DIM X AS LONG, Y AS LONG
  105.  
  106.     subRadius = ABS(R)
  107.     RadiusError = -subRadius
  108.     X = subRadius
  109.     Y = 0
  110.  
  111.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  112.  
  113.     ' Draw the middle span here so we don't draw it twice in the main loop,
  114.     ' which would be a problem with blending turned on.
  115.     LINE (CX - X, CY)-(CX + X, CY), , BF
  116.  
  117.     WHILE X > Y
  118.         RadiusError = RadiusError + Y * 2 + 1
  119.         IF RadiusError >= 0 THEN
  120.             IF X <> Y + 1 THEN
  121.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  122.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  123.             END IF
  124.             X = X - 1
  125.             RadiusError = RadiusError - X * 2
  126.         END IF
  127.         Y = Y + 1
  128.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  129.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  130.     WEND
  131.  
  132.  
  133.  
  134.  
Title: Re: Dropping Balls
Post by: bplus on April 04, 2018, 09:36:52 am
Dang! I could of drawn a picture with the colored balls AND have the pyramid magically appear as from all the balls flying around slowly finding their resting place. BBL




Title: Re: Dropping Balls
Post by: _vince on June 10, 2018, 10:35:11 am
How hard would it be to adapt for an hourglass simulator?
Title: Re: Dropping Balls
Post by: bplus on June 10, 2018, 11:41:07 am
How hard would it be to adapt for an hourglass simulator?

Wow, it's been just a couple of months since I have posted this but seems like a year. BTW I did finish the BBL later thing and was not impressed and other things came up, so apologies for that if anyone was waiting....

Sand in 2D is easy from this, if I recall, just adjust the bounce factors. But if you want a really cool 3D sim, Ashish would be likely of great help with the GL stuff.

Glass-like graphics effects might be interesting to explore! Does anyone have some simple models?

Oh Petr did some transparency stuff for buttons but need shiny glass reflecting surround colors if not images fit onto the glass surface and distorted according to glass shape. (Yikes more 3D stuff!)
Title: Re: Dropping Balls
Post by: bplus on June 10, 2018, 04:32:34 pm
How is this for a nice effect?

...

I like this much better:
https://www.qb64.org/forum/index.php?topic=262.0

[banned user], if you modify my code could you write that somewhere on or around title.

I don't understand the need or use of map!

And what have you against the sounds?  ;D
Title: Re: Dropping Balls
Post by: _vince on June 10, 2018, 07:45:21 pm
Here's a draft, several flaws though.

Code: QB64: [Select]
  1. _TITLE "Dropping Balls 2 w sound by vince 2018-03-31"
  2. ' attempt to fix
  3. CONST xmax = 800
  4. CONST ymax = 600
  5.  
  6. pi = 3.1415926
  7.  
  8. SCREEN _NEWIMAGE(xmax, ymax, 32)
  9. '_SCREENMOVE 360, 60
  10.  
  11. gravity = 1
  12. balls = 1000
  13. DIM x(balls), y(balls), r(balls), c(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  14.  
  15.  
  16. screen ,,1,0
  17. line (0,0)-(800,600),_rgb(0,0,0),bf
  18. a=0
  19. xx = 150*sin(2*a)
  20. yy = 300*cos(a)
  21. pset (xx+400,yy+300), _rgb(100,100,100)
  22. for a = 0 to 2*pi step 0.01
  23.         xx = 150*sin(2*a)
  24.         yy = 300*cos(a)
  25.         line -(xx+400,yy+300), _rgb(100,100,100)
  26.  
  27. paint (400,150),_rgb(100,100,100)
  28. paint (400,450),_rgb(100,100,100)
  29. line (380,150)-(420,450),_rgb(100,100,100),bf
  30.  
  31. pcopy 1,0
  32.  
  33.  
  34. balls = 0
  35. for yy = 50 to 300-10 step 8
  36. for xx = 10 to 600-10 step 8
  37.         if point(xx,yy) <> _rgb(0,0,0) then
  38.                 balls = balls + 1
  39.  
  40.                 r(balls) = 3
  41.                 x(balls) = xx
  42.                 y(balls) = yy
  43.                 c(balls) = 15
  44.                 dx(balls) = rand(0, 3) * rdir
  45.                 dy(balls) = 15
  46.  
  47.                 rr(balls) = 255
  48.                 gg(balls) = 255
  49.                 bb(balls) = 0
  50.         end if
  51.  
  52. screen ,,0,0
  53.  
  54. 'FOR i = 1 TO balls
  55. '    r(i) = rand(15, 20)
  56. '    x(i) = rand(r(i), xmax - r(i))
  57. '    y(i) = rand(r(i), ymax - r(i))
  58. '    c(i) = rand(1, 15)
  59. '    dx(i) = rand(0, 3) * rdir
  60. '    dy(i) = rand(10, 20)
  61. '    rr(i) = rand(200, 255)
  62. '    gg(i) = rand(200, 255)
  63. '    bb(i) = rand(200, 255)
  64. 'NEXT
  65.         pcopy 1,0
  66.     FOR i = 1 TO balls
  67.         'ready for collision
  68.         dy(i) = dy(i) + gravity
  69.         a(i) = _ATAN2(dy(i), dx(i))
  70.         power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  71.         imoved = 0
  72.         FOR j = i + 1 TO balls
  73.             IF SQR((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2) < r(i) + r(j) THEN
  74.                 imoved = 1
  75.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  76.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  77.                 'update new dx, dy for i and j balls
  78.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  79.                 power = .7 * (power1 + power2) / 2
  80.                 dx(i) = power * COS(a(i))
  81.                 dy(i) = power * SIN(a(i))
  82.                 dx(j) = power * COS(a(j))
  83.                 dy(j) = power * SIN(a(j))
  84.                 x(i) = x(i) + dx(i)
  85.                 y(i) = y(i) + dy(i)
  86.                 x(j) = x(j) + dx(j)
  87.                 y(j) = y(j) + dy(j)
  88.                 snd 120 + r(i) * 250, r(j) * .15
  89.                 EXIT FOR
  90.             END IF
  91.         NEXT
  92.         IF imoved = 0 THEN
  93.             x(i) = x(i) + dx(i)
  94.             y(i) = y(i) + dy(i)
  95.         END IF
  96.         'IF x(i) < -r(i) OR x(i) > xmax + r(i) THEN
  97.         '    x(i) = xmax / 2 + rand(0, 100) * rdir
  98.         '    y(i) = 0
  99.         '    dx(i) = rand(0, 3) * rdir
  100.         '    dy(i) = 1
  101.         'END IF
  102.         'IF y(i) + r(i) > ymax + gravity THEN snd (y(i) + r(i) - (ymax + gravity)) * 100 + r(i) * 20, 6 'only when hits floor, not for rolling balls
  103.                 screen ,,1,0
  104.                 '
  105.         IF point(x(i)+r(i),y(i))<>_rgb(100,100,100) THEN
  106.                         'y(i) = ymax - r(i)
  107.                         x(i) = x(i) - r(i)
  108.                         dy(i) = dy(i) * -.7
  109.                         dx(i) = dx(i) * -.7
  110.                         'x(i) = x(i) + .1 * dx(i)
  111.                 elseif  point(x(i)-r(i),y(i))<>_rgb(100,100,100) then
  112.                         x(i) = x(i) + r(i)
  113.                         dy(i) = dy(i) * -.7
  114.                         dx(i) = dx(i) * -.7
  115.                 elseif  point(x(i),y(i)+r(i))<>_rgb(100,100,100) then
  116.                         y(i) = y(i) - r(i)
  117.                         dy(i) = dy(i) * -.7
  118.                         dx(i) = dx(i) * -.7
  119.                 elseif  point(x(i),y(i)-r(i))<>_rgb(100,100,100) then
  120.                         y(i) = y(i) + r(i)
  121.                         dy(i) = dy(i) * -.7
  122.                         dx(i) = dx(i) * -.7
  123.                 end if
  124.                 screen ,,0,0
  125.  
  126.         FOR rad = r(i) TO 1 STEP -1
  127.             COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  128.             fcirc x(i), y(i), rad
  129.         NEXT
  130.     NEXT
  131.     _DISPLAY
  132.     _LIMIT 20
  133.  
  134. FUNCTION rand (lo, hi)
  135.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  136.  
  137. FUNCTION rdir ()
  138.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  139.  
  140. 'Steve McNeil's  copied from his forum  note: Radius is too common a name ;-)
  141. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  142.     DIM subRadius AS LONG, RadiusError AS LONG
  143.     DIM X AS LONG, Y AS LONG
  144.  
  145.     subRadius = ABS(R)
  146.     RadiusError = -subRadius
  147.     X = subRadius
  148.     Y = 0
  149.  
  150.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  151.  
  152.     ' Draw the middle span here so we don't draw it twice in the main loop,
  153.     ' which would be a problem with blending turned on ;-)
  154.     LINE (CX - X, CY)-(CX + X, CY), , BF
  155.  
  156.     WHILE X > Y
  157.         RadiusError = RadiusError + Y * 2 + 1
  158.         IF RadiusError >= 0 THEN
  159.             IF X <> Y + 1 THEN
  160.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  161.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  162.             END IF
  163.             X = X - 1
  164.             RadiusError = RadiusError - X * 2
  165.         END IF
  166.         Y = Y + 1
  167.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  168.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  169.     WEND
  170.  
  171. SUB snd (frq, dur)
  172.     'SOUND frq / 2.2, dur * .01
  173.  
  174.  
  175.  
Title: Re: Dropping Balls
Post by: _vince on June 10, 2018, 08:26:37 pm
As requested by Phil

Code: QB64: [Select]
  1. _TITLE "Dropping Balls pile attempt bplus ;-)"
  2. 'attempt to build pile by adjusting drop rate, elasticity, gravity
  3. ' remove sound and adjust dropping to center of screen
  4.  
  5. ' built from Dropping balls 4 w snd and STATIC created 2018-04-3
  6. ' add STATIC's moving ball before figuring bounce from collision
  7. ' which was a mod of Dropping Balls 2 w sound posted 2018-03-31
  8. CONST xmax = 800
  9. CONST ymax = 600
  10.  
  11. pi = 3.1415926
  12.  
  13. SCREEN _NEWIMAGE(xmax, ymax, 32)
  14. _SCREENMOVE 360, 60
  15. elastic = .5
  16. gravity = .5
  17. balls = 160
  18. DIM x(balls), y(balls), r(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  19.  
  20. dim aa(balls)
  21. dim aaa(balls)
  22. FOR i = 1 TO balls
  23.     r(i) = 50
  24.     x(i) = xmax / 2 + (i MOD 2) * 8 - 4
  25.     y(i) = 0
  26.     dx(i) = 0
  27.     dy(i) = 3
  28.     rr(i) = rand(200, 255)
  29.     gg(i) = rand(200, 255)
  30.     bb(i) = rand(200, 255)
  31.         aa(i) = 2*pi*rnd
  32.         aaa(i) = 0
  33.         'rnd*0.1-0.05
  34. maxBall = 0
  35.  
  36. t = timer
  37.     CLS
  38.     'loopCnt = loopCnt + 1
  39.     'IF loopCnt MOD 17 = 0 THEN
  40.         IF maxBall < balls and timer-t>1 THEN
  41.                 maxBall = maxBall + 1
  42.                 t = timer
  43.         end if
  44.  
  45.  
  46.     'END IF
  47.     COLOR _RGB32(255, 255, 255)
  48.     _PRINTSTRING (100, 10), "Balls:" + STR$(maxBall)
  49.  
  50.     FOR i = 1 TO maxBall
  51.                 aa(i) = aa(i) + aaa(i)
  52.  
  53.                 if abs(aaa(i)) > 0.5 then aaa(i)=aaa(i)+ sgn(aaa(i))*0.02
  54.  
  55.         'ready for collision
  56.         dy(i) = dy(i) + gravity
  57.         a(i) = _ATAN2(dy(i), dx(i))
  58.  
  59.         imoved = 0
  60.         FOR j = i + 1 TO maxBall
  61.  
  62.             ' The following is STATIC's adjustment of ball positions if overlapping
  63.             ' before calcultion of new positions from collision
  64.             ' Displacement vector and its magnitude.  Thanks STxAxTIC !
  65.             nx = x(j) - x(i)
  66.             ny = y(j) - y(i)
  67.             nm = SQR(nx ^ 2 + ny ^ 2)
  68.             IF nm < 1 + r(i) + r(j) THEN
  69.                 nx = nx / nm
  70.                 ny = ny / nm
  71.  
  72.                 ' Regardless of momentum exchange, separate the balls along the lone connecting them.
  73.                 DO WHILE nm < 1 + r(i) + r(j)
  74.                     flub = .001 '* RND
  75.  
  76.                     x(j) = x(j) + flub * nx
  77.                     y(j) = y(j) + flub * ny
  78.  
  79.                     x(i) = x(i) - flub * nx
  80.                     y(i) = y(i) - flub * ny
  81.  
  82.                     nx = x(j) - x(i)
  83.                     ny = y(j) - y(i)
  84.                     nm = SQR(nx ^ 2 + ny ^ 2)
  85.                     nx = nx / nm
  86.                     ny = ny / nm
  87.                 LOOP
  88.  
  89.  
  90.                 imoved = 1
  91.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  92.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  93.  
  94.                 'update new dx, dy for i and j balls
  95.                 power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  96.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  97.                 power = elastic * (power1 + power2) / 2
  98.                 dx(i) = power * COS(a(i))
  99.                 dy(i) = power * SIN(a(i))
  100.                 dx(j) = power * COS(a(j))
  101.                 dy(j) = power * SIN(a(j))
  102.                 x(i) = x(i) + dx(i)
  103.                 y(i) = y(i) + dy(i)
  104.                 x(j) = x(j) + dx(j)
  105.                 y(j) = y(j) + dy(j)
  106.                 'EXIT FOR
  107.  
  108.                                 aaa(i) = rnd*0.2-0.1
  109.             END IF
  110.         NEXT
  111.         IF imoved = 0 THEN
  112.             x(i) = x(i) + dx(i)
  113.             y(i) = y(i) + dy(i)
  114.         END IF
  115.         IF x(i) < -r(i) OR x(i) > xmax + r(i) THEN
  116.             x(i) = xmax / 2 + (i MOD 2) * 4 * r(i) - 2 * r(i)
  117.             y(i) = 0
  118.             dx(i) = 0
  119.             dy(i) = 3
  120.  
  121.                                 aaa(i) = rnd*0.2-0.1
  122.         END IF
  123.  
  124.         IF y(i) + r(i) > ymax THEN
  125.                 y(i) = ymax - r(i): dy(i) = -dy(i) * elastic '???: x(i) = x(i) + .1 * dx(i)
  126.  
  127.                                 aaa(i) = rnd*0.2-0.1
  128.                 end if
  129.  
  130.         FOR rad = r(i) TO 1 STEP -1
  131.             COLOR _RGB32(rr(i) - 3 * rad, gg(i) - 3 * rad, bb(i) - 3 * rad)
  132.             fcirc x(i), y(i), rad
  133.         NEXT
  134.                 textrot x(i), y(i), ltrim$(str$(i)), aa(i), aa(i)
  135.  
  136.     NEXT
  137.     _DISPLAY
  138.     _LIMIT 30
  139.  
  140.  
  141. sub textrot (x0 as integer, y0 as integer, s as string, a as double, b as double)
  142.  
  143.         dim img as long
  144.         img = _newimage(16,16,32)
  145.  
  146.         _dest img
  147.         line (0,0)-(16,16),_rgb(0,0,0),bf
  148.         color _rgb(255,255,255)
  149.         _printstring (0,0),left$(s$,2),img
  150.  
  151.         _source img
  152.         _dest 0
  153.         dim z as long
  154.         r = 0.5
  155.         for yy = -16/r to 16/r
  156.         for xx = -16/r to 16/r
  157.                 xxx=r*xx*cos(a)+r*yy*sin(a)+8
  158.                 yyy=r*yy*cos(a)-r*xx*sin(a)+8
  159.                 if xxx<15 and xxx>=0 then
  160.                 if yyy<15 and yyy>=0 then
  161.                 z = point(xxx,yyy)
  162.                 if z = _rgb(255,255,255) then
  163.                         pset (x0+xx,y0+yy),_rgb(0,0,0)
  164.                 end if
  165.                 end if
  166.                 end if
  167.         next
  168.         next
  169.  
  170.         _freeimage(img)
  171.  
  172.         _source 0
  173.  
  174. FUNCTION rand (lo, hi)
  175.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  176.  
  177. FUNCTION rdir ()
  178.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  179.  
  180. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  181. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  182.     DIM subRadius AS LONG, RadiusError AS LONG
  183.     DIM X AS LONG, Y AS LONG
  184.  
  185.     subRadius = ABS(R)
  186.     RadiusError = -subRadius
  187.     X = subRadius
  188.     Y = 0
  189.  
  190.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  191.  
  192.     ' Draw the middle span here so we don't draw it twice in the main loop,
  193.     ' which would be a problem with blending turned on.
  194.     LINE (CX - X, CY)-(CX + X, CY), , BF
  195.  
  196.     WHILE X > Y
  197.         RadiusError = RadiusError + Y * 2 + 1
  198.         IF RadiusError >= 0 THEN
  199.             IF X <> Y + 1 THEN
  200.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  201.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  202.             END IF
  203.             X = X - 1
  204.             RadiusError = RadiusError - X * 2
  205.         END IF
  206.         Y = Y + 1
  207.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  208.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  209.     WEND
  210.  
  211. SUB snd (frq, dur)
  212.     'SOUND frq / 2.2, dur * .01
  213.  
Title: Re: Dropping Balls
Post by: FellippeHeitor on June 10, 2018, 08:28:22 pm
I'm stunned! That was awesome, Vince!
Title: Re: Dropping Balls
Post by: bplus on June 10, 2018, 08:36:39 pm
Hi [banned user],

The mirror effect helps explain what I first saw. I like silence too, and the song and unique sounds. Thanks for adding notes to code.


Hi V,

The hour glass is good start, ball containment seems first priority but some are being contained, I will look into what you are doing with that more closely.

The 2nd one with the numbers rotated on balls is cool and it would be really interesting if a constant but different spin were added to each ball as they move around. Nice one guys!

I have to confess, when I saw hour glass running I was thinking lottery and the ping pong balls WITH NUMBERS flying around! :D
Title: Re: Dropping Balls
Post by: FellippeHeitor on June 10, 2018, 08:40:24 pm
I have to confess, when I saw hour glass running I was thinking lottery and the ping pong balls WITH NUMBERS flying around! :D

Exactly what I had in mind when I proposed it too!
Title: Re: Dropping Balls
Post by: bplus on June 10, 2018, 09:02:38 pm
Hey great minds think alike!  oops... ;D

Title: Re: Dropping Balls
Post by: Ashish on June 11, 2018, 08:16:29 am
@vince
That hourglass one simulation was good!
Title: Re: Dropping Balls
Post by: TempodiBasic on June 11, 2018, 09:34:16 am
Hy Guys
Hy Vincent

very very fine effect in your mod/demo... but it is outlaw! :-) for gravity law surely....
see image to get into issue....

Thank's to share code
Title: Re: Dropping Balls
Post by: bplus on June 11, 2018, 10:33:24 am
Yes, I found that problem developing in my run too, the problem with modifying over and over... I had that fixed (maybe I should say balanced) in original code by altering the drop point. It also has something to do with elastic = sticky variable the lower the elastic setting the higher the stickiness.
Title: Re: Dropping Balls
Post by: _vince on June 11, 2018, 01:27:02 pm
Hy Guys
Hy Vincent

very very fine effect in your mod/demo... but it is outlaw! :-) for gravity law surely....
see image to get into issue....

Thank's to share code

Entirely bplus's and xstatic's fault. I had no part in the physics simulation.
Title: Re: Dropping Balls
Post by: bplus on June 11, 2018, 02:26:35 pm
Hy Guys
Hy Vincent

very very fine effect in your mod/demo... but it is outlaw! :-) for gravity law surely....
see image to get into issue....

Thank's to share code

Entirely bplus's and xstatic's fault. I had no part in the physics simulation.

V you have changed the code since I saw it (the problem is now exaggerated! probably due to big increase in Ball Radius), don't blame me for what you do with the code.

Append: I will say also (in your defense) that it is probably quite unexpected that a change in ball radius should effect "the physic's" but this code was modified from original dropping balls to try and get as perfect a pyramid formed and to do that "the physics" was already stretched beyond real world kind (if it was ever close to start).  Also by same logic, STxAxTIC should get 0 blame for what I did with his code.

Title: Re: Dropping Balls
Post by: _vince on June 11, 2018, 04:07:40 pm
I'm joking, bplus. Tempodibasic's riveting analysis warranted the response.
Title: Re: Dropping Balls
Post by: bplus on June 11, 2018, 04:23:18 pm
I'm joking, bplus. Tempodibasic's riveting analysis warranted the response.

I was hoping that! but apparently I don't have much faith in my hopes.

Well I really like the numbers on the balls and would like to see them spin independently as they move and the hour glass thing might spin off to lottery thing. (pun intended)
Title: Re: Dropping Balls
Post by: _vince on June 13, 2018, 12:24:48 am
By the way, bplus, I may as well ask here. Since you make these screensaver/graphic demo type programs (as do I) in a variety of *BASICs (smallbasic, largebasic, purebasic, justbasic, etc), how do you feel about FreeBASIC?  It's pretty much the best for this sort of thing: makes small speedy exes, all the same graphics capability, cleaner syntax, and doesn't obscure advanced features, such as memory access, from the user. There's #lang "fblite" if you want it more QB-like, ie not forcing variable declaration. I suppose it doesn't have the same pull for the old DOS QB45 compatible nostalgia crowd and may not be as beginner friendly as the other BASICs but it is objectively the best.  I would certainly prefer it over QB64 for this type of thing, I wonder if you feel the same.
Title: Re: Dropping Balls
Post by: bplus on June 13, 2018, 09:23:44 am
Hi V,

I did try FB before QB64, quite a struggle to get started with it. Since I got into QB64, 50% of my FB no longer works. It is too confusing learning these 2 Basic's so similar and yet... I try running code off their forum and usually something goes wrong for me. Seems to me, QB64 has plenty of graphics devices for interesting screen stuff specially compared to what I had been using previous couple of years.

I started looking into your Hour Glass modification last night, looks like you are flipping between 2 screen pages which threw me (a technique I also found at FB instead of QB64 _DISPLAY which does the screen update in one go). But the one thing that really blew my mind was this:
Code: QB64: [Select]
  1.  
  2. SCREEN , , 1, 0 'hmm never used these before = active page 1, visual page 0
  3. LINE (0, 0)-(800, 600), _RGB(0, 0, 0), BF '>>>>>>>>>>>>>>>>>>>>> If I comment out just this one line
  4. a = 0
  5. xx = 150 * SIN(2 * a)
  6. yy = 300 * COS(a)
  7. PSET (xx + 400, yy + 300), _RGB(100, 100, 100)
  8. FOR a = 0 TO 2 * pi STEP 0.01
  9.     xx = 150 * SIN(2 * a)
  10.     yy = 300 * COS(a)
  11.     LINE -(xx + 400, yy + 300), _RGB(100, 100, 100)
  12.  
  13. PAINT (400, 150), _RGB(100, 100, 100)
  14. PAINT (400, 450), _RGB(100, 100, 100)
  15. LINE (380, 150)-(420, 450), _RGB(100, 100, 100), BF
  16.  
  17. PCOPY 1, 0  'new also to me
  18.  
  19.  
  20. balls = 0
  21. FOR yy = 50 TO 300 - 10 STEP 8
  22.     FOR xx = 10 TO 600 - 10 STEP 8
  23.         IF POINT(xx, yy) <> _RGB(0, 0, 0) THEN
  24.             balls = balls + 1
  25.             '  VVVVVVVVVVVVVVVVVVVVVV stuff here starts giving me errors, for ball array assignment???????
  26.             r(balls) = 3
  27.             x(balls) = xx
  28.             y(balls) = yy
  29.             c(balls) = 15
  30.             dx(balls) = rand(0, 3) * rdir
  31.             dy(balls) = 15
  32.  
  33.             rr(balls) = 255
  34.             gg(balls) = 255
  35.             bb(balls) = 0
  36.         END IF
  37.     NEXT
  38.  
  39. SCREEN , , 0, 0
  40.  
  41.  

How can a simple LINE statement that basically clears the screen be so vital to ball array assignments?

Also why do the balls immediately head to the right of the screen when not contained by the Hour glass figure? Their change in direction, dx (maybe called the x vector), should not be that uniform at all.

Title: Re: Dropping Balls
Post by: Petr on June 13, 2018, 02:51:10 pm
Quote
Hi V,

I did try FB before QB64, quite a struggle to get started with it. Since I got into QB64, 50% of my FB no longer works. It is too confusing learning these 2 Basic's so similar and yet... I try running code off their forum and usually something goes wrong for me. Seems to me, QB64 has plenty of graphics devices for interesting screen stuff specially compared to what I had been using previous couple of years.

I started looking into your Hour Glass modification last night, looks like you are flipping between 2 screen pages which threw me (a technique I also found at FB instead of QB64 _DISPLAY which does the screen update in one go). But the one thing that really blew my mind was this:
Code: [Select]

SCREEN , , 1, 0 'hmm never used these before = active page 1, visual page 0
LINE (0, 0)-(800, 600), _RGB(0, 0, 0), BF '>>>>>>>>>>>>>>>>>>>>> If I comment out just this one line
a = 0
xx = 150 * SIN(2 * a)
yy = 300 * COS(a)
PSET (xx + 400, yy + 300), _RGB(100, 100, 100)
FOR a = 0 TO 2 * pi STEP 0.01
    xx = 150 * SIN(2 * a)
    yy = 300 * COS(a)
    LINE -(xx + 400, yy + 300), _RGB(100, 100, 100)
NEXT

PAINT (400, 150), _RGB(100, 100, 100)
PAINT (400, 450), _RGB(100, 100, 100)
LINE (380, 150)-(420, 450), _RGB(100, 100, 100), BF

PCOPY 1, 0  'new also to me


balls = 0
FOR yy = 50 TO 300 - 10 STEP 8
    FOR xx = 10 TO 600 - 10 STEP 8
        IF POINT(xx, yy) <> _RGB(0, 0, 0) THEN
            balls = balls + 1
            '  VVVVVVVVVVVVVVVVVVVVVV stuff here starts giving me errors, for ball array assignment???????
            r(balls) = 3
            x(balls) = xx
            y(balls) = yy
            c(balls) = 15
            dx(balls) = rand(0, 3) * rdir
            dy(balls) = 15

            rr(balls) = 255
            gg(balls) = 255
            bb(balls) = 0
        END IF
    NEXT
NEXT

SCREEN , , 0, 0


How can a simple LINE statement that basically clears the screen be so vital to ball array assignments?

Also why do the balls immediately head to the right of the screen when not contained by the Hour glass figure? Their change in direction, dx (maybe called the x vector), should not be that uniform at all.



I was curious and tried it. And I got the hourglass body. Could you give me a link where this source comes from? Thanks

Code: QB64: [Select]
  1.  
  2. 'SCREEN , , 1, 0 'hmm never used these before = active page 1, visual page 0
  3.  
  4. one& = _NEWIMAGE(800, 600, 32)
  5. _DEST one&
  6.  
  7. LINE (0, 0)-(800, 600), _RGB(0, 0, 0), BF '>>>>>>>>>>>>>>>>>>>>> If I comment out just this one line
  8. a = 0
  9. xx = 150 * SIN(2 * a)
  10. yy = 300 * COS(a)
  11. PSET (xx + 400, yy + 300), _RGB(100, 100, 100)
  12. FOR a = 0 TO 2 * _PI STEP 0.01
  13.     xx = 150 * SIN(2 * a)
  14.     yy = 300 * COS(a)
  15.     LINE -(xx + 400, yy + 300), _RGB(100, 100, 100)
  16.  
  17. PAINT (400, 150), _RGB(100, 100, 100)
  18. PAINT (400, 450), _RGB(100, 100, 100)
  19. LINE (380, 150)-(420, 450), _RGB(100, 100, 100), BF
  20.  
  21. PCOPY 1, 0 'standard Qbasic statement - copy videomemory source, dest
  22.  
  23.  
  24. 'balls = 0
  25. TYPE balls
  26.     r AS INTEGER
  27.     x AS INTEGER
  28.     y AS INTEGER
  29.     c AS LONG
  30.     dx AS INTEGER
  31.     dy AS INTEGER
  32.     rr AS _UNSIGNED _BYTE
  33.     gg AS _UNSIGNED _BYTE
  34.     bb AS _UNSIGNED _BYTE
  35. REDIM balls(0) AS balls
  36.  
  37.  
  38. FOR yy = 50 TO 300 - 10 STEP 8
  39.     FOR xx = 10 TO 600 - 10 STEP 8
  40.         _SOURCE one&
  41.         IF POINT(xx, yy) <> _RGB(0, 0, 0) THEN
  42.             balls = balls + 1
  43.             REDIM _PRESERVE balls(balls) AS balls
  44.             '  VVVVVVVVVVVVVVVVVVVVVV stuff here starts giving me errors, for ball array assignment???????
  45.             balls(balls).r = 3
  46.             balls(balls).x = xx
  47.             balls(balls).y = yy
  48.             balls(balls).c = 15
  49.             balls(balls).dx = RND * 3 * rdir 'this i know not. What is it rdir?
  50.             balls(balls).dy = 15
  51.  
  52.             balls(balls).rr = 255
  53.             balls(balls).gg = 255
  54.             balls(balls).bb = 0
  55.         END IF
  56.     NEXT
  57.  
  58. 'SCREEN , , 0, 0
  59. SCREEN one&
  60.  

I have never seen Free Basic.
Title: Re: Dropping Balls
Post by: bplus on June 13, 2018, 03:58:42 pm
Quote
I was curious and tried it. And I got the hourglass body. Could you give me a link where this source comes from? Thanks

Reply #12 of this thread is Vince mod of Dropping Balls with the Hour Glass figure.

Title: Re: Dropping Balls
Post by: Petr on June 13, 2018, 05:22:26 pm
Aha! Good work. Thank for reply.
Title: Re: Dropping Balls
Post by: _vince on June 13, 2018, 07:26:20 pm
How can a simple LINE statement that basically clears the screen be so vital to ball array assignments?

The double for loop traverses the entire screen and only creates a ball if it's inside the hourglass by reading the colour of the screen.  I've used screen ,,1,0 for storing the hourglass image and drawing it on the main screen with pcopy 1,0, not for double buffering or screen syncing.  The same could be done with _source,_dest,_newimage, and _putimage.
Title: Re: Dropping Balls
Post by: bplus on June 14, 2018, 09:16:18 am
Thanks, have that figured out and can contain the balls inside hourglass but balls are disappearing at bottom so maybe a leak. I can do the thing without color detection by using two boundary arrays but getting build at bottom has been very frustrating .
Title: Re: Dropping Balls
Post by: bplus on March 04, 2020, 10:33:20 pm
The Hourglass challenge was finally solved here:
https://www.qb64.org/forum/index.php?topic=1035.msg102243#msg102243

and put to practical use as a minute timer (more or less).
https://www.qb64.org/forum/index.php?topic=1035.msg102358#msg102358

I am bumping to work with Qwerkey in regards to this:
Quote
@bplus, I'd like to add your Dropping Balls program https://www.qb64.org/forum/index.php?topic=194.0 to Samples Gallery (2D/3D Graphics without SUB _GL).  It has very good graphics and the whole post is a good demonstration of collaborative work between various members.  There is quite a bit of amazing stuff in that post.  I want to use the code of « Reply #4 on: April 03, 2018, 05:39:05 PM » in Samples.  That code could do with a bit of tidying up (and perhaps a few more explanatory comments).  Would you be able to do that before I add to Samples?  Thanks, Qwerkey
Title: Re: Dropping Balls
Post by: bplus on March 05, 2020, 01:28:20 am
@Qwerkey New and I think improved :)

Code: QB64: [Select]
  1. _TITLE "Dropping Balls: Pile Attempt #3" ' bplus started 2018-04-03"
  2. ' Attempt to build a pile by adjusting drop rate, elasticity, and gravity.
  3. ' Built from Dropping balls 4 w snd and STATIC created 2018-04-3
  4. ' Add STATIC's ball moving before figuring any bounce from collision
  5. ' which was a mod in Dropping Balls 2 w sound posted 2018-03-31.
  6. ' 2020-03-04 Pile Attempt #3 revive and tidy up
  7.  
  8. CONST xmax = 750, ymax = 720, elastic = .8, gravity = .75, balls = 400, br = 15
  9. SCREEN _NEWIMAGE(xmax, ymax, 32)
  10. _SCREENMOVE 360, 20
  11. DIM x(balls), y(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  12. FOR i = 1 TO balls 'initialize balls to drop
  13.     x(i) = xmax / 2 + (i MOD 2) * 8 - 4: y(i) = 0 '                                     location
  14.     dx(i) = 0: dy(i) = 3 '                                                        change on axis
  15.     rr(i) = 150 + RND * 100: gg(i) = 150 + RND * 100: bb(i) = 150 + RND * 100 '        rgb color
  16.     CLS
  17.     loopCnt = loopCnt + 1 '                   drop ball every 17 loops so previous ball is clear
  18.     IF loopCnt MOD 17 = 0 THEN
  19.         IF maxBall < balls THEN maxBall = maxBall + 1
  20.     END IF
  21.     _PRINTSTRING (100, 10), "Balls:" + STR$(maxBall)
  22.     FOR i = 1 TO maxBall
  23.         'ready for collision
  24.         dy(i) = dy(i) + gravity '                               gravity increase update on y axis
  25.         a(i) = _ATAN2(dy(i), dx(i)) '                                       angle ball is heading
  26.         imoved = 0
  27.         FOR j = i + 1 TO maxBall
  28.             '      The following is STxAxTIC's adjustment of ball positions if overlapping before
  29.             ' calculation of new positions from collision. Displacement vector and its magnitude:
  30.             nx = x(j) - x(i): ny = y(j) - y(i)
  31.             nm = SQR(nx ^ 2 + ny ^ 2)
  32.             IF nm < 1 + 2 * br THEN
  33.                 nx = nx / nm: ny = ny / nm
  34.                 ' Regardless of momentum exchange, separate balls along the line connecting them.
  35.                 DO WHILE nm < 1 + 2 * br
  36.                     flub = .001
  37.                     x(j) = x(j) + flub * nx: y(j) = y(j) + flub * ny
  38.                     x(i) = x(i) - flub * nx: y(i) = y(i) - flub * ny
  39.                     nx = x(j) - x(i): ny = y(j) - y(i)
  40.                     nm = SQR(nx ^ 2 + ny ^ 2)
  41.                     nx = nx / nm: ny = ny / nm
  42.                 LOOP
  43.                 imoved = 1
  44.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  45.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  46.                 power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5 '       update new dx, dy for i and j balls
  47.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  48.                 power = elastic * (power1 + power2) / 2
  49.                 dx(i) = power * COS(a(i)): dy(i) = power * SIN(a(i))
  50.                 dx(j) = power * COS(a(j)): dy(j) = power * SIN(a(j))
  51.                 x(i) = x(i) + dx(i): y(i) = y(i) + dy(i)
  52.                 x(j) = x(j) + dx(j): y(j) = y(j) + dy(j)
  53.             END IF '                                                              Thanks STxAxTIC
  54.         NEXT
  55.         IF imoved = 0 THEN x(i) = x(i) + dx(i): y(i) = y(i) + dy(i)
  56.         IF x(i) - br < 0 OR x(i) + br > xmax THEN '       keep balls inside sides and bottom edge
  57.             dx(i) = -dx(i)
  58.             IF x(i) - br < 0 THEN x(i) = br
  59.             IF x(i) + br > xmax THEN x(i) = xmax - br
  60.         END IF
  61.         IF y(i) + br > ymax THEN y(i) = ymax - br: dy(i) = -dy(i) * elastic
  62.         FOR rad = br TO 1 STEP -1 '                                         finally draw the ball
  63.             fcirc x(i), y(i), rad, _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  64.         NEXT
  65.     NEXT
  66.     _DISPLAY
  67.     _LIMIT 20
  68.  
  69. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG, C AS _UNSIGNED LONG) '       SMcNeill's fill circle
  70.     DIM subRadius AS LONG, RadiusError AS LONG, X AS LONG, Y AS LONG
  71.     subRadius = ABS(R): RadiusError = -subRadius: X = subRadius: Y = 0
  72.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  73.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  74.     WHILE X > Y
  75.         RadiusError = RadiusError + Y * 2 + 1
  76.         IF RadiusError >= 0 THEN
  77.             IF X <> Y + 1 THEN
  78.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  79.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  80.             END IF
  81.             X = X - 1
  82.             RadiusError = RadiusError - X * 2
  83.         END IF
  84.         Y = Y + 1
  85.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  86.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  87.     WEND
  88.  

 

 
Title: Re: Dropping Balls
Post by: Qwerkey on March 05, 2020, 05:12:34 am
Now added to Samples.
Title: Re: Dropping Balls
Post by: _vince on March 05, 2020, 10:22:38 am
please fix bplus's name!
Title: Re: Dropping Balls
Post by: bplus on March 05, 2020, 11:24:46 am
please fix bplus's name!

Thanks _vince

@Qwerkey along with fixing names in Samples, I reworded a little as to how I remembered the experience.
Title: Re: Dropping Balls
Post by: Qwerkey on March 05, 2020, 12:16:27 pm
@bplus Thanks for fixing the text, but did you remove the @Author Name format in the Author ID line?  Fellippe wanted to change to this format.
Title: Re: Dropping Balls
Post by: bplus on March 05, 2020, 12:22:30 pm
@bplus Thanks for fixing the text, but did you remove the @Author Name format in the Author ID line?  Fellippe wanted to change to this format.

OK added @ but it does not make sense to me in a thread we can't reply in.

So what is the reasoning for @ here?
Title: Re: Dropping Balls
Post by: FellippeHeitor on March 05, 2020, 12:52:15 pm
Easy link to author's profile.
Title: Re: Dropping Balls
Post by: bplus on March 06, 2020, 10:38:36 am
OK now I see how it's working, good :)

@FellippeHeitor
Update: Dang! It did not notify me for Librarian's "Circle Intersect Line" post :(
It does seem to be working in Samples Child Boards.