Public Group

This topic is 2378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
Of course it is. Put a breakpoint in there when it does that and you might notice that the ball is above the paddle, then the paddle moves and is above the ball, so the next frame it will move down again and on and on and on.

A concept you might want to look up is called Hysteresis (IF you really want to keep that approach).

Even if you add an area where the paddle doesn't move it won't look very natural to constantly stop-move-stop, because a real player estimates where the ball WILL be when it reaches him. AI can easily calculate that, add a random error and simply move to that position. To look more like a human player, the calculated position can be adjusted every once in a while as the ball gets closer and the random error gets smaller.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 87
• 11
• 10
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009615
• ### Who's Online (See full list)

There are no registered users currently online

×