Author Topic: Dropping Balls  (Read 31627 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dropping Balls
« Reply #30 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
« Last Edit: March 04, 2020, 10:35:48 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dropping Balls
« Reply #31 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.  

 
Dropping Balls Pile Attempt #3.PNG

 
« Last Edit: March 05, 2020, 01:43:20 am by bplus »

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Dropping Balls
« Reply #32 on: March 05, 2020, 05:12:34 am »
Now added to Samples.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Dropping Balls
« Reply #33 on: March 05, 2020, 10:22:38 am »
please fix bplus's name!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dropping Balls
« Reply #34 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.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Dropping Balls
« Reply #35 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.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dropping Balls
« Reply #36 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?

FellippeHeitor

  • Guest
Re: Dropping Balls
« Reply #37 on: March 05, 2020, 12:52:15 pm »
Easy link to author's profile.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Dropping Balls
« Reply #38 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.
« Last Edit: March 06, 2020, 10:47:43 am by bplus »