Author Topic: 2D ball collisions without trigonometry.  (Read 8933 times)

0 Members and 1 Guest are viewing this topic.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
2D ball collisions without trigonometry.
« on: May 03, 2021, 11:36:08 pm »
Looking at vector addition videos example https://www.khanacademy.org/math/precalculus/x9e81a4f98389efdf:vectors/x9e81a4f98389efdf:vec-add-mag-dir/v/mag-dir-vec-sums

Say 2 objects, with no dimensions, collide at origin 0,0  (heading towards 0, 0)    This is the simple case for 2D collisions.  It is simple because the objects have no dimension and the collision is at 0, 0 .  The vector sum of these 2 objects, gives a resultant vector  (call it the mvector ) that is heading away from 0, 0.   mvector  exists momentarily  since energy is stored if the collision is elastic. For a moment, both objects head in the direction of mVector only for a small distance.

As the objects rebound from the stored energy, mVector determines the resultant direction and velocity of both objects.

The hard part is to calculate the mvector with objects like balls because the angle of collision wont always be parallel to the x axis of the screen

That requires some kind of vector rotation. I'm still working on that.

Any ideas how to do that?



« Last Edit: May 04, 2021, 12:03:06 am by NOVARSEG »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
Re: 2D ball collisions without trigonometry.
« Reply #1 on: May 04, 2021, 11:54:52 pm »
I tried my hand at an abortive attempt at a billiards program without resorting to trig. It had some "issues", and the code is a hot mess, but I imagine a good non-trig way is to obtain a normalized; aka unit, vector of the ball being processed, then use dot product between the balls direction and the strike angle to determine how much of a balls magnitude / speed/ force to apply to another ball. Of course, such a system is not dimensionless, colliding at 0,0

https://www.qb64.org/forum/index.php?topic=2382.msg116030#msg116030

  [ You are not allowed to view this attachment ]  

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: 2D ball collisions without trigonometry.
« Reply #2 on: May 05, 2021, 12:36:14 am »
That looks useful.

I've got a program that shows two balls colliding where the strike angle is in the same direction as the cyan colored ball.  I might remove the idea of "dimensionless"  for now because it is an abstract idea.  There is a idea  of "moments" involved because the mvector exits only for a moment.


The resultant direction of the cyan ball should maintain the same direction. This should give a clue on where the mvector is and how to calculate it as a function of strike angle and direction /velocity  of both balls.

run the program and in the top left corner you will see 56 56 that is the  x, y of the strike angle in pixels.  The magnitude is 80 pixels since the ball radius is 40 pixels

If anyone has a pool table at home what happens to the cyan ball does it stop maybe?

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3. DIM originX AS INTEGER
  4. originX = _WIDTH / 2
  5.  
  6. DIM originY AS INTEGER
  7. originY = _HEIGHT / 2
  8. DIM X(1) AS INTEGER 'can't have a fraction of a pixel
  9. DIM Y(1) AS INTEGER 'can't have a fraction of a pixel
  10. DIM DirY(1) AS INTEGER
  11. DIM DirX(1) AS INTEGER
  12.  
  13.  
  14. Y(0) = 672
  15. X(0) = originX - (700 - originY)
  16.  
  17. X(1) = _WIDTH - 40 'originX + (366 - originY)
  18. Y(1) = _HEIGHT - 40 '350
  19.  
  20. DirX(0) = 1
  21. DirY(0) = -1
  22.  
  23. DirX(1) = -1
  24. DirY(1) = -1
  25.  
  26.  
  27.     _DELAY .003
  28.     obj = 0
  29.  
  30.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 <= 80 AND tog = 0 THEN
  31.         tog = 1
  32.  
  33.         PRINT X(0) - X(1); " "; Y(1) - Y(0)
  34.         ' PRINT ABS(3200 - (30 ^ 2)) ^ .5
  35.         'PRINT DirX(0) * ABS(3200 + 3200) ^ .5
  36.         'PRINT DirY(0) * ABS(3200 - 3200) ^ .5
  37.  
  38.         PRINT DirX(0) * ABS(3136 + (X(1) - X(0)) ^ 2) ^ .5
  39.         PRINT DirY(0) * ABS(3136 - (Y(1) - Y(0)) ^ 2) ^ .5
  40.  
  41.  
  42.  
  43.  
  44.  
  45.         DO
  46.             IF INKEY$ <> "" THEN EXIT DO
  47.         LOOP
  48.  
  49.         '_DELAY .6
  50.     END IF
  51.  
  52.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 > 80 AND tog = 1 THEN tog = 0
  53.  
  54.     r = 0: g = 0: B = 0
  55.     FOR S = 1 TO 40
  56.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  57.     NEXT
  58.  
  59.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  60.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  61.     X(obj) = X(obj) + DirX(obj)
  62.  
  63.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  64.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  65.     Y(obj) = Y(obj) + DirY(obj)
  66.  
  67.  
  68.     r = 255: g = 0: B = 0
  69.     FOR S = 1 TO 40
  70.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  71.     NEXT
  72.  
  73.     obj = 1
  74.  
  75.     r = 0: g = 0: B = 0
  76.     FOR S = 1 TO 40
  77.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  78.     NEXT
  79.  
  80.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  81.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  82.     X(obj) = X(obj) + DirX(obj)
  83.  
  84.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  85.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  86.     Y(obj) = Y(obj) + DirY(obj)
  87.  
  88.     r = 0: g = 255: B = 255
  89.     FOR S = 1 TO 40
  90.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  91.     NEXT
  92.  
  93.     ' _DISPLAY
  94.  
  95.  
  96.  
  97.  
  98.  




« Last Edit: May 05, 2021, 12:59:26 am by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: 2D ball collisions without trigonometry.
« Reply #3 on: May 05, 2021, 02:58:08 am »
Different ball collision  angles. How is mvector related to collision angle?

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3. DIM originX AS INTEGER
  4. originX = _WIDTH / 2
  5.  
  6. DIM originY AS INTEGER
  7. originY = _HEIGHT / 2
  8. DIM X(1) AS INTEGER 'can't have a fraction of a pixel
  9. DIM Y(1) AS INTEGER 'can't have a fraction of a pixel
  10. DIM DirY(1) AS INTEGER
  11. DIM DirX(1) AS INTEGER
  12.  
  13. LL1:
  14.  
  15. IF f1 = 2 THEN f1 = 0
  16.  
  17. IF f1 = 1 THEN
  18.     f1 = 2
  19.     Y(0) = 672
  20.     X(0) = originX - (700 - originY)
  21.  
  22. IF f1 = 0 THEN
  23.     f1 = 1
  24.     Y(0) = 648
  25.     X(0) = originX - (700 - originY)
  26.  
  27. X(1) = _WIDTH - 40
  28. Y(1) = _HEIGHT - 40
  29.  
  30. DirX(0) = 1
  31. DirY(0) = -1
  32.  
  33. DirX(1) = -1
  34. DirY(1) = -1
  35.  
  36.     _DELAY .003
  37.     obj = 0
  38.  
  39.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 <= 80 AND tog = 0 THEN
  40.         tog = 1
  41.  
  42.         LOCATE 2, 1: PRINT X(0) - X(1); " X"
  43.  
  44.         LOCATE 3, 1: PRINT Y(1) - Y(0); " Y"
  45.         ' PRINT ABS(3200 - (30 ^ 2)) ^ .5
  46.         'PRINT DirX(0) * ABS(3200 + 3200) ^ .5
  47.         'PRINT DirY(0) * ABS(3200 - 3200) ^ .5
  48.  
  49.         '' LOCATE 4, 1: PRINT DirX(0) * ABS(3136 + (X(1) - X(0)) ^ 2) ^ .5
  50.         ' LOCATE 5, 1: PRINT DirY(0) * ABS(3136 - (Y(1) - Y(0)) ^ 2) ^ .5
  51.  
  52.         _DELAY 2
  53.         obj = 0
  54.         r = 0: g = 0: B = 0
  55.         FOR S = 1 TO 40
  56.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  57.         NEXT
  58.  
  59.         obj = 1
  60.         r = 0: g = 0: B = 0
  61.         FOR S = 1 TO 40
  62.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  63.         NEXT
  64.  
  65.         GOTO LL1
  66.  
  67.     END IF
  68.  
  69.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 > 80 AND tog = 1 THEN tog = 0
  70.  
  71.     r = 0: g = 0: B = 0
  72.     FOR S = 1 TO 40
  73.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  74.     NEXT
  75.  
  76.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  77.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  78.     X(obj) = X(obj) + DirX(obj)
  79.  
  80.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  81.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  82.     Y(obj) = Y(obj) + DirY(obj)
  83.  
  84.  
  85.     r = 255: g = 0: B = 0
  86.     FOR S = 1 TO 40
  87.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  88.     NEXT
  89.  
  90.     obj = 1
  91.  
  92.     r = 0: g = 0: B = 0
  93.     FOR S = 1 TO 40
  94.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  95.     NEXT
  96.  
  97.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  98.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  99.     X(obj) = X(obj) + DirX(obj)
  100.  
  101.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  102.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  103.     Y(obj) = Y(obj) + DirY(obj)
  104.  
  105.     r = 0: g = 255: B = 255
  106.     FOR S = 1 TO 40
  107.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  108.     NEXT
  109.  
  110.  


Another angle added

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3. DIM originX AS INTEGER
  4. originX = _WIDTH / 2
  5.  
  6. DIM originY AS INTEGER
  7. originY = _HEIGHT / 2
  8. DIM X(1) AS INTEGER 'can't have a fraction of a pixel
  9. DIM Y(1) AS INTEGER 'can't have a fraction of a pixel
  10. DIM DirY(1) AS INTEGER
  11. DIM DirX(1) AS INTEGER
  12.  
  13. LL1:
  14.  
  15. IF f1 = 3 THEN f1 = 0
  16.  
  17. IF f1 = 2 THEN
  18.     f1 = 3
  19.     Y(0) = 728
  20.     X(0) = originX - (700 - originY)
  21.  
  22. 'GOTO LL2
  23.  
  24.  
  25. IF f1 = 1 THEN
  26.     f1 = 2
  27.     Y(0) = 672
  28.     X(0) = originX - (700 - originY)
  29.  
  30. IF f1 = 0 THEN
  31.     f1 = 1
  32.     Y(0) = 648
  33.     X(0) = originX - (700 - originY)
  34. LL2:
  35.  
  36. X(1) = _WIDTH - 40
  37. Y(1) = _HEIGHT - 40
  38.  
  39. DirX(0) = 1
  40. DirY(0) = -1
  41.  
  42. DirX(1) = -1
  43. DirY(1) = -1
  44.  
  45.     _DELAY .003
  46.     obj = 0
  47.  
  48.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 <= 80 AND tog = 0 THEN
  49.         tog = 1
  50.  
  51.         LOCATE 2, 1: PRINT X(0) - X(1); " X"
  52.  
  53.         LOCATE 3, 1: PRINT Y(1) - Y(0); " Y"
  54.         ' PRINT ABS(3200 - (30 ^ 2)) ^ .5
  55.         'PRINT DirX(0) * ABS(3200 + 3200) ^ .5
  56.         'PRINT DirY(0) * ABS(3200 - 3200) ^ .5
  57.  
  58.         '' LOCATE 4, 1: PRINT DirX(0) * ABS(3136 + (X(1) - X(0)) ^ 2) ^ .5
  59.         ' LOCATE 5, 1: PRINT DirY(0) * ABS(3136 - (Y(1) - Y(0)) ^ 2) ^ .5
  60.  
  61.         _DELAY 2
  62.         obj = 0
  63.         r = 0: g = 0: B = 0
  64.         FOR S = 1 TO 40
  65.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  66.         NEXT
  67.  
  68.         obj = 1
  69.         r = 0: g = 0: B = 0
  70.         FOR S = 1 TO 40
  71.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  72.         NEXT
  73.  
  74.         GOTO LL1
  75.  
  76.     END IF
  77.  
  78.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 > 80 AND tog = 1 THEN tog = 0
  79.  
  80.     r = 0: g = 0: B = 0
  81.     FOR S = 1 TO 40
  82.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  83.     NEXT
  84.  
  85.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  86.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  87.     X(obj) = X(obj) + DirX(obj)
  88.  
  89.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  90.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  91.     Y(obj) = Y(obj) + DirY(obj)
  92.  
  93.  
  94.     r = 255: g = 0: B = 0
  95.     FOR S = 1 TO 40
  96.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  97.     NEXT
  98.  
  99.     obj = 1
  100.  
  101.     r = 0: g = 0: B = 0
  102.     FOR S = 1 TO 40
  103.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  104.     NEXT
  105.  
  106.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  107.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  108.     X(obj) = X(obj) + DirX(obj)
  109.  
  110.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  111.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  112.     Y(obj) = Y(obj) + DirY(obj)
  113.  
  114.     r = 0: g = 255: B = 255
  115.     FOR S = 1 TO 40
  116.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  117.     NEXT
  118.  
  119.  



« Last Edit: May 05, 2021, 03:15:04 am by NOVARSEG »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
Re: 2D ball collisions without trigonometry.
« Reply #4 on: May 05, 2021, 06:59:02 am »
referring to the picture I attached, it is showing an exit angle for the striking ball that is orthogonal to the "strike axis" (i.e. the line between the centers of the two balls. The stationary ball moves straight away along that strike axis, mainly because it is not in motion initially. It gets a lot more complex when both balls are moving. At that point I presume that it becomes a function of adding the new (orthogonal) movement vector to the old pre-strike movement vector and modifying the magnitudes of the results according to the dot product result between strike axis and incoming vector. Thereby each ball imparts some of its energy to the other. I was not entirely successful in implementation in my example, as the balls would rather bizarrely swap places with each other or get stuck together on the same spot.

I opted for a Dot Product of unit vector function as it is a relatively straightforward computation and handled all angles reasonably, including head on strikes that would stop a cue ball in its tracks and send the struck ball on its way. Otherwise, I assume that the real world physics are quite beyond my pay grade.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: 2D ball collisions without trigonometry.
« Reply #5 on: May 08, 2021, 03:21:43 am »
Vector addition seems to work for simple cases.  I'm running tests on 2 balls with equal mass and velocity using vector addition  and vector rotation.

There are problems with unequal velocities such as when one ball is stationary and the other moving.  Does the stationary ball have a vector?.   So it looks like vector addition is only part of the solution.

'First case: starting vectors
red ball is traveling sqrt(8)x, 0y    towards 0, 0
cyan ball is traveling 2x , -2y        towards 0, 0

let strike angle = 0 (as respects x axis)

At what strike angle do these balls mathematically collide? The vector addition of the ball vectors implies that the bisection angle between the ball vectors is the position of the resultant vector. (mvector)

vector addition gives    sqrt(8) - 2 X  , 2 Y  (mvector)


0 X  , 2.164784 Y   (mvector)

OK  final directions and velocities of both balls.

red ball
sqrt(8)x, 0y   +   0 X  , 2.164784 Y  =  sqrt(8) x ,  2.164784 Y

cyan ball 2x , -2y   +  0 X  , 2.164784 Y =  2x ,  .167484y

Check the math.

According to the literature, the total momentum is preserved. Since the masses are the same then total velocities will do.   The total velocities are red + cyan ball velocity = (8^.5 ) *2

PRINT (4 + .16748 ^ 2) ^ .5 + (8 + 2.16474 ^ 2) ^ .5

= (8^.5 ) *2

Ok the math works out but more tests needed.
**************************
 the above math had some problems
« Last Edit: May 09, 2021, 01:43:24 am by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: 2D ball collisions without trigonometry.
« Reply #6 on: May 09, 2021, 01:29:34 am »
2 ball collision.    Both balls equal mass and velocity.

The red ball moves along X axis and the cyan ball moves at a 45 degrees angle.

press any key to run program again.  There are lines that show final direction and velocity.  Not sure if the code is correct.  Does this look OK?

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3. DIM DirY(1) AS INTEGER
  4. DIM DirX(1) AS INTEGER
  5.  
  6. LL1: CLS
  7. Y(0) = _HEIGHT / 2
  8. X(0) = 60
  9.  
  10. X(1) = _WIDTH - 196
  11. Y(1) = _HEIGHT - 40
  12.  
  13. DirX(0) = 1
  14. DirY(0) = -1
  15.  
  16. DirX(1) = -1
  17. DirY(1) = -1
  18.  
  19.     _DELAY .003
  20.     obj = 0
  21.  
  22.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 <= 80 AND tog = 0 THEN
  23.         tog = 1
  24.  
  25.         LOCATE 2, 1: PRINT X(0) - X(1); " X"
  26.  
  27.         LOCATE 3, 1: PRINT Y(1) - Y(0); " Y"
  28.         LOCATE 4, 1: PRINT X(0); Y(0)
  29.  
  30.         rx = -80
  31.         ry = 0
  32.  
  33.         ' rx = 0
  34.         ' ry = -80
  35.  
  36.         ' rx = -(3200 ^ .5) ' * 2
  37.         ' ry = (3200 ^ .5) ' / 2
  38.  
  39.         cx = 3200 ^ .5 '* 2
  40.         cy = -(3200 ^ .5) ' / 2
  41.  
  42.         mvectorx = (0 - rx) - cx
  43.         mvectory = (0 - ry) + (0 - cy)
  44.         PRINT "mvectorx"; mvectorx
  45.         PRINT "mvectory"; mvectory
  46.  
  47.         'rotates mvector so it is at right angles to X axis
  48.         'mvectory = (mvectorx ^ 2 + mvectory ^ 2) ^ .5
  49.         ' mvectorx = 0
  50.  
  51.         rxf = rx + mvectorx
  52.         ryf = ry + mvectory
  53.         cxf = cx + mvectorx
  54.         cyf = cy + mvectory
  55.  
  56.         PRINT "rxf"; rxf
  57.         PRINT "ryf"; ryf
  58.         PRINT "cxf"; cxf
  59.         PRINT "cyf"; cyf
  60.  
  61.         rxf = rxf * 3
  62.         ryf = ryf * 3
  63.         cxf = cxf * 3
  64.         cyf = cyf * 3
  65.  
  66.         LINE (X(0), Y(0))-(X(0) + rxf, Y(0) - ryf)
  67.         LINE (X(1), Y(1))-(X(1) + cxf, Y(1) - cyf)
  68.  
  69.         DO
  70.             IF INKEY$ <> "" THEN EXIT DO
  71.         LOOP
  72.  
  73.         obj = 0
  74.         r = 0: g = 0: B = 0
  75.         FOR S = 1 TO 40
  76.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  77.         NEXT
  78.  
  79.         obj = 1
  80.         r = 0: g = 0: B = 0
  81.         FOR S = 1 TO 40
  82.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  83.         NEXT
  84.  
  85.         GOTO LL1
  86.  
  87.     END IF
  88.  
  89.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 > 80 AND tog = 1 THEN tog = 0
  90.  
  91.     r = 0: g = 0: B = 0
  92.     FOR S = 1 TO 40
  93.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  94.     NEXT
  95.  
  96.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  97.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  98.     X(obj) = X(obj) + DirX(obj)
  99.  
  100.     ' IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  101.     ' IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  102.     ' Y(obj) = Y(obj) + DirY(obj)
  103.  
  104.  
  105.     r = 255: g = 0: B = 0
  106.     FOR S = 1 TO 40
  107.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  108.     NEXT
  109.  
  110.     obj = 1
  111.  
  112.     r = 0: g = 0: B = 0
  113.     FOR S = 1 TO 40
  114.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  115.     NEXT
  116.  
  117.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  118.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  119.     X(obj) = X(obj) + DirX(obj)
  120.  
  121.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  122.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  123.     Y(obj) = Y(obj) + DirY(obj)
  124.  
  125.     r = 0: g = 255: B = 255
  126.     FOR S = 1 TO 40
  127.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  128.     NEXT
  129.  
  130.  
« Last Edit: May 09, 2021, 02:01:47 am by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: 2D ball collisions without trigonometry.
« Reply #7 on: May 09, 2021, 11:48:41 pm »
Ball collision code -very simple.  Vector addition only.

Vector rotation not needed in this case.  Have not verified if the collisions are correct but it looks fairly reasonable.

WAIT BUG DETECTED !!!!!!!!!!!!!!!!!!!!!!!!!

« Last Edit: May 09, 2021, 11:51:16 pm by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: 2D ball collisions without trigonometry.
« Reply #8 on: May 10, 2021, 03:41:10 am »
Nasty bug.  Ball collisions using vector addition only.  Equal velocity and mass.

It looks like the final velocities are unchanged if the original velocities of both balls are the same.

I find it interesting that 99.99 % of internet info about ball collisions is about a moving ball colliding with a stationary ball or wall.  Almost no mention about balls colliding with equal velocity.

I still do not know if the vector addition can describe ball collisions.  Can anyone back this up?


Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3. DIM DirY(1) AS INTEGER
  4. DIM DirX(1) AS INTEGER
  5.  
  6. L = 2
  7.  
  8.  
  9. Y(0) = _HEIGHT / 2
  10. X(0) = 60
  11.  
  12. X(1) = _WIDTH - 196
  13. Y(1) = _HEIGHT - 40
  14.  
  15. DirX(0) = 1
  16. DirY(0) = 0 '-1
  17.  
  18. DirX(1) = -1
  19. DirY(1) = -1
  20. rx = -80
  21. ry = 0
  22. cx = 3200 ^ .5
  23. cy = -(3200 ^ .5)
  24.  
  25.     _DELAY .0015
  26.  
  27.  
  28.     IF (((X(1) - X(0)) ^ 2) + (Y(0) - Y(1)) ^ 2) ^ .5 <= 80 THEN
  29.  
  30.  
  31.         LOCATE 10 + L, 1: PRINT X(0) - X(1); " X"
  32.  
  33.         LOCATE 11 + L, 1: PRINT Y(1) - Y(0); " Y"
  34.  
  35.         mvectorx = (0 - rx) + (0 - cx)
  36.         mvectory = (0 - ry) + (0 - cy)
  37.         LOCATE 12 + L, 1: PRINT "mvectorx"; mvectorx
  38.         LOCATE 13 + L, 1: PRINT "mvectory"; mvectory
  39.  
  40.         rxf = rx + mvectorx
  41.         ryf = ry + mvectory
  42.         cxf = cx + mvectorx
  43.         cyf = cy + mvectory
  44.         rx = rxf
  45.         ry = -ryf
  46.         cx = cxf
  47.         cy = -cyf
  48.  
  49.         LOCATE 14 + L, 1: PRINT "rxf"; rxf; "         "
  50.         LOCATE 15 + L, 1: PRINT "ryf"; ryf; "         "
  51.         LOCATE 16 + L, 1: PRINT "cxf"; cxf; "         "
  52.         LOCATE 17 + L, 1: PRINT "cyf"; cyf; "         "
  53.  
  54.  
  55.         DirX(0) = rxf / 80
  56.         DirY(0) = ryf / -80
  57.         DirX(1) = cxf / 80
  58.         DirY(1) = cyf / -80
  59.  
  60.         LINE (X(0), Y(0))-(X(0) + rxf * 3, Y(0) - ryf * 3)
  61.         LINE (X(1), Y(1))-(X(1) + cxf * 3, Y(1) - cyf * 3)
  62.  
  63.         _DELAY .5
  64.  
  65.         obj = 0
  66.         r = 0: g = 0: B = 0
  67.         FOR S = 1 TO 40
  68.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  69.         NEXT
  70.  
  71.         obj = 1
  72.         r = 0: g = 0: B = 0
  73.         FOR S = 1 TO 40
  74.             CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  75.         NEXT
  76.  
  77.     END IF
  78.  
  79.     obj = 0
  80.  
  81.     r = 0: g = 0: B = 0
  82.     FOR S = 1 TO 40
  83.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  84.     NEXT
  85.  
  86.     IF X(obj) + 40 = _WIDTH THEN DirX(obj) = -1
  87.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  88.  
  89.     X(obj) = X(obj) + DirX(obj)
  90.  
  91.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  92.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  93.  
  94.     Y(obj) = Y(obj) + DirY(obj)
  95.  
  96.  
  97.     r = 255: g = 0: B = 0
  98.     FOR S = 1 TO 40
  99.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  100.     NEXT
  101.  
  102.     obj = 1
  103.  
  104.     r = 0: g = 0: B = 0
  105.     FOR S = 1 TO 40
  106.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  107.     NEXT
  108.  
  109.     IF X(obj) + 40 >= _WIDTH THEN DirX(obj) = -1
  110.     IF X(obj) - 40 = 0 THEN DirX(obj) = 1
  111.     X(obj) = X(obj) + DirX(obj)
  112.  
  113.     IF Y(obj) + 40 = _HEIGHT THEN DirY(obj) = -1
  114.     IF Y(obj) - 40 = 0 THEN DirY(obj) = 1
  115.     Y(obj) = Y(obj) + DirY(obj)
  116.  
  117.     r = 0: g = 255: B = 255
  118.     FOR S = 1 TO 40
  119.         CIRCLE (X(obj), Y(obj)), S, _RGB(r, g, B)
  120.     NEXT
  121.  
  122.  

Ok I've been searching the net and there is no examples of 2 ball collisions at the same velocity.  I have no way to verify my results.  Plenty (thousands) of examples of cue balls hitting stationary balls though -sigh
« Last Edit: May 10, 2021, 05:54:18 pm by NOVARSEG »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: 2D ball collisions without trigonometry.
« Reply #10 on: May 11, 2021, 05:11:33 pm »
Oh 2 balls same mass and velocity collide dead onto each other is supposed to knock all energy out of each other come to dead stop, right? (Been considering sum of Forces acting on object.)

I think I've been just trading momentums in such circumstance, having them bounce off each other like a off a wall.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
Re: 2D ball collisions without trigonometry.
« Reply #11 on: May 11, 2021, 08:48:50 pm »
Trying to get my head around this one.... Same mass. Same Velocity. Assuming they are colliding from directly opposite vectors (head on). The collision would be like hitting a fixed wall. Assuming that both objects can survive the re-percussive force of the collision then both objects should rebound in the opposite direction to their initial vectors. Some energy should be lost and as such reduce the velocities of each object. Of course there are other external influences which have been disregarded... There are a few ways that both objects could come to a dead stop-ish... If both object were not totally solid like steel. Say... plaster. Both objects should shatter. All energy either dissipated or transferred to the fragments. If one object was made of solid copper and the other a mono-pole magnet then both objects would not collide. The magnetic field would be induced into the copper object creating another magnetic field which would repel the magnet. More so as both objects near collision. Both fields opposing the velocities of both objects to the point of reducing momentum to a point of equilibrium.

Or something like that....

Therefore, excluding other external forces, the two spheres should just bounce of each other....
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: 2D ball collisions without trigonometry.
« Reply #12 on: May 11, 2021, 09:22:06 pm »
For simplicity sake no other forces, no gravity, no magnetism, no friction, no desire for coffee...

From the paper of OldMoses link https://www.vobarian.com/collisions/2dcollisions2.pdf
v1' = (v1(m1-m2) + 2m2v2)/(m1 + m2)
v2' = (v2(m2-m1) + 2m1v1)/(m1 + m2)

assume for simplicity m1 = m2 = 1 then

v1' = (v1(1-1) + 2v2)/2 = v2 
v2' = (v2(0) + 2v1)/(2) = v1

Ha! they do trade momentums therefore no dead stop which sum of vector forces suggests.
« Last Edit: May 11, 2021, 09:36:19 pm by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
Re: 2D ball collisions without trigonometry.
« Reply #13 on: May 11, 2021, 09:28:17 pm »
WHAT!?!?  No coffee?  Heresy!!!
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: 2D ball collisions without trigonometry.
« Reply #14 on: May 11, 2021, 09:41:45 pm »
WHAT!?!?  No coffee?  Heresy!!!

I am pretty sure I said no desire for coffee, coffee still exists at least until it gets cyber attacked.