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:
- 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
- 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
- if(timeY<timeX) mySpeed = (distanceY)/Math.abs(distanceY);
-
else mySpeed = (int)(distanceY*1.0/timeX);
to
- if(timeY<timeX)
-
{
-
if(distanceY > 0)
-
mySpeed = 1;
-
else if(distanceY < 0)
-
mySpeed = -1;
-
else
-
mySpeed = 0;
-
}
-
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