Author Topic: LG SmartTV "loading" animation (kind of)  (Read 6295 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

FellippeHeitor

  • Guest
LG SmartTV "loading" animation (kind of)
« on: October 03, 2018, 12:28:53 am »
This was written just for fun and the inspiration for it is as stated in the title of this thread. It was supposed to be something similar to Newton's cradle, but it was easier to allow the circles to coexist in the z-axis so that I only needed to check collision between the currently moving circle and the next in the array (without having to calculate where they would rest and whatnot). The circles have an intentional gravitational tendency to remain at 90 degrees, even after having been hit.

This would likely have been much more efficient if Terry's library had been used, but I'm admittedly still to learn it.

Code: QB64: [Select]
  1. CONST false = 0, true = NOT false
  2.  
  3. SCREEN _NEWIMAGE(200, 200, 32)
  4.  
  5. TYPE ball
  6.     x AS SINGLE
  7.     y AS SINGLE
  8.     angle AS SINGLE
  9.     moving AS _BYTE
  10.  
  11. DIM ball(1 TO 4) AS ball
  12.  
  13. DATA 78,194,155,61
  14. DATA 90,194,89,83
  15. DATA 102,89,188,150
  16. DATA 138,83,128,188
  17.  
  18. FOR i = 1 TO 4
  19.     READ ball(i).angle
  20.     READ r, g, b
  21.     ball(i).color = _RGB32(r, g, b)
  22. ball(4).moving = true
  23.  
  24. x = _WIDTH / 2
  25. y = _WIDTH / 2
  26.  
  27.     CLS
  28.     CircleFill x, y, 60, _RGB32(216, 216, 216)
  29.     FOR i = 1 TO UBOUND(ball)
  30.         nextBall = i + 1
  31.         IF nextBall > UBOUND(ball) THEN nextBall = 1
  32.  
  33.         IF ball(i).moving THEN
  34.             ball(i).angle = (ball(i).angle + 6) MOD 360
  35.             ball(i).x = x + COS(_D2R(ball(i).angle)) * 50
  36.             ball(i).y = y + SIN(_D2R(ball(i).angle)) * 50
  37.             IF dist(ball(i).x, ball(i).y, ball(nextBall).x, ball(nextBall).y) <= 20 THEN
  38.                 ball(nextBall).moving = true
  39.                 ball(i).moving = false
  40.             END IF
  41.         ELSE
  42.             IF ball(i).angle < 90 THEN ball(i).angle = ball(i).angle + .5
  43.             ball(i).x = x + COS(_D2R(ball(i).angle)) * 50
  44.             ball(i).y = y + SIN(_D2R(ball(i).angle)) * 50
  45.         END IF
  46.     NEXT
  47.  
  48.     FOR i = 1 TO UBOUND(ball)
  49.         CircleFill ball(i).x, ball(i).y, 10, ball(i).color
  50.     NEXT
  51.  
  52.     _DISPLAY
  53.     _LIMIT 60
  54.  
  55. FUNCTION dist! (x1!, y1!, x2!, y2!)
  56.     dist! = _HYPOT((x2! - x1!), (y2! - y1!))
  57.  
  58. SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG)
  59.     DIM x0 AS SINGLE, y0 AS SINGLE
  60.     DIM e AS SINGLE
  61.  
  62.     x0 = R
  63.     y0 = 0
  64.     e = -R
  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.  

2018-10-03 01_33_30-Untitled.png
« Last Edit: October 03, 2018, 12:34:58 am by FellippeHeitor »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: LG SmartTV "loading" animation (kind of)
« Reply #1 on: October 03, 2018, 04:48:22 pm »

I find it cool!
I'm ready to watch cartoons.

Programming isn't difficult, only it's  consuming time and coffee

FellippeHeitor

  • Guest
Re: LG SmartTV "loading" animation (kind of)
« Reply #2 on: October 03, 2018, 04:49:11 pm »
Thanks, Tempodi!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: LG SmartTV "loading" animation (kind of)
« Reply #3 on: October 05, 2018, 11:00:22 am »
Here is the first vision I had for a mod of this:
Code: QB64: [Select]
  1. _TITLE "Dang balls again! by bplus 2018-10-03     press spacebar for different color setting..."
  2. 'QB64 X 64 version 1.2 20180228/86  from git b301f92
  3. CONST WW = 800
  4. CONST WH = 600
  5. SCREEN _NEWIMAGE(WW, WH, 32)
  6. _SCREENMOVE (1280 - WW) / 2 + 30, (760 - WH) / 2
  7. CONST PI = 3.141492653589793
  8. CONST PI2 = 1.570796326794897
  9. CONST bigr = 100
  10. CONST x0 = 400
  11. CONST y0 = 300
  12. TYPE ballType
  13.     x AS SINGLE
  14.     y AS SINGLE
  15.     c AS _UNSIGNED LONG
  16. DIM SHARED cN, pR, pG, pB
  17. DIM p5(4) AS ballType
  18. DIM p4(3) AS ballType
  19.  
  20. angle = 2 * PI / 20 ' 20 degrees
  21. smallr = bigr * SIN(.5 * angle)
  22.  
  23. resetPlasma
  24.  
  25. FOR i = 4 TO 0 STEP -1
  26.     p5(i).x = x0 + bigr * COS((i + 3) * angle)
  27.     p5(i).y = y0 + bigr * SIN((i + 3) * angle)
  28.     p5(i).c = changePlasma~&
  29.     'fcirc p5(i).x, p5(i).y, smallr
  30. bc~& = p5(1).c
  31. cN = cN - 4
  32. FOR i = 3 TO 0 STEP -1
  33.     p4(i).x = x0 + bigr * COS((i + 3.5) * angle)
  34.     p4(i).y = y0 + bigr * SIN((i + 3.5) * angle)
  35.     p4(i).c = changePlasma~&
  36.     'fcirc p4(i).x, p4(i).y, smallr
  37. d = .05
  38. WHILE _KEYDOWN(27) = 0
  39.     k$ = INKEY$
  40.     IF k$ = " " THEN resetPlasma
  41.     FOR a = (4 + 3) * angle TO 2 * PI + 3 * angle STEP PI / 45
  42.         CLS
  43.         COLOR _RGB32(200, 200, 200)
  44.         PRINT "R,G,B: ";
  45.         PRINT USING "#.####  "; pR, pG, pB
  46.         FOR i = 0 TO 3
  47.             fcirc p4(i).x, p4(i).y, smallr, p4(i).c
  48.         NEXT
  49.         fcirc x0 + bigr * COS(a), y0 + bigr * SIN(a), smallr, bc~&
  50.         _DISPLAY
  51.         _LIMIT 120
  52.     NEXT
  53.     bc~& = p5(4).c
  54.     FOR i = 4 TO 0 STEP -1
  55.         IF i <> 0 THEN p5(i).c = p5(i - 1).c ELSE p5(i).c = changePlasma~&
  56.         fcirc p5(i).x, p5(i).y, smallr, p5(i).c
  57.     NEXT
  58.     FOR i = 3 TO 0 STEP -1
  59.         p4(i).c = p5(i + 1).c
  60.     NEXT
  61.     _DISPLAY
  62.     _LIMIT 30
  63.  
  64. FUNCTION changePlasma~& ()
  65.     cN = cN + 1
  66.     changePlasma~& = _RGB32(127 + 127 * SIN(pR * cN), 127 + 127 * SIN(pG * cN), 127 + 127 * SIN(pB * cN))
  67.  
  68. SUB resetPlasma ()
  69.     pR = RND * RND: pG = RND * RND: pB = RND * RND
  70.  
  71.  
  72. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  73. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG, K AS _UNSIGNED LONG)
  74.     DIM subRadius AS LONG, RadiusError AS LONG
  75.     DIM X AS LONG, Y AS LONG
  76.  
  77.     subRadius = ABS(R)
  78.     RadiusError = -subRadius
  79.     X = subRadius
  80.     Y = 0
  81.  
  82.     IF subRadius = 0 THEN PSET (CX, CY), K: EXIT SUB
  83.  
  84.     ' Draw the middle span here so we don't draw it twice in the main loop,
  85.     ' which would be a problem with blending turned on.
  86.     LINE (CX - X, CY)-(CX + X, CY), K, BF
  87.  
  88.     WHILE X > Y
  89.         RadiusError = RadiusError + Y * 2 + 1
  90.         IF RadiusError >= 0 THEN
  91.             IF X <> Y + 1 THEN
  92.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), K, BF
  93.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), K, BF
  94.             END IF
  95.             X = X - 1
  96.             RadiusError = RadiusError - X * 2
  97.         END IF
  98.         Y = Y + 1
  99.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), K, BF
  100.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), K, BF
  101.     WEND

 
Dang! balls again!.PNG


But it's just a cheap trick not REAL Ball collisions like Fellippe's and this:
Code: QB64: [Select]
  1. _TITLE "Delta angle by bplus 2018-10-05"
  2.  
  3. ' collisions on the topological rim of circle
  4.  
  5. 'given 2 ball angles A and B there are 2 distances the Major and the Minor
  6. ' make a function that returns the Minor, so don't have to calculate the x, y coordinates of each ball
  7.  
  8.  
  9. 'QB64 X 64 version 1.2 20180228/86  from git b301f92
  10. CONST WW = 800
  11. CONST WH = 600
  12. SCREEN _NEWIMAGE(WW, WH, 32)
  13. _SCREENMOVE (1280 - WW) / 2 + 30, (760 - WH) / 2
  14. CONST PI = 3.141492653589793
  15. CONST PI2 = 1.570796326794897
  16. CONST BIGR = 100
  17. CONST x0 = 400
  18. CONST y0 = 300
  19. TYPE ballType
  20.     a AS SINGLE '           all balls are located by angle a
  21.     da AS SINGLE '          all balls move according to delta angle
  22.     c AS _UNSIGNED LONG '   all balls are colored
  23.  
  24. nBalls = 7
  25. DIM s(1 TO nBalls) AS ballType 'system array
  26. nDiv = 24 'divide circle n times
  27. angle = 2 * PI / nDiv ' 20 degrees, EDIT: no now it's 15 degrees! 360 / 24 = 15
  28. smallr = BIGR * SIN(.5 * angle)
  29.  
  30. 'initialize system, setup like Fellippe's only make it a 2D system
  31. FOR i = 1 TO nBalls
  32.     s(i).a = PI / 2 - (nBalls * PI / 20) - (nBalls) * .5 * angle + i * angle + i * PI / 20 'the last + to separate balls
  33.     IF i = nBalls THEN s(i).da = PI / 30 ELSE s(i).da = 0
  34.     s(i).c = _RGBA32((nBalls - i + 2) * 20, (nBalls - i + 2) * 20, (nBalls - i + 2) * 20, 100) 'use transpaency to check overlap
  35. l = 30
  36. WHILE _KEYDOWN(27) = 0
  37.     CLS
  38.     FOR i = 1 TO nBalls
  39.         'collision check  just check the ball after ball i
  40.         IF i = nBalls THEN j = 1 ELSE j = i + 1
  41.         IF MinorDistance!(s(i).a, s(j).a) < angle THEN 'collision
  42.             SWAP s(i).da, s(j).da
  43.             SWAP s(i).c, s(j).c
  44.         END IF
  45.     NEXT
  46.  
  47.     'update
  48.     FOR i = 1 TO nBalls
  49.         s(i).a = s(i).a + s(i).da
  50.         fcirc x0 + BIGR * COS(s(i).a), y0 + BIGR * SIN(s(i).a), smallr, s(i).c
  51.     NEXT
  52.     _DISPLAY
  53.     l = l + .1
  54.     IF l > 800 THEN l = 800
  55.     _LIMIT l
  56.  
  57. ' all angles are in radian units
  58. FUNCTION MinorDistance! (angleA, angleB)
  59.     ab = angleA - angleB
  60.     IF ab < 0 THEN ab = ab + 2 * PI
  61.     ba = angleB - angleA
  62.     IF ba < 0 THEN ba = ba + 2 * PI
  63.     IF ab < ba THEN MinorDistance! = ab ELSE MinorDistance! = ba
  64.  
  65. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  66. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG, K AS _UNSIGNED LONG)
  67.     DIM subRadius AS LONG, RadiusError AS LONG
  68.     DIM X AS LONG, Y AS LONG
  69.  
  70.     subRadius = ABS(R)
  71.     RadiusError = -subRadius
  72.     X = subRadius
  73.     Y = 0
  74.  
  75.     IF subRadius = 0 THEN PSET (CX, CY), K: EXIT SUB
  76.  
  77.     ' Draw the middle span here so we don't draw it twice in the main loop,
  78.     ' which would be a problem with blending turned on.
  79.     LINE (CX - X, CY)-(CX + X, CY), K, BF
  80.  
  81.     WHILE X > Y
  82.         RadiusError = RadiusError + Y * 2 + 1
  83.         IF RadiusError >= 0 THEN
  84.             IF X <> Y + 1 THEN
  85.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), K, BF
  86.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), K, BF
  87.             END IF
  88.             X = X - 1
  89.             RadiusError = RadiusError - X * 2
  90.         END IF
  91.         Y = Y + 1
  92.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), K, BF
  93.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), K, BF
  94.     WEND
  95.  

 
Delta angle.PNG



Append: Handling collisions when balls are next to or on top of one another is very tricky! Once again my brain is busted trying to handle this situation even on this 1D topology!
« Last Edit: October 05, 2018, 11:34:33 am by bplus »

FellippeHeitor

  • Guest
Re: LG SmartTV "loading" animation (kind of)
« Reply #4 on: October 05, 2018, 11:33:58 am »
Interesting mods, bplus.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: LG SmartTV "loading" animation (kind of)
« Reply #5 on: October 05, 2018, 11:40:51 am »
Quote
Append: Handling collisions when balls are next to or on top of one another is very tricky! Once again my brain is busted trying to handle this situation even on this 1D topology!

Hi Fellippe,

It was interesting and once again I am baffled by collision handling, not detection, deciding what happens after detection, specially when balls are next to each other. I almost got to one version of Windows "wait" animation.

Marked as best answer by on Today at 04:25:56 pm

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: LG SmartTV "loading" animation (kind of)
« Reply #6 on: October 06, 2018, 01:35:39 am »
  • Undo Best Answer
  • I'm curious about the CircleFill subroutine in your code. Is that a faster method than using PAINT?

    Where on Earth did you come up with that? It's boggling my mind.
    In order to understand recursion, one must first understand recursion.

    FellippeHeitor

    • Guest
    Re: LG SmartTV "loading" animation (kind of)
    « Reply #7 on: October 06, 2018, 01:53:08 am »
    The circleFill routine in my code is a courtesy of v: https://www.qb64.org/forum/index.php?action=profile;u=17

    The fcirc routine in bplus's code was written by Steve.

    It's not even a matter of being fast (didn't test it); it's a matter of being more efficient in that no paint ever leaks and you can also draw semitransparent filled circles with these.

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: LG SmartTV "loading" animation (kind of)
    « Reply #8 on: October 06, 2018, 09:20:01 am »
    I'm curious about the CircleFill subroutine in your code. Is that a faster method than using PAINT?

    Where on Earth did you come up with that? It's boggling my mind.

    Yes, mine came from Steve's example. I have tested it against anything else I found or invented. It was also discussed here:
    https://www.qb64.org/forum/index.php?topic=298.45

    Vince offered a similar routine that turns out to be basically the same when roundness fixed.

    It is based on radical symmetry of circle, do calculations on 1/8th of circle and you have all you need to finish the circle AND no overlap or gap when using alpha colors.

    Paint is slower and has to be used with cautions.

    I wonder if this routine could be added to QB64.exe?
    « Last Edit: October 06, 2018, 09:30:08 am by bplus »

    Offline SMcNeill

    • QB64 Developer
    • Forum Resident
    • Posts: 3972
      • View Profile
      • Steve’s QB64 Archive Forum
    Re: LG SmartTV "loading" animation (kind of)
    « Reply #9 on: October 06, 2018, 09:46:05 am »
    I'm curious about the CircleFill subroutine in your code. Is that a faster method than using PAINT?

    Where on Earth did you come up with that? It's boggling my mind.

    Yes, mine came from Steve's example. I have tested it against anything else I found or invented. It was also discussed here:
    https://www.qb64.org/forum/index.php?topic=298.45

    I enjoyed that topic.  Pondering over Petr's code got me to rethink how I've calculated values for _MEM routines in the past, and has helped me find a better way to tweak a lot of my own code to optimize its speed/performance.  With just a little work, we took his routine, optimized it a bit, and made it run 10x faster!

    ..A trick I wish I could pull off for all my project So!  :D
    https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

    Offline TerryRitchie

    • Seasoned Forum Regular
    • Posts: 495
    • Semper Fidelis
      • View Profile
    Re: LG SmartTV "loading" animation (kind of)
    « Reply #10 on: October 06, 2018, 01:52:11 pm »
    I just read through that whole topic and it was fascinating to see this evolve. I'll need to remember the BF trick with making lines faster.
    In order to understand recursion, one must first understand recursion.