Connecting Tech Pros Worldwide Forums | Help | Site Map

Pong AI Problem

Member
 
Join Date: Oct 2007
Posts: 112
#1: Jun 13 '08
im just starting to work on AI things and i started with a pong game...

i came up with a system that every time the ball hits a wall or a paddle it calls the alert() method of the AIController class.

the problem is the paddle always reaches and hits the ball the first time, then seems to move in crazy direction and will never hit another shot...

heres what i got so far:

Expand|Select|Wrap|Line Numbers
  1. public void alert()
  2.         {
  3.             //set path based on the balls anticipated y-intercept
  4.             if(controlLeft)
  5.             {
  6.                 System.out.println("Left Pad AI ALERTED");
  7.  
  8.                 //find the distance that needs to be travelled
  9.                 int distanceX, distanceY;
  10.                 distanceX = ballX-leftPadX;
  11.                 distanceY = (int)(ballY-leftPadY-((.5)*leftPadHeight)-(.5*ballSize));
  12.  
  13.                 double timeX = distanceX*1.0/ballSpeedX;
  14.                 double timeY = distanceY*1.0/ballSpeedY;
  15.  
  16.                 if(timeY<timeX) mySpeed = (distanceY)/Math.abs(distanceY);
  17.                 else mySpeed = (int)(distanceY*1.0/timeX);        
  18.             }
  19.         }
the paddle attempst to predict where the ball will intercept the y-axis and calculate the speed it will need to move to get to the other side but it doesnt really work too well and i cant figure out why....

any ideas?
thanks,
ken

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: Jun 17 '08

re: Pong AI Problem


Greetings, drsmooth!

It look are using keys to move about your screens. If this is a fact, try stepping into it in debug mode to pin point the area with the problem, or at least be able to see where it runs wild!

Thanks for posting your code, by the way, it helps:-)

Dököll
Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#3: Jun 17 '08

re: Pong AI Problem


Hi drsmooth!
Quote:

Originally Posted by drsmooth

i came up with a system that every time the ball hits a wall or a paddle it calls the alert() method of the AIController class.

the problem is the paddle always reaches and hits the ball the first time, then seems to move in crazy direction and will never hit another shot...

That sounds to me, as if some old value was being used instead of the new value needed for the new calculation.
Quote:

Originally Posted by drsmooth

heres what i got so far:

Expand|Select|Wrap|Line Numbers
  1. public void alert()
  2.         {
  3.             //set path based on the balls anticipated y-intercept
  4.             if(controlLeft)
  5.             {
  6.                 System.out.println("Left Pad AI ALERTED");
  7.  
  8.                 //find the distance that needs to be travelled
  9.                 int distanceX, distanceY;
  10.                 distanceX = ballX-leftPadX;
  11.                 distanceY = (int)(ballY-leftPadY-((.5)*leftPadHeight)-(.5*ballSize));
  12.  
  13.                 double timeX = distanceX*1.0/ballSpeedX;
  14.                 double timeY = distanceY*1.0/ballSpeedY;
  15.  
  16.                 if(timeY<timeX) mySpeed = (distanceY)/Math.abs(distanceY);
  17.                 else mySpeed = (int)(distanceY*1.0/timeX);        
  18.             }
  19.         }
the paddle attempst to predict where the ball will intercept the y-axis and calculate the speed it will need to move to get to the other side but it doesnt really work too well and i cant figure out why....

As I said before, I'm assuming, that some old value is being used. The variables ballX, ballY, leftPadX, leftPadY, leftPadHeight, ballSize, ballSpeedX and ballSpeedY seem to be global, so the problem should lie with one of those.
I guess, ballX and ballY are probably ok, as they are changed all the time and it works the first time.
Possibly there is an error with leftPadY - does it actually get set to the new value? (Or is the Pad set back to the middle of the screen?)
The size values are probably fine. The time and speed values confuse me a little - why do you use
Expand|Select|Wrap|Line Numbers
  1. double timeY = distanceY*1.0/ballSpeedY;
Shouldn't the distance between ballY and it's destinationY be used here?

By the way, I'd change the lines
Expand|Select|Wrap|Line Numbers
  1. if(timeY<timeX) mySpeed = (distanceY)/Math.abs(distanceY);
  2. else mySpeed = (int)(distanceY*1.0/timeX);
to
Expand|Select|Wrap|Line Numbers
  1. if(timeY<timeX)
  2. {
  3.     if(distanceY > 0)
  4.         mySpeed = 1;
  5.     else if(distanceY < 0)
  6.         mySpeed = -1;
  7.     else
  8.         mySpeed = 0;
  9. }
  10. else mySpeed = (int)((double)distanceY/timeX);
or in another way prevent a division with 0 (because if distanceY == 0 then Math.abs(distanceY) == 0 => big problem).
That might even be the problem!

Greetings,
Nepomuk
Member
 
Join Date: Oct 2007
Posts: 112
#4: Jun 18 '08

re: Pong AI Problem


thanks alot for all youre help....sorry it took me so long to reply, ive been so busy with finals and things at school...now im done so i can refocus my efforts on getting this to work...

i think i got the paddle to work now...just for starters i want to make it block everything...ill put in the error later.

problem now is i have glitches at some of the border cases where the paddle will freak out and miss like 5 in a row and then get back in sync with the ball...what could cause that?

if it helps, this is the new code for the alert method:

Expand|Select|Wrap|Line Numbers
  1.   public void alert()
  2.   {
  3.    //set path based on the balls anticipated y-intercept
  4.    if(controlLeft)
  5.    {
  6.     System.out.println("Left Pad AI ALERTED");
  7.  
  8.     //find the distance that needs to be travelled
  9.     int distanceX, distanceY;
  10.     distanceX = ballX-leftPadX;
  11.  
  12.     double timeX = distanceX*1.0/ballSpeedX;
  13.    //to find the distance in the y direction multiply the time in the x direction by the speed in the Y
  14.     distanceY = (int)(timeX*ballSpeedY);
  15.  
  16.     double timeY = distanceY*1.0/ballSpeedY;
  17.  
  18.     if(timeY<timeX)
  19.     {
  20.         if(distanceY > 0) mySpeed = 1;
  21.         else if(distanceY < 0) mySpeed = -1;
  22.         else mySpeed = 0;
  23.     } 
  24.     else 
  25.     {
  26.         mySpeed = (int)((double)distanceY/timeX);
  27.     }
  28.    }
  29.   }
i also have a question about this line:

Expand|Select|Wrap|Line Numbers
  1.  //to find the distance in the y direction multiply the time in the x direction by the speed in the Y
  2.     distanceY = (int)(timeX*ballSpeedY);
  3.  
  4.     double timeY = distanceY*1.0/ballSpeedY;
i changed it up so that the distanceY would be the time calculated before the ball crossed the x-axis * the speed in the Y-direction...so the timeY value is uselesss? the paddle behaves differently, but it seems to work either way, with the same problems...
Familiar Sight
 
Join Date: Mar 2008
Posts: 174
#5: Jun 20 '08

re: Pong AI Problem


Read this: http://javaboutique.internet.com/tutorials/Java_Game_Programming/PongKIEng.html
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#6: Jun 20 '08

re: Pong AI Problem


Actually I encourage you, drsmooth, to not read that. You are doing it the right way trying to puzzle it out yourself. I admire that. The experts in this forum will do their best to help you. Do you have any unit tests for this yet? I suggest you write some that will test different values of ball speed and time so that you can get an idea of what combination of values are causing your paddle to freak out.

Another good option is to go through it step by step and verify that all your calculations are correct.
Member
 
Join Date: Oct 2007
Posts: 112
#7: Jun 21 '08

re: Pong AI Problem


thanks all of you, i will work on some test data this week and if i still cant crack it i will tell you all of my findings...

i did however look at that site and sortof already covered that ground so it didnt provide me much of a solution...

im not also considering that my collision detection may be part of the problem...i will be stepping through and trying to find the problems this week

thanks for all of you help,
ken
Reply


Similar Java bytes