Pong AI

Started by
17 comments, last by nano511 12 years, 8 months ago
Hello, before i start, i chose to put this here because i think its to simple to be called AI. But anyways..

I want to be able to find out where the ball.yPos is going to hit AI_X_POS. I dont really know how i would do this at all. Could someone please explain how i would go about doing this. Dont just give me a whole big line of code with a whole bunch of math. Im just going into 9nth grade these year so please be as simple as possible.


E: WAIT DONT TELL ME! I just remembered something i learned last year that might be able to figure it out. I'll tell you if it diddnt work

E2: I think i got like half of it. Here's what i have... y = slope * AI_X_POS + b. Not really sure where to go from here o_O( slope = (ball.yPos - lastBallYPos) / (ball.xPos - lastBallXPos ); )

You can help me nowbiggrin.gif
Advertisement

Hello, before i start, i chose to put this here because i think its to simple to be called AI. But anyways..

I want to be able to find out where the ball.yPos is going to hit AI_X_POS. I dont really know how i would do this at all. Could someone please explain how i would go about doing this. Dont just give me a whole big line of code with a whole bunch of math. Im just going into 9nth grade these year so please be as simple as possible.


E: WAIT DONT TELL ME! I just remembered something i learned last year that might be able to figure it out. I'll tell you if it diddnt work

E2: I think i got like half of it. Here's what i have... y = slope * AI_X_POS + b. Not really sure where to go from here o_O( slope = (ball.yPos - lastBallYPos) / (ball.xPos - lastBallXPos ); )

You can help me nowbiggrin.gif


If we got a ball with position bx,by and velocity vx,vy we only need to figure out what by will be when bx is AI_X_POS

thus we get:
targetx = bx+t*vx;
targety = by+t*vy;

we allready know what targetx should be:

thus

AI_X_POS = bx+t*vx;

since we know bx and vx allready we can get t

t = (AI_X_POS-bx)/vx; (if t is negative the ball is moving away from the ai paddle)

now we can calculate targety using the formula above

if targety is higher or lower than the upper/lower edge of the field the ball will bounce and the AIs prediction will be wrong, for pong however this is probably a good idea to keep the AI beatable (the AI will switch direction when the ball bounces on the walls rather than move to the correct position immediatly)

given the variables you have we get something like :


float t = (AI_X_POS-ball.xPos) / (ball.xPos-ball.lastXPos);
float AI_TARGET_Y_POS = min(highestPossibleYPosition,max(lowestPossibleYPosition,ball.yPos + t*(ball.yPos-ball.lastXPos)));
//(highest and lowest PossibleYPosition should be set to the highest and lowest Y positions the paddle can have)


(you have to make sure that ball.xPos-ball.lastXPos never is 0)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
So the ball is moving in a linear motion so we can describe the line by the an equation of the form y = k * x + m

We can easily calulate k an m:
k = ball.xVel / ball.yVel
m = ball.yPos - k * ball.xPos

now we can solve the equation when x=AI_X_POS
y = k * AI_X_POS + m

You might also want to take the wall bounce into the calculation but that should not be too hard when you know the basics.
You're pretty much there :-)

You've got y = mx + c. Just set c = ball.yPos - then you're basically adding the change in position (in y) to the current position (in y) which will give you the exact coordinates where it hits the AI's vertical line.

Incidentally, I'm not sure why you need to do currentPosition - lastPosition to find slope. Don't you store the ball's velocity (speed and direction - how the ball's position changes over time) as separate x and y coordinates somewhere? If so, you could just do something like ball.xVel / ball.yVel.
[TheUnbeliever]

So the ball is moving in a linear motion so we can describe the line by the an equation of the form y = k * x + m

We can easily calulate k an m:
k = ball.xVel / ball.yVel
m = ball.yPos - k * ball.xPos

now we can solve the equation when x=AI_X_POS
y = k * AI_X_POS + m

You might also want to take the wall bounce into the calculation but that should not be too hard when you know the basics.


Lol. Obviously the OP was asking about wall bounces. He wanted to know how to make predictions about where the ball will end up once it reaches the opposing side.

Not how to calculate movement in a straight line.



The answer is that you must compute the linear trajectory and see if it hits before the opponent's side, if not, you have the intersection. if it does, you calculate one more iteration, if this hits the opponent's side, you're done. if not, you have the interval length, now you go "mod that length" and then you only have to compute from the last interval to the opponent's side.

it's very simple and can easily take place in the time it takes to render a single frame. if you want an unscoreable opponent, this is all you need to do.

having it angle the ball to maximize the difficulty of your return is a simple linear system on points scored against you vs. the terminal angle of the ball as it approaches your side, using the reverse of the above calculation to force it.

pong has always been a game of dumbing down the opponent enough that the game is fun to play.
Im talking about finding where the ball will be when its in line with the line that the AI moves on. The AI only moves up and down on one line, AI_x_POS. I need the AI to know what the ball's y value will be when the ball crosses AI_X_POS. the AI need to know this before the ball reaches there. I

have yet to read simons because i dont have time right now, but someone was talking about bouncing off walls. For that, i just reverse the y value. Ill be back in about an hour and a half and ill tell you what's going on.
Here nano. Readup.


http://en.wikipedia....ne_intersection

This might give you a general idee.
Idono! That's me!
What do i have to include to use min() and max()?
I included algorithm and im using namespace std but it says "no instance of overloaded function "min: matches the argument list" and the same for max.
I still need help lol

This topic is closed to new replies.

Advertisement