Author Topic: Deflection Balls  (Read 3980 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.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Deflection Balls
« on: December 06, 2020, 02:03:51 am »
B+ made something very similar to this maybe a year or so ago I believe. So I decided to attempt it on my own. :) I also used Steve's Gold Standard circle fill-in Sub.
I really don't know how the SIN and COS works, but it does. :)

Code: QB64: [Select]
  1. DIM dxx AS SINGLE, dyy AS SINGLE
  2. DIM dxxx AS SINGLE, dyyy AS SINGLE
  3. DIM dxxxx AS SINGLE, dyyyy AS SINGLE
  4. DIM dx2 AS SINGLE, dy2 AS SINGLE
  5. DIM dxx2 AS SINGLE, dyy2 AS SINGLE
  6. DIM dxxx2 AS SINGLE, dyyy2 AS SINGLE
  7. DIM dxxxx2 AS SINGLE, dyyyy2 AS SINGLE
  8.  
  9. _TITLE "Deflections"
  10.  
  11. SCREEN _NEWIMAGE(800, 600, 32)
  12.  
  13. cx = 400: cy = 300
  14. cx2 = 400: cy2 = 300
  15. cx3 = 400: cy3 = 300
  16. cx4 = 400: cy4 = 300
  17.  
  18. dx = 1.25
  19. dy = .25
  20. dxx = .25
  21. dyy = 1
  22. dxxx = 1
  23. dyyy = .25
  24. dxxxx = 1.25
  25. dyyyy = .75
  26.  
  27.     _LIMIT 100
  28.     cx = cx + dx
  29.     cy = cy + dy
  30.     cx2 = cx2 + dxx
  31.     cy2 = cy2 + dyy
  32.     cx3 = cx3 + dxxx
  33.     cy3 = cy3 + dyyy
  34.     cx4 = cx4 + dxxxx
  35.     cy4 = cy4 + dyyyy
  36.     r = 20
  37.     c = _RGB32(255, 0, 0)
  38.     fillCircle cx, cy, r, c
  39.     IF cx < 20 OR cx > 780 OR cy < 20 OR cy > 580 THEN
  40.         dx2 = (dx - cx)
  41.         dy2 = (dy - cy)
  42.         dx = SIN(dx2)
  43.         dy = COS(dy2)
  44.         dx = dx * 2
  45.         dy = dy * 2
  46.     END IF
  47.     r2 = 20
  48.     c2 = _RGB32(0, 255, 0)
  49.     fillCircle cx2, cy2, r2, c2
  50.     IF cx2 < 20 OR cx2 > 780 OR cy2 < 20 OR cy2 > 580 THEN
  51.         dxx2 = (dxx - cx2)
  52.         dyy2 = (dyy - cy2)
  53.         dxx = SIN(dxx2)
  54.         dyy = COS(dyy2)
  55.         dxx = dxx * 2
  56.         dyy = dyy * 2
  57.     END IF
  58.     r3 = 20
  59.     c3 = _RGB32(0, 0, 255)
  60.     fillCircle cx3, cy3, r3, c3
  61.     IF cx3 < 20 OR cx3 > 780 OR cy3 < 20 OR cy3 > 580 THEN
  62.         dxxx2 = (dxxx - cx3)
  63.         dyyy2 = (dyyy - cy3)
  64.         dxxx = SIN(dxxx2)
  65.         dyyy = COS(dyyy2)
  66.         dxxx = dxxx * 2
  67.         dyyy = dyyy * 2
  68.     END IF
  69.     r4 = 20
  70.     c4 = _RGB32(255, 255, 255)
  71.     fillCircle cx4, cy4, r4, c4
  72.     IF cx4 < 20 OR cx4 > 780 OR cy4 < 20 OR cy4 > 580 THEN
  73.         dxxxx2 = (dxxxx - cx4)
  74.         dyyyy2 = (dyyyy - cy4)
  75.         dxxxx = SIN(dxxxx2)
  76.         dyyyy = COS(dyyyy2)
  77.         dxxxx = dxxxx * 2
  78.         dyyyy = dyyyy * 2
  79.     END IF
  80.  
  81.     _DISPLAY
  82.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGB32(0, 0, 0, 5), BF
  83.  
  84. 'from Steve Gold standard
  85. SUB fillCircle (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  86.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  87.     DIM X AS INTEGER, Y AS INTEGER
  88.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  89.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  90.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  91.     WHILE X > Y
  92.         RadiusError = RadiusError + Y * 2 + 1
  93.         IF RadiusError >= 0 THEN
  94.             IF X <> Y + 1 THEN
  95.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  96.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  97.             END IF
  98.             X = X - 1
  99.             RadiusError = RadiusError - X * 2
  100.         END IF
  101.         Y = Y + 1
  102.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  103.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  104.     WEND
  105.  

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Deflection Balls
« Reply #1 on: December 06, 2020, 05:16:22 am »
Cool... A bit like Tron on steroids... Nicely done!
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Deflection Balls
« Reply #2 on: December 06, 2020, 12:52:33 pm »
Thanks Johnno :). Yeah I also used the trails in a huge way. lol

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Deflection Balls
« Reply #3 on: December 06, 2020, 01:17:01 pm »
Balls and colors moving around, what's not to like? :)

Hey is that Ken I see using a CircleFill SUB? Wow welcome to the new world! ;-))

Quote
I really don't know how the SIN and COS works, but it does. :)

You are changing the angle when you hit a border, dx and dy usually denote the change in x or y from one loop to the next. da for angle change would make this a little clearer.

Basically when you hit a border you change the sign of dx or dy depending if you hit the floor or ceiling change y component, hit walls = change x component  -1*dx or -1*dy just changes the direction from up to down or vice versa of left to right in dx or vice versa.

da can be calculated from dx and dy or vice versa, eg

'find dx, dy from a (not da)
dx = speed *cos(a)  ' a is ball angle (da is a change)
dy = speed *sin(a)

da = _atan2(dy, dx) ' I think

yeah this probably not going anywhere but if I keep plugging away... ;-))
Code: QB64: [Select]
  1. a = _PI / 4 ' this is 45 degrees in radians pi is 180, 2*pi full circle = 360
  2.  
  3. ' 45 degrees is SE in QB64 about 4:30 o'clock
  4. ' dx should be positve, dy also positive
  5.  
  6. speed = 1
  7. dx = speed * COS(a) ' a is ball angle (da is a change)
  8. dy = speed * SIN(a)
  9.  
  10. da = _ATAN2(dy, dx) ' I think
  11.  
  12. PRINT dx, dy, _R2D(da) ' 3rd number 45 ?
  13.  
  14.  
  15.  
  16.  
« Last Edit: December 06, 2020, 01:37:39 pm by bplus »

Marked as best answer by SierraKen on December 06, 2020, 08:27:37 am

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Deflection Balls
« Reply #4 on: December 06, 2020, 01:25:30 pm »
LOL Thanks B+! Here is an update to it. After awhile one of the balls were stuck moving the same ways forever pretty much. So I think I fixed it. I ran it for about 12 minutes or so with no problems. But you will have to let it run for maybe 15-20 seconds or so to be able to see all 4.

Code: QB64: [Select]
  1. DIM dxx AS SINGLE, dyy AS SINGLE
  2. DIM dxxx AS SINGLE, dyyy AS SINGLE
  3. DIM dxxxx AS SINGLE, dyyyy AS SINGLE
  4. DIM dx2 AS SINGLE, dy2 AS SINGLE
  5. DIM dxx2 AS SINGLE, dyy2 AS SINGLE
  6. DIM dxxx2 AS SINGLE, dyyy2 AS SINGLE
  7. DIM dxxxx2 AS SINGLE, dyyyy2 AS SINGLE
  8.  
  9. _TITLE "Deflections"
  10.  
  11. SCREEN _NEWIMAGE(800, 600, 32)
  12.  
  13. cx = 400: cy = 300
  14. cx2 = 400: cy2 = 300
  15. cx3 = 400: cy3 = 300
  16. cx4 = 400: cy4 = 300
  17.  
  18. dx = 1.25
  19. dy = .25
  20. dxx = .25
  21. dyy = 1
  22. dxxx = 1
  23. dyyy = .25
  24. dxxxx = 1.25
  25. dyyyy = .75
  26.  
  27.     _LIMIT 100
  28.     cx = cx + dx
  29.     cy = cy + dy
  30.     cx2 = cx2 + dxx
  31.     cy2 = cy2 + dyy
  32.     cx3 = cx3 + dxxx
  33.     cy3 = cy3 + dyyy
  34.     cx4 = cx4 + dxxxx
  35.     cy4 = cy4 + dyyyy
  36.     r = 20
  37.     c = _RGB32(255, 0, 0)
  38.     fillCircle cx, cy, r, c
  39.     IF cx < 20 OR cx > 780 OR cy < 20 OR cy > 580 THEN
  40.         dx2 = (dx - cx)
  41.         dy2 = (dy - cy)
  42.         dx = SIN(dx2)
  43.         dy = COS(dy2)
  44.         dx = dx * 1.75
  45.         dy = dy * 2
  46.     END IF
  47.     r2 = 20
  48.     c2 = _RGB32(0, 255, 0)
  49.     fillCircle cx2, cy2, r2, c2
  50.     IF cx2 < 20 OR cx2 > 780 OR cy2 < 20 OR cy2 > 580 THEN
  51.         dxx2 = (dxx - cx2)
  52.         dyy2 = (dyy - cy2)
  53.         dxx = SIN(dxx2)
  54.         dyy = COS(dyy2)
  55.         dxx = dxx * 1.75
  56.         dyy = dyy * 2
  57.     END IF
  58.     r3 = 20
  59.     c3 = _RGB32(0, 0, 255)
  60.     fillCircle cx3, cy3, r3, c3
  61.     IF cx3 < 20 OR cx3 > 780 OR cy3 < 20 OR cy3 > 580 THEN
  62.         dxxx2 = (dxxx - cx3)
  63.         dyyy2 = (dyyy - cy3)
  64.         dxxx = SIN(dxxx2)
  65.         dyyy = COS(dyyy2)
  66.         dxxx = dxxx * 1.75
  67.         dyyy = dyyy * 2
  68.     END IF
  69.     r4 = 20
  70.     c4 = _RGB32(255, 255, 255)
  71.     fillCircle cx4, cy4, r4, c4
  72.     IF cx4 < 20 OR cx4 > 780 OR cy4 < 20 OR cy4 > 580 THEN
  73.         dxxxx2 = (dxxxx - cx4)
  74.         dyyyy2 = (dyyyy - cy4)
  75.         dxxxx = SIN(dxxxx2)
  76.         dyyyy = COS(dyyyy2)
  77.         dxxxx = dxxxx * 1.75
  78.         dyyyy = dyyyy * 2
  79.     END IF
  80.  
  81.     _DISPLAY
  82.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGB32(0, 0, 0, 5), BF
  83.  
  84. 'from Steve Gold standard
  85. SUB fillCircle (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  86.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  87.     DIM X AS INTEGER, Y AS INTEGER
  88.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  89.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  90.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  91.     WHILE X > Y
  92.         RadiusError = RadiusError + Y * 2 + 1
  93.         IF RadiusError >= 0 THEN
  94.             IF X <> Y + 1 THEN
  95.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  96.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  97.             END IF
  98.             X = X - 1
  99.             RadiusError = RadiusError - X * 2
  100.         END IF
  101.         Y = Y + 1
  102.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  103.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  104.     WEND
  105.  
« Last Edit: December 06, 2020, 01:28:01 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Deflection Balls
« Reply #5 on: December 06, 2020, 01:38:49 pm »
You had sticky balls?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Deflection Balls
« Reply #6 on: December 06, 2020, 02:02:15 pm »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Deflection Balls
« Reply #7 on: December 06, 2020, 02:55:16 pm »
I tried and tried, but I can never get away from the jokes. LOL

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Deflection Balls
« Reply #8 on: December 06, 2020, 02:59:09 pm »
Well I try and try to do it with friends, I get in big trouble with strangers.

See now that might be an opening for a joke ;)