Pong AI Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    Pong AI Problem

    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:

    Code:
    public void alert()
    		{
    			//set path based on the balls anticipated y-intercept
    			if(controlLeft)
    			{
    				System.out.println("Left Pad AI ALERTED");
    
    				//find the distance that needs to be travelled
    				int distanceX, distanceY;
    				distanceX = ballX-leftPadX;
    				distanceY = (int)(ballY-leftPadY-((.5)*leftPadHeight)-(.5*ballSize));
    				
    				double timeX = distanceX*1.0/ballSpeedX;
    				double timeY = distanceY*1.0/ballSpeedY;
    				
    				if(timeY<timeX) mySpeed = (distanceY)/Math.abs(distanceY);
    				else mySpeed = (int)(distanceY*1.0/timeX);		
    			}
    		}
    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
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    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

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Hi drsmooth!
      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.
      Originally posted by drsmooth
      heres what i got so far:

      Code:
      public void alert()
      		{
      			//set path based on the balls anticipated y-intercept
      			if(controlLeft)
      			{
      				System.out.println("Left Pad AI ALERTED");
      
      				//find the distance that needs to be travelled
      				int distanceX, distanceY;
      				distanceX = ballX-leftPadX;
      				distanceY = (int)(ballY-leftPadY-((.5)*leftPadHeight)-(.5*ballSize));
      				
      				double timeX = distanceX*1.0/ballSpeedX;
      				double timeY = distanceY*1.0/ballSpeedY;
      				
      				if(timeY<timeX) mySpeed = (distanceY)/Math.abs(distanceY);
      				else mySpeed = (int)(distanceY*1.0/timeX);		
      			}
      		}
      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[code=java]double timeY = distanceY*1.0/ballSpeedY;[/code]Shouldn't the distance between ballY and it's destinationY be used here?

      By the way, I'd change the lines[code=java]if(timeY<timeX) mySpeed = (distanceY)/Math.abs(distan ceY);
      else mySpeed = (int)(distanceY *1.0/timeX);[/code]to[code=java]if(timeY<timeX)
      {
      if(distanceY > 0)
      mySpeed = 1;
      else if(distanceY < 0)
      mySpeed = -1;
      else
      mySpeed = 0;
      }
      else mySpeed = (int)((double)d istanceY/timeX);[/code]or in another way prevent a division with 0 (because if distanceY == 0 then Math.abs(distan ceY) == 0 => big problem).
      That might even be the problem!

      Greetings,
      Nepomuk

      Comment

      • drsmooth
        New Member
        • Oct 2007
        • 112

        #4
        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...il l 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:

        Code:
          public void alert()
          {
           //set path based on the balls anticipated y-intercept
           if(controlLeft)
           {
            System.out.println("Left Pad AI ALERTED");
        
            //find the distance that needs to be travelled
            int distanceX, distanceY;
            distanceX = ballX-leftPadX;
            
            double timeX = distanceX*1.0/ballSpeedX;
           //to find the distance in the y direction multiply the time in the x direction by the speed in the Y
            distanceY = (int)(timeX*ballSpeedY);
            
            double timeY = distanceY*1.0/ballSpeedY;
            
            if(timeY<timeX)
            {
                if(distanceY > 0) mySpeed = 1;
                else if(distanceY < 0) mySpeed = -1;
                else mySpeed = 0;
            } 
            else 
            {
                mySpeed = (int)((double)distanceY/timeX);
            }
           }
          }
        i also have a question about this line:

        Code:
         //to find the distance in the y direction multiply the time in the x direction by the speed in the Y
            distanceY = (int)(timeX*ballSpeedY);
            
            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...

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          Read this: http://javaboutique.in ternet.com/tutorials/Java_Game_Progr amming/PongKIEng.html

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            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.

            Comment

            • drsmooth
              New Member
              • Oct 2007
              • 112

              #7
              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

              Comment

              Working...