# dx9 and pong

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

## Recommended Posts

I am working on a pong game using dx9 and c++.I want to have the computer paddle move up and down on its own.let me know if you need some code or more information.

##### Share on other sites
How about having the computer predict the location where the ball will go and move its paddle there at a predefined speed (possibly depending on the speed of the ball)?

You can predict the ball's destination by intersecting its velocity vector with the paddle axis (and if the velocity vector is normalized, the distance of the intersection along that vector also happens to give the time to intersection).

##### Share on other sites
here is the code I am using
 if(right_paddle>=-220) { right_paddle=-right_paddle_vel; } if(right_paddle<=220) { right_paddle+=right_paddle_vel; } 

##### Share on other sites
Hmm. What is the significance of the number 220?

##### Share on other sites
it is the top or bottom of the screen

##### Share on other sites

here is the code I am using
 if(right_paddle>=-220) { right_paddle=-right_paddle_vel; } if(right_paddle<=220) { right_paddle+=right_paddle_vel; } 

If your paddle position is greater than -220, it's position will be set to -right_paddle_vel. I'm going to assume right_paddle_vel is less than 220, so it seems as if it will be stuck there.
Otherwise, we will keep moving it at a speed of right_paddle_vel.

Maybe you want to do this.

 if(paddle_y > ball_y){ paddle_y -= vel; } else if(paddle_y < ball_y){ paddle_y += vel; } 

where vel is some positive number. Edited by incertia

##### Share on other sites
m = (y2 - y 1) / (x2 - x1)
All you need is two points, the starting position (x1 + y1) and the ending position (x2 + y2). However, it's pretty easy if you know the velocity to find another point, put it in here, and then use either y = mx + b or y1 - y = m(x1 -x) To find anothe point. Just use some basic math to calculate the ending position of the ball, then move your computer paddle at a predetermined speed to find it (Preferably slower than velx, or else you'll end up having a never ending A.I.)

##### Share on other sites
well I have worked on this problem and have almost finished it.here is the code I am using.
 if(right_paddle > screenHeight || right_paddle < -screenHeight) { right_paddle+=right_paddle; } right_paddle+=5; 

##### Share on other sites
I have managed to get the right paddle to move from the top of the screen to the bottom of the screen but I cant get it to move back from the bottom of the screen to the top of the screen, basically I want some very simple Ai for the computer controlled right paddle.here is the code I am working with.
 if(right_paddle > screenHeight || right_paddle < -screenHeight) { right_paddle+=right_paddle; } right_paddle+=5;