I am trying to make an AI for my pong game, but the AI controlled paddle jitters a lot when it is trying to hit the ball.
Here is the AI section of my code:
function calculateAI() {
if (paddleA.AI) {
if (ball.x + (ball.width / 2) <= ctx.canvas.width * 0.4 && ball.directionX == -1) {
if (ball.y + (ball.height / 2) >= paddleA.y + (paddleA.height + paddleA.heightModifier * 0.75) && paddleA.y + paddleA.height + paddleA.heightModifier < ctx.canvas.height) {
paddleA.y += paddleA.speed + paddleA.speedModifier;
}
else if (ball.y + (ball.height / 2) <= paddleA.y + (paddleA.height + paddleA.heightModifier * 0.25) && paddleA.y > 0) {
paddleA.y -= paddleA.speed + paddleA.speedModifier;
}
}
else {
if (paddleA.y + ((paddleA.height + paddleA.heightModifier) / 2) <= (ctx.canvas.height / 2) - 50 && paddleA.y + paddleA.height + paddleA.heightModifier < ctx.canvas.height) {
paddleA.y += paddleA.speed + paddleA.speedModifier;
}
else if (paddleA.y + ((paddleA.height + paddleA.heightModifier) / 2) >= (ctx.canvas.height / 2) + 50 && paddleA.y > 0) {
paddleA.y -= paddleA.speed + paddleA.speedModifier;
}
}
}
}
You can find a .zip of the full code here: http://dl.dropbox.com/u/23225581/Pong.zip
My question, is how could I restructure this so the AI doesn't jitter so much? If anyone has other suggestions for the AI in general, I would love to hear them.