Search code examples
mathalgebra

Math - Difficult Pong AI


I am writing a number of difficulties for my Pong clone I am writing to familiarize myself with SFML and Xcode. For the hardest difficulty, I would like to create an AI level where the computer knows instantly where the ball will go. So, if I had my xVelocity and my yVelocity, I could effectively have the slope. The thing is, every time the ball hits the top or bottom, the yVelocity reverses. So essentially, I have an algebra slope problem which does the opposite every time the walls are hit.

Now, my screen height is 600 pixels and the hit detection is 5 pixels on the top and bottom making the space 590 pixels.

My question: Is there are sort of formula which would encompass all of these factors. Say for instance, the ball is hit at x = 30 and y = 240 with a slope of 1.45, I want to get the y value at which it will hit when x = 770.

Let me know if I can simplify this. Again, I know how I could figure it out by calculating it say 4 times if the ball bounces 4 times but I was wondering if there was a way to figure it out taking in y velocity switch at the boundaries.

Thanks!


Solution

  • Edit: Just read your screen is actually 590 pixels high, this changes the math but not the formulas

    Calculate where the ball would hit ignoring the collisions. If (0,0) is the top left of your arena, take y = mx + b, where b is your y offset (ball was hit at y = 240) and m is your slope (1.45)

    Now we want to know what y will equal when x is 770-30 places further, so do the math:

    y = (1.45)(740) + (240) = 1313
    

    This is obviously outside of your range. It will have reflected

    y/height = floor(1313/590) = 2 times
    

    meaning the slope is still moving upward, and it will hit at

    y mod height = 1313 mod 590 or 133
    

    If it had reflected an odd number of times (floor(y/2) %2 == 1) then you would have to use the following to calculate it

    MAX_HEIGHT - (y mod height) = 590 - (1903 mod 590) = 590 - 133 = 457
    

    You can visualize this by stacking multiple 590 height fields on top of each other, with one being where you started:

    --------------------------------------------------------------------
    |
    |
    |
    |                                                 ball ends up here (*)
    |                                                                *
    |                                                            *
    |                                                        *
    ------------------------(reflection  two)------------*---------------
    |                                                *
    |                                            *
    |                                        *
    |                                    *
    |                                *
    |                            *
    |                        *
    ---------------------*---------(reflection 1)------------------------------
    |                *
    |            *
    |        *
    |    *
    |*ball hit here
    | 
    |
    -----------------------------------------------------------------------
    

    The same ideas should apply for going downward. Calculate position, figure out number of reflections, use mod or 590 - mod to determine where it should be.