Author Topic: Math help with puck/paddle bounce  (Read 6554 times)

0 Members and 1 Guest are viewing this topic.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Math help with puck/paddle bounce
« on: June 04, 2020, 02:12:54 pm »
Calling all math wizards. I have no idea why this problem is eluding me but I just cant seem to create the math needed for this problem I have.

A rectangular paddle of known size (rw x rh) moves only in a vertical direction.

When a ball hits the surface it needs to deflect at the tangent point of an arc instead of a flat surface.

I drew the diagram below when I set out to create the math to help as a visual aid.

Would anyone care to help with the math needed to accomplish this? I would very much appreciate it.
In order to understand recursion, one must first understand recursion.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #1 on: June 04, 2020, 02:55:13 pm »
You also know the speed of the paddle, right?

I think it matters.
It works better if you plug it in.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #2 on: June 04, 2020, 03:02:17 pm »
Yes, I do, but I don't plan on adding any spin or "english" to the ball using the Y velocity of the paddle.
In order to understand recursion, one must first understand recursion.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #3 on: June 04, 2020, 03:22:39 pm »
Vince and I solved this like 2 months ago search angle pong.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #4 on: June 04, 2020, 07:02:32 pm »
Vince and I solved this like 2 months ago search angle pong.

You mean this? That I solved. https://www.qb64.org/forum/index.php?topic=2309.msg115507#msg115507

Terry just wants a vertical paddle like used in pong I think. That is much simpler collision because you can treat the paddle like a moving wall (vertical or horizontal).

Terry do you have a pong game? I can probably dig one up.

If the ball is moving dx and dy then just reverse the dx, eg dx = -dx also you can put the ball x, y tangent to the paddle (touching the paddle) so ball looks as if it hit paddle but does not sink through it, nor starts it's reverse course too early (before touching the paddle).
« Last Edit: June 04, 2020, 07:11:09 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #5 on: June 04, 2020, 07:52:40 pm »
Apologies for forgetting your role in that bplus, was on the highway when I typed out my response but I wanted to give something to Terry rather than make everyone wait.

Anyway yes, the issue of collision detection and velocity reflection has been well-studied by a few people. If you want proper momentum exchange for arbitrary objects, you'll need Collisions.bas.
« Last Edit: June 04, 2020, 07:54:43 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #6 on: June 04, 2020, 08:04:30 pm »
You mean this? That I solved. https://www.qb64.org/forum/index.php?topic=2309.msg115507#msg115507

Terry just wants a vertical paddle like used in pong I think. That is much simpler collision because you can treat the paddle like a moving wall (vertical or horizontal).

Terry do you have a pong game? I can probably dig one up.

If the ball is moving dx and dy then just reverse the dx, eg dx = -dx also you can put the ball x, y tangent to the paddle (touching the paddle) so ball looks as if it hit paddle but does not sink through it, nor starts it's reverse course too early (before touching the paddle).

That's what I created for Task 22, A pong game that will be built step by step to show simple game design. I have it complete except for the Yvector() function returning the correct figure. Right now the puck just bounces back and forth. (see below) This will get called in the MovePuck() subroutine.

Code: QB64: [Select]
  1.  
  2. '-------------------------------------------------------------------------------------------------------------
  3. '** Declare constants
  4. '-------------------------------------------------------------------------------------------------------------
  5.  
  6. CONST FALSE = 0, TRUE = NOT FALSE '               truth detectors
  7. CONST BRIGHTWHITE = _RGB32(255, 255, 255) '       define colors
  8. CONST WHITE = _RGB32(192, 192, 192)
  9. CONST GRAY = _RGB32(128, 128, 128) '
  10. CONST SCREENWIDTH = 768 '                         screen width
  11. CONST SCREENHEIGHT = 576 '                        screen height
  12. CONST PUCKSIZE = 10 '                             puck square size
  13. CONST PADDLEWIDTH = 10 '                          paddle width
  14. CONST PADDLEHEIGHT = 60 '                         paddle height
  15. CONST PADDLESPEED = 2 '                           paddle Y speed
  16. CONST PADDLEOFFSET = 20 '                         paddle offset from side of screen
  17. CONST P1MISSED = PUCKSIZE \ 2 '                   puck on left side edge of screen
  18. CONST P2MISSED = SCREENWIDTH - 1 - PUCKSIZE \ 2 ' puck on right side edge of screen
  19. CONST PADDLEYMIN = PADDLEHEIGHT \ 2 + 30 '                                     min Y coordinate for paddles
  20. CONST PADDLEYMAX = SCREENHEIGHT - 30 - PADDLEHEIGHT \ 2 '                      max Y coordinate for paddles
  21. CONST PUCKXMIN = PADDLEOFFSET + PADDLEWIDTH + PUCKSIZE \ 2 '                   min X coordinate for puck
  22. CONST PUCKXMAX = SCREENWIDTH - 1 - PADDLEOFFSET - PADDLEWIDTH - PUCKSIZE \ 2 ' max X coordinate for puck
  23. CONST PUCKYMIN = 30 + PUCKSIZE \ 2 '                                           min Y coordinate for puck
  24. CONST PUCKYMAX = SCREENHEIGHT - 30 - PUCKSIZE \ 2 '                            max Y coordinate for puck
  25. CONST FPS = 300 '                                                              game frames per second
  26.  
  27. '-------------------------------------------------------------------------------------------------------------
  28. '** Declare TYPE definitions
  29. '-------------------------------------------------------------------------------------------------------------
  30.  
  31. TYPE RECTTYPE '        rectangle definition for collision detection
  32.     x1 AS INTEGER '    upper left X coordinate
  33.     y1 AS INTEGER '    upper left Y coordinate
  34.     x2 AS INTEGER '    lower right X coordinate
  35.     y2 AS INTEGER '    lower right Y coordinate
  36.  
  37. TYPE MOVINGOBJECT '    moving object definition
  38.     x AS SINGLE '      center X coordinate
  39.     y AS SINGLE '      center Y coordinate
  40.     xv AS SINGLE '     X vector component
  41.     yv AS SINGLE '     Y vector component
  42.     rect AS RECTTYPE ' rectangle coordinates for collision detection
  43.  
  44. '-------------------------------------------------------------------------------------------------------------
  45. '** Declare global variables
  46. '-------------------------------------------------------------------------------------------------------------
  47.  
  48. DIM PlayField AS LONG '        playing field image
  49. DIM PlayFieldCopy AS LONG '    playing field image copy (without scores)
  50. DIM P1Paddle AS MOVINGOBJECT ' player 1 paddle
  51. DIM P2Paddle AS MOVINGOBJECT ' player 2 paddle
  52. DIM Puck AS MOVINGOBJECT '     puck properties
  53. DIM Computer AS INTEGER '      TRUE if computer playing
  54. DIM Score(9) AS STRING * 15 '  encoded font data
  55. DIM Pscore(1) AS INTEGER '     player scores (0=player1, 1=player2
  56. DIM Missed AS INTEGER '        TRUE when a player misses the puck
  57. DIM GameOver AS INTEGER '      TRUE when a player reaches a score of 9
  58.  
  59. '-------------------------------------------------------------------------------------------------------------
  60. '** Begin main program
  61. '-------------------------------------------------------------------------------------------------------------
  62.  
  63. SCREEN _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32) ' enter graphics screen
  64. _TITLE "QB64 PONG!" '                             give window a title
  65. _DELAY .5 '                                       slight delay
  66. _SCREENMOVE _MIDDLE '                             move window to center of desktop
  67. Initialize '                                      initialize game settings
  68. DO '                                              begin main program loop
  69.     NewGame '                                     start a new game
  70.     DO '                                          begin game loop
  71.         PlayGame '                                play the game
  72.     LOOP UNTIL GameOver OR _KEYDOWN(27) '         leave when game over or player exits
  73. LOOP UNTIL _KEYDOWN(27) '                         leave when player exits
  74. Cleanup '                                         clean player's computer memory and exit to operating system
  75.  
  76. '-------------------------------------------------------------------------------------------------------------
  77. '** end main program
  78. '-------------------------------------------------------------------------------------------------------------
  79.  
  80. '-------------------------------------------------------------------------------------------------------------
  81. '** Declare subroutines and functions
  82. '-------------------------------------------------------------------------------------------------------------
  83.  
  84. '-------------------------------------------------------------------------------------------------------------
  85. SUB ResetRound ()
  86.     '---------------------------------------------------------------------------------------------------------
  87.  
  88.     SHARED Missed AS INTEGER '    need access to puck miss indicator
  89.     SHARED Puck AS MOVINGOBJECT ' need access to puck
  90.  
  91.     IF SGN(Puck.xv) = 1 THEN '                          puck was heading right?
  92.         Puck.x = SCREENWIDTH * .25 '                    yes, serve from player 1 side
  93.         Puck.xv = .5 '                                  puck will travel right
  94.     ELSE '                                              no, puck was travleing left
  95.         Puck.x = SCREENWIDTH * .75 '                    serve from player 2 side
  96.         Puck.xv = -.5 '                                 puck will travel left
  97.     END IF
  98.     Puck.y = INT(RND(1) * (SCREENHEIGHT - 100)) + 50 '  random puck Y coordinate
  99.     Puck.yv = (RND(1) - RND(1)) / 2 '                   random puck Y vector component
  100.     Missed = FALSE '                                    reset puck missed indicator
  101.  
  102.  
  103. '-------------------------------------------------------------------------------------------------------------
  104. SUB PlayGame ()
  105.     '---------------------------------------------------------------------------------------------------------
  106.  
  107.     SHARED PlayField AS LONG '    need access to playing field image
  108.     SHARED Missed AS INTEGER '    need access to puck miss indicator
  109.     SHARED Puck AS MOVINGOBJECT ' need access to puck
  110.     SHARED Pscore() AS INTEGER '  need access to player scores
  111.     SHARED GameOver AS INTEGER '  need access to game over indicator
  112.  
  113.     ResetRound '                                             set up next game round
  114.     DO '                                                     begin round loop
  115.         _LIMIT FPS '                                         limit game speed
  116.         _PUTIMAGE , PlayField '                              reset playing field
  117.         MovePaddles '                                        update player paddles
  118.         MovePuck '                                           update puck
  119.         _DISPLAY '                                           update screen with changes
  120.     LOOP UNTIL Missed OR _KEYDOWN(27) '                      leave when puck missed or a player presses ESC
  121.     IF SGN(Puck.xv) = -1 THEN '                              was puck heading left?
  122.         Pscore(1) = Pscore(1) + 1 '                          yes, increase player 2 score
  123.     ELSE '                                                   no, puck was heading right
  124.         Pscore(0) = Pscore(0) + 1 '                          increase player 1 score
  125.     END IF
  126.     IF Pscore(0) = 9 OR Pscore(1) = 9 THEN GameOver = TRUE ' set game over indicator when player reaches 9
  127.     UpdateScores '                                           update player scores
  128.  
  129.  
  130. '-------------------------------------------------------------------------------------------------------------
  131. SUB UpdateScores ()
  132.     '---------------------------------------------------------------------------------------------------------
  133.  
  134.     SHARED PlayField AS LONG '     need access to playing field image
  135.     SHARED PlayFieldCopy AS LONG ' need access to playing field image copy
  136.     SHARED Pscore() AS INTEGER '   need access to player scores
  137.     DIM Clr AS _UNSIGNED LONG '    font color
  138.  
  139.     _PUTIMAGE , PlayFieldCopy, PlayField '                      restore playing field
  140.     _DEST PlayField '                                           make playing field image the destination
  141.     IF Pscore(0) = 9 THEN Clr = BRIGHTWHITE ELSE Clr = WHITE '  set color of font
  142.     DrawScore SCREENWIDTH \ 2 - 100, 50, Pscore(0), Clr '       draw player 1's score
  143.     IF Pscore(1) = 9 THEN Clr = BRIGHTWHITE ELSE Clr = WHITE '  set color of font
  144.     DrawScore SCREENWIDTH \ 2 + 55, 50, Pscore(1), Clr '        draw player 2's score
  145.     _DEST 0 '                                                   set destination back to screen
  146.  
  147.  
  148. '-------------------------------------------------------------------------------------------------------------
  149. SUB DrawScore (dx AS INTEGER, dy AS INTEGER, n AS INTEGER, clr AS _UNSIGNED LONG)
  150.     '---------------------------------------------------------------------------------------------------------
  151.  
  152.     SHARED Score() AS STRING * 15 ' need access to encoded font data
  153.     DIM x AS INTEGER '              font column counter
  154.     DIM y AS INTEGER '              font row counter
  155.     DIM p AS INTEGER '              string position counter
  156.  
  157.     p = 0 '                                       reset string position counter
  158.     FOR y = 0 TO 4 '                              cycle through 5 rows
  159.         FOR x = 0 TO 2 '                          cycle through 3 columns
  160.             p = p + 1 '                           increment string position counter
  161.             IF MID$(Score(n), p, 1) <> " " THEN ' character in this position?
  162.                 LINE (dx + x * 15, dy + y * 15)-(dx + x * 15 + 15, dy + y * 15 + 15), clr, BF ' yes, draw cube
  163.             END IF
  164.     NEXT x, y '                                   leave when all rows and columns processed
  165.  
  166.  
  167. '-------------------------------------------------------------------------------------------------------------
  168. SUB MovePuck ()
  169.     '---------------------------------------------------------------------------------------------------------
  170.  
  171.     SHARED P1Paddle AS MOVINGOBJECT ' need access to player 1 paddle
  172.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  173.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  174.     SHARED Missed AS INTEGER '        need access to puck miss indicator
  175.     SHARED Pscore() AS INTEGER '      need access to player scores
  176.  
  177.     Puck.x = Puck.x + Puck.xv '            add X vector component to X coordinate
  178.     Puck.y = Puck.y + Puck.yv '            add Y vector component to Y coordinate
  179.     Puck.rect.x1 = Puck.x - PUCKSIZE \ 2 ' calculate puck rectangle coordinates
  180.     Puck.rect.y1 = Puck.y - PUCKSIZE \ 2
  181.     Puck.rect.x2 = Puck.x + PUCKSIZE \ 2
  182.     Puck.rect.y2 = Puck.y + PUCKSIZE \ 2
  183.     IF Puck.x < PUCKXMIN OR Puck.x > PUCKXMAX THEN '                             is puck within a paddle area?
  184.         IF SGN(Puck.xv) = -1 AND RectCollide(Puck.rect, P1Paddle.rect) THEN '    going left and hit paddle?
  185.             Puck.xv = -Puck.xv + .025 '                                          yes, reverse X vector
  186.             Puck.x = PUCKXMIN '                                                  place puck to right of paddle
  187.  
  188.             '** change in Y vector component here
  189.  
  190.             SOUND 440, 1 '                                                       audio report
  191.         ELSEIF SGN(Puck.xv) = 1 AND RectCollide(Puck.rect, P2Paddle.rect) THEN ' going right and hit paddle?
  192.             Puck.xv = -Puck.xv - .025 '                                          yes, reverse X vector
  193.             Puck.x = PUCKXMAX '                                                  place puck to left of paddle
  194.  
  195.             '** change in Y vector component here
  196.  
  197.             SOUND 440, 1 '                                                       audio report
  198.         END IF
  199.     END IF
  200.     IF Puck.y < PUCKYMIN OR Puck.y > PUCKYMAX THEN '                             is puck hitting a side line?
  201.         IF SGN(Puck.yv) = -1 THEN Puck.y = PUCKYMIN ELSE Puck.y = PUCKYMAX '     yes, place puck above/below
  202.         Puck.yv = -Puck.yv '                                                     reverse Y vector
  203.         SOUND 880, 1 '                                                           audio report
  204.     END IF
  205.     IF Puck.x < P1MISSED OR Puck.x > P2MISSED THEN '                             did puck hit edge of screen?
  206.         Missed = TRUE '                                                          yes, set missed indicator
  207.         SOUND 220, 1 '                                                           audio report
  208.     END IF
  209.     LINE (Puck.rect.x1, Puck.rect.y1)-(Puck.rect.x2, Puck.rect.y2), BRIGHTWHITE, BF ' draw puck
  210.  
  211.  
  212. '-------------------------------------------------------------------------------------------------------------
  213. SUB MovePaddles ()
  214.     '---------------------------------------------------------------------------------------------------------
  215.  
  216.     SHARED P1Paddle AS MOVINGOBJECT ' need access to player 1 paddle
  217.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  218.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  219.     SHARED Computer AS INTEGER '      need access to computer opponent
  220.  
  221.     IF _KEYDOWN(87) OR _KEYDOWN(119) THEN '                       player 1 pressing W or w keys?
  222.         P1Paddle.y = P1Paddle.y - PADDLESPEED '                   yes, move paddle up
  223.         IF P1Paddle.y < PADDLEYMIN THEN P1Paddle.y = PADDLEYMIN ' keep paddle in playing field
  224.     ELSEIF _KEYDOWN(83) OR _KEYDOWN(115) THEN '                   player 1 pressing S or s keys?
  225.         P1Paddle.y = P1Paddle.y + PADDLESPEED '                   yes, move paddle down
  226.         IF P1Paddle.y > PADDLEYMAX THEN P1Paddle.y = PADDLEYMAX ' keep paddle in playing field
  227.     END IF
  228.     IF Computer THEN '                                            is player 1 playing computer?
  229.         IF SGN(Puck.xv) = 1 THEN '                                yes, is puck heading for computer?
  230.             IF Puck.y - P2Paddle.y < 0 THEN '                     yes, is puck higher than center of paddle?
  231.                 P2Paddle.y = P2Paddle.y - .5 - RND(1) / 3 '       yes, move computer paddle up
  232.                 IF P2Paddle.y < PADDLEYMIN THEN P2Paddle.y = PADDLEYMIN ' keep paddle in playing field
  233.             ELSE '                                                no, puck is below center of paddle
  234.                 P2Paddle.y = P2Paddle.y + .5 + RND(1) / 3 '       move computer paddle down
  235.                 IF P2Paddle.y > PADDLEYMAX THEN P2Paddle.y = PADDLEYMAX ' keep paddle in playing field
  236.             END IF
  237.         END IF
  238.     ELSEIF _KEYDOWN(18432) THEN '                                 player 2 pressing up arrow key?
  239.         P2Paddle.y = P2Paddle.y - PADDLESPEED '                   yes, move paddle up
  240.         IF P2Paddle.y < PADDLEYMIN THEN P2Paddle.y = PADDLEYMIN ' keep paddle in playing field
  241.     ELSEIF _KEYDOWN(20480) THEN '                                 player 2 pressing down arrow key?
  242.         P2Paddle.y = P2Paddle.y + PADDLESPEED '                   yes, move paddle down
  243.         IF P2Paddle.y > PADDLEYMAX THEN P2Paddle.y = PADDLEYMAX ' keep paddle in playing field
  244.     END IF
  245.     P1Paddle.rect.y1 = P1Paddle.y - PADDLEHEIGHT \ 2 '            calc player 1 paddle rectangle
  246.     P1Paddle.rect.y2 = P1Paddle.y + PADDLEHEIGHT \ 2
  247.     P2Paddle.rect.y1 = P2Paddle.y - PADDLEHEIGHT \ 2 '            calc player 2 paddle rectangle
  248.     P2Paddle.rect.y2 = P2Paddle.y + PADDLEHEIGHT \ 2
  249.     LINE (P1Paddle.rect.x1, P1Paddle.rect.y1)-(P1Paddle.rect.x2, P1Paddle.rect.y2), BRIGHTWHITE, BF ' draw
  250.     LINE (P2Paddle.rect.x1, P2Paddle.rect.y1)-(P2Paddle.rect.x2, P2Paddle.rect.y2), BRIGHTWHITE, BF ' paddles
  251.  
  252.  
  253. '-------------------------------------------------------------------------------------------------------------
  254. SUB NewGame ()
  255.     '---------------------------------------------------------------------------------------------------------
  256.  
  257.     SHARED P1Paddle AS MOVINGOBJECT ' need access toplayer 1 paddle
  258.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  259.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  260.     SHARED PlayField AS LONG '        need access to playing field image
  261.     SHARED Pscore() AS INTEGER '      need access to player scores
  262.     SHARED Missed AS INTEGER '        need access to puck miss indicator
  263.     SHARED GameOver AS INTEGER '      need access to game over indicator
  264.     SHARED Computer AS INTEGER '      need access to computer opponent
  265.     DIM Blink AS INTEGER '            blink bit
  266.     DIM NumPlayers AS INTEGER '       number of players selected
  267.     DIM Frames AS INTEGER '           frame counter
  268.  
  269.     P1Paddle.y = SCREENHEIGHT \ 2 '                                                reset paddle locations
  270.     P2Paddle.y = P1Paddle.y
  271.     Puck.xv = .5 '                                                                 reset puck X speed
  272.     Puck.y = SCREENHEIGHT \ 2
  273.     Missed = FALSE '                                                               reset missed indicator
  274.     GameOver = FALSE '                                                             reset game over indicator
  275.     Computer = FALSE '                                                             player 2 is human
  276.     SOUND 440, 1 '                                                                 new game audio
  277.     SOUND 880, 1
  278.     DO '                                                                           begin setup loop
  279.         _LIMIT FPS '                                                               limit frame speed
  280.         _PUTIMAGE , PlayField '                                                    restore playing field
  281.         MovePaddles '                                                              allow paddle movement
  282.         Frames = Frames + 1 '                                                      increment frame counter
  283.         IF Frames = FPS THEN '                                                     max frames reached?
  284.             Frames = 0 '                                                           yes, reset frame counter
  285.             Blink = 1 - Blink '                                                    toggle blink bit
  286.         END IF
  287.         LOCATE (SCREENHEIGHT \ 16), ((SCREENWIDTH \ 8) - 51) \ 2 + 1 '             position cursor on screen
  288.         SELECT CASE Blink '                                                        which blinker bit?
  289.             CASE 0 '                                                               blinker bit 0
  290.                 IF NumPlayers = 0 THEN '                                           number of players chosen?
  291.                     PRINT "    SELECT (1) OR (2) PLAYERS NOW : ESC TO EXIT    "; ' no, display player select
  292.                 ELSE '                                                             yes
  293.                     PRINT "(1) OR (2) PLAYERS : SPACEBAR TO PLAY : ESC TO EXIT"; ' display all instructions
  294.                 END IF
  295.             CASE 1 '                                                               blinker bit 1
  296.                 PRINT SPACE$(51); '                                                remove instructions
  297.         END SELECT
  298.         LOCATE (SCREENHEIGHT \ 16) - 2, ((SCREENWIDTH \ 16) - 27) \ 2 + 1 '        position cursor on screen
  299.         PRINT "PLAYER 1: W = UP / S = DOWN" '                                      display keys to player 1
  300.         LOCATE (SCREENHEIGHT \ 16) - 2, ((SCREENWIDTH \ 8) - 31) \ 2 + (SCREENWIDTH \ 32) + 1 'position cursor
  301.         SELECT CASE NumPlayers '                                                   how many players selected?
  302.             CASE 0: PRINT " SELECT (1) OR (2) PLAYERS NOW " '                      ask number of players
  303.             CASE 1: PRINT " PLAYER 2: THE CPU HAS CONTROL " '                      inform computer playing
  304.             CASE 2: PRINT "PLAYER 2: UP ARROW / DOWN ARROW" '                      display keys player 2
  305.         END SELECT
  306.         IF NumPlayers <> 1 THEN '                                                  has one player been chosen?
  307.             IF _KEYDOWN(49) THEN '                                                 no, "1" key been pressed?
  308.                 NumPlayers = 1 '                                                   yes, set players to 1
  309.                 Computer = TRUE '                                                  remember AI now has control
  310.                 SOUND 440, 1 '                                                     play affirmation beep
  311.                 SOUND 880, 1 '                                                     play affirmation beep
  312.             END IF
  313.         END IF
  314.         IF NumPlayers <> 2 THEN '                                                  has two player been chosen?
  315.             IF _KEYDOWN(50) THEN '                                                 no, "2" key been pressed?
  316.                 NumPlayers = 2 '                                                   yes, set players to 2
  317.                 Computer = FALSE '                                                 human is playing human
  318.                 SOUND 440, 1 '                                                     play affirmation beep
  319.                 SOUND 880, 1 '                                                     play affirmation beep
  320.             END IF
  321.         END IF
  322.         _DISPLAY '                                                                 update screen with changes
  323.     LOOP UNTIL (_KEYDOWN(32) AND NumPlayers) OR _KEYDOWN(27) '                     leave when players select
  324.     Pscore(0) = 0 '                                                                reset scores
  325.     Pscore(1) = 0
  326.     UpdateScores '                                                                 update on screen scores
  327.  
  328.  
  329. '-------------------------------------------------------------------------------------------------------------
  330. SUB Initialize ()
  331.     '---------------------------------------------------------------------------------------------------------
  332.  
  333.     SHARED PlayField AS LONG '        need access to playing field image
  334.     SHARED PlayFieldCopy AS LONG '    need access to playing field image copy
  335.     SHARED P1Paddle AS MOVINGOBJECT ' need access to player 1 paddle
  336.     SHARED P2Paddle AS MOVINGOBJECT ' need access to player 2 paddle
  337.     SHARED Puck AS MOVINGOBJECT '     need access to puck
  338.     SHARED Score() AS STRING * 15 '   need access to encoded font data
  339.     DIM y AS INTEGER '                center line counter
  340.  
  341.     RANDOMIZE TIMER '                                               seed random number generator
  342.  
  343.     '** do as many precalculations as possible for values that will not change
  344.  
  345.     P1Paddle.x = PADDLEOFFSET + PADDLEWIDTH \ 2 '                   calculate player 1 paddle X coordinate
  346.     P2Paddle.x = SCREENWIDTH - 1 - PADDLEOFFSET - PADDLEWIDTH \ 2 ' calculate player 2 paddle X coordinate
  347.     P1Paddle.rect.x1 = P1Paddle.x - PADDLEWIDTH \ 2 '               calculate player 1 paddle rectangle
  348.     P1Paddle.rect.x2 = P1Paddle.x + PADDLEWIDTH \ 2
  349.     P2Paddle.rect.x1 = P2Paddle.x - PADDLEWIDTH \ 2 '               calculate player 2 paddle rectangle
  350.     P2Paddle.rect.x2 = P2Paddle.x + PADDLEWIDTH \ 2
  351.  
  352.     '** draw default playing field
  353.  
  354.     PlayField = _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32) '          create image
  355.     _DEST PlayField '                                               make it destination
  356.     CLS '                                                           clear image
  357.     LINE (0, 19)-(SCREENWIDTH - 1, 29), BRIGHTWHITE, BF '           draw top side line
  358.     LINE (0, SCREENHEIGHT - 30)-(SCREENWIDTH - 1, SCREENHEIGHT - 20), BRIGHTWHITE, BF ' draw bottom side line
  359.     FOR y = 40 TO SCREENHEIGHT - 52 STEP 30 '                       cycle top to bottom
  360.         LINE ((SCREENWIDTH \ 2) - 5, y)-((SCREENWIDTH \ 2) + 5, y + 15), GRAY, BF '     draw center lines
  361.     NEXT y
  362.     _DEST 0 '                                                       destination back to screen
  363.     PlayFieldCopy = _COPYIMAGE(PlayField) '                         save a copy of the playing field image
  364.     Score(0) = "0000 00 00 0000" '                                  create numeric font data
  365.     Score(1) = "  1  1  1  1  1"
  366.     Score(2) = "222  22222  222"
  367.     Score(3) = "333  3333  3333"
  368.     Score(4) = "4 44 4444  4  4"
  369.     Score(5) = "5555  555  5555"
  370.     Score(6) = "6666  6666 6666"
  371.     Score(7) = "777  7  7  7  7"
  372.     Score(8) = "8888 88888 8888"
  373.     Score(9) = "9999 9999  9999"
  374.  
  375.  
  376. '-------------------------------------------------------------------------------------------------------------
  377. FUNCTION RectCollide (Rect1 AS RECTTYPE, Rect2 AS RECTTYPE)
  378.     '---------------------------------------------------------------------------------------------------------
  379.  
  380.     RectCollide = FALSE '                       assume no collision
  381.     IF Rect1.x2 >= Rect2.x1 THEN '              rect 1 lower right X >= rect 2 upper left  X ?
  382.         IF Rect1.x1 <= Rect2.x2 THEN '          rect 1 upper left  X <= rect 2 lower right x ?
  383.             IF Rect1.y2 >= Rect2.y1 THEN '      rect 1 lower right Y >= rect 2 upper left  Y ?
  384.                 IF Rect1.y1 <= Rect2.y2 THEN '  rect 1 upper left  Y <= rect 2 lower right Y ?
  385.                     RectCollide = TRUE '        if all 4 IFs TRUE then a collision is occurring
  386.                 END IF
  387.             END IF
  388.         END IF
  389.     END IF
  390.  
  391.  
  392. '-------------------------------------------------------------------------------------------------------------
  393. SUB Cleanup ()
  394.     '---------------------------------------------------------------------------------------------------------
  395.  
  396.     SHARED PlayField AS LONG '     need access to playing field image
  397.     SHARED PlayFieldCopy AS LONG ' need access to playing field image copy
  398.  
  399.     _FREEIMAGE PlayField '         free images from memory
  400.     _FREEIMAGE PlayFieldCopy
  401.     SYSTEM '                       return to operating system
  402.  
  403.  
  404. '-------------------------------------------------------------------------------------------------------------
  405. FUNCTION Yvector (PaddleY AS INTEGER, PuckY AS INTEGER, PuckYV)
  406.     '---------------------------------------------------------------------------------------------------------
  407.  
  408.  
  409.     '** math here (ugh)
  410.  
  411.  
  412.  
« Last Edit: June 04, 2020, 08:05:57 pm by TerryRitchie »
In order to understand recursion, one must first understand recursion.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #7 on: June 04, 2020, 08:26:36 pm »
I might be getting a little lost in the question...

So it looks like the game posted above works fine - the ball just reverses its x-velocity when it hits the paddle, or so it seems. Is this the desired behavior or no? Going from the figure at the top of the page, I see some things that ordinarily wouldn't be there in this problem - like why is the paddle drawn with some kind of generating circle? Is there something about the pong mechanics that's not so obvious?
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #8 on: June 04, 2020, 08:30:17 pm »
Yeah, Terry your pong game is working for me also?

You could put a tiny bit or randomness in when ball dx is changed so it's not quite so boring, call it English on the ball. :)
« Last Edit: June 04, 2020, 08:32:58 pm by bplus »

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #9 on: June 04, 2020, 08:31:29 pm »
I might be getting a little lost in the question...

So it looks like the game posted above works fine - the ball just reverses its x-velocity when it hits the paddle, or so it seems. Is this the desired behavior or no? Going from the figure at the top of the page, I see some things that ordinarily wouldn't be there in this problem - like why is the paddle drawn with some kind of generating circle? Is there something about the pong mechanics that's not so obvious?

I would like the front of the paddle to be seen as an arc rather than a flat surface. The puck would then have its Y vector component modified based on bouncing off that arc instead of flat surface.
« Last Edit: June 04, 2020, 08:35:52 pm by TerryRitchie »
In order to understand recursion, one must first understand recursion.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #10 on: June 04, 2020, 08:33:59 pm »
OH! I totally get what you want.

This reminds me of a fresnel lens, where its a huge lens for light, but perfectly flat. The surface is graded as if the whole thing was curved. IIIIII know what you want.

bplus want this or should I?
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #11 on: June 04, 2020, 08:37:48 pm »
OH! I totally get what you want.

This reminds me of a fresnel lens, where its a huge lens for light, but perfectly flat. The surface is graded as if the whole thing was curved. IIIIII know what you want.

bplus want this or should I?

My Air Hockey Game is it! I use round Strikers instead of flat paddles for much more interesting play.

Looking up link now...

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #12 on: June 04, 2020, 08:38:26 pm »
OH! I totally get what you want.

This reminds me of a fresnel lens, where its a huge lens for light, but perfectly flat. The surface is graded as if the whole thing was curved. IIIIII know what you want.

bplus want this or should I?

Yeah :-)  Yes, right now the only time the puck gets its Y vector component changed is when it hits a side line. Being able to change the Y vector with the paddle will add variableness (is that word?) to the trajectory of the puck. Just like air hockey, but a more subtle arc.
In order to understand recursion, one must first understand recursion.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #13 on: June 04, 2020, 08:39:19 pm »
So to be clear - the paddle needs to look like a paddle and move like a paddle, but reflect the ball as if curved. (This is a one-liner solution with vectors but it seems like bplus has something close to cooked already.)
« Last Edit: June 04, 2020, 08:40:23 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Math help with puck/paddle bounce
« Reply #14 on: June 04, 2020, 08:43:59 pm »
A more subtle arc like from an ellipse?

I have a Breakout Game that uses a "loaf-of-bread" paddle, LOL. I have to check the math I used for that.