Jump to content
  • Advertisement
Sign in to follow this  
Slateboard

Help with Pong AI in C# w/ XNA

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on Pong in C# w/ XNA (a rewrite from my last attempt), and I'm now at the point where I want to give the second paddle AI.

I'm not sure how to implement it. All previous attempts resulted in the paddle stuttering or not moving correctly.

Share this post


Link to post
Share on other sites
Advertisement
You haven't provided much information in your post. How have you been approaching the problem so far?

Share this post


Link to post
Share on other sites
Well in my attempts, I tried to move the paddle when the ball moved in a certain direction.

If it's Y value was += a number, then it meant the ball was moving upwards, and so the paddle would move upwards to stop it.

The problem here is that it'd simply move upward, without any real attempt at following the ball, so it looked really bad.

Share this post


Link to post
Share on other sites
I have seen this be done:
Say you are playing from left to right, you could compare the balls y position with the paddle's y position.

If the ball is higher, then move the paddle higher (the amount it would move could be stored in a vairable that you could tweak to adjust speed and judder and difficulty)

If the ball is lower, then move the paddle lower.

On the game I saw, the above would only occur when the ball was heading towards the computer controlled pad, not when heading away.

This seemed natural enough in that I only realised what it was doing when I purposely decided to think what it was doing. Then it took about 3 volleys.

This is very basic but it's a start and I think it may help you. Let me know if it doesn't.

Share this post


Link to post
Share on other sites
I got some time to try out your method and it worked.

It checks to see if the ball is incoming (based on whether it goes to the right of the screen's midpoint) and if the Ball's Y position is greater than or less than the Paddle's Y position, it moves accordingly.

My next step with the AI is to change the paddle's speed or something to reflect greater difficulty.

Share this post


Link to post
Share on other sites
This is how i aprroched the same problem

All pseudo code ofc

//Some Variables
float maxMovmentValue = 5f; //The Maximum amount the paddle can move at any one time

void MoveAiPaddle(float Amount)
{
//We simpally move the Ai's paddle along the Y axis by 'Amount'
Ai.Paddle.Position += MathHelper.Clamp(Amount, -maxMovmentValue, maxMovmentValue);
}


void CheckRelPos()
{
//Check the paddles Y Position Relative the the Balls position
float ballYPos = pongBall.Center.YPosition; // we get the CENTER of the Texture
float paddleYPos = Ai.Paddle.Center.Y;


float difference = paddleYPos - ballYPos;
//Here we attempt to move the paddle by the difference in y values
//The Method should clamp the value so it can only move a certain amount.
MoveAiPaddle(difference);
}




This works if your ball speeds up and if you have the ball bounding off at an angle from your paddle.

The AI paddle is always moving to keep its center inline with the center of the ball. if the ball is moving tofast the paddle may not reach it as it can only move by '' of course just tweak the value to your liking or something.

P.S. this may not be the best way but at least its an idea

Best of Luck ~Andy^

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!