🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Pong AI help

Started by
2 comments, last by Seann999 12 years, 2 months ago
Right now I'm working on a pong clone game and the AI I want is pretty simple, if the ball is above the paddle, the paddle moves up and reverse. What I have is this:


if(ball.getY() < y - height/2){
moveUp();
}

if(ball.getY() > y + height/2){
moveDown();
}


What's happening is the paddle looks like it's spazzing out, it alternates between going up and down like every .5 seconds, but still travels in the general direction it's supposed to go.
Advertisement
moveUp and moveDown affect the y variable right? In that case you may want to make it so that only one of those functions is called each time (possibly by turning the moveDown if statement into an else-if statement)
#1 question before anyone should EVER bother helping you: Have you traced the execution of your code? If so, what happens? If you find it, you will know what your problem is and likely not need anyone else to do it for you.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

The ai paddle is probably spazzin out because each frame, when the paddle catches up to the ball, it goes too far up/down and in the next frame has to go back down/up. So instead of moving the opponent at a constand speed/frame, it should move by the difference between the position of the ball and center of paddle. Example: move by ballY - paddleY, if difference is greater than the max speed the paddle can go, put it as max speed.

This topic is closed to new replies.

Advertisement