problems with first game
June 24, 2004 10:56 AM
im making a pong clone for my first game in c++ and opengl. ive got all of the things down but i have to questions.
question 1: how do i make the computer player go for the ball when it is going to his side?
question 2: how do i determine what direction the ball goes when hits the wall or paddle at a certain angle?
thanks in advance
Quote: Original post by Anonymous Poster
im making a pong clone for my first game in c++ and opengl. ive got all of the things down but i have to questions.
question 1: how do i make the computer player go for the ball when it is going to his side?
question 2: how do i determine what direction the ball goes when hits the wall or paddle at a certain angle?
thanks in advance
answer 1: AI
answer 2: maths
answer 1: nothing hard, just make the paddle go to the y-direction till it equals the balls y-position.
answer 2: if it hits a horizontal wall=flip y-direction, if it hits a vertical wall (like the paddle)=flip x-direction.
Ok, I would hope you are utilizing some sort of velocity vectors for your ball movement. So your ball should have a velocity in the x and y direction. Say the paddles are verticle (and therefore can only move up and down) and when the ball is moving to the right it is said to have a positive velocity in the x direction. If the player's paddle is on the left, when the ball's velocity is positive (xVel > 0), the computer's paddle should move to intercept (if (ballXPos > compXPos) compXPos++, or something). This should get the job done but won't look so hot (the computer's paddle will basically mirror the ball's position). Your wall situation is the same. If the ball collides with a wall, you just have to multiply the ball's y velocity by -1.
These simple problems are common to all games and sometimes even the simplest problem can take quite a bit of effort to overcome. You problably could have figured this out, though, as it is not very complex. Next time I recomend you think on it a while before asking. You'll come acrossed the answer. Even if you don't get it right away, keep working at it. Sleep on it! I can't stress that enough. Whenever I come across something I can't solve, I think on it for an hour or two and if I'm still no where, I put it down for the day and I'll be damned if 9 out of 10 times I don't think of the answer the next morning in the shower or on my way to work or something. So while I encourage asking as many questions as you can, when it comes to things like this, try for 2 or 3 days on your own before asking. When you come across the answer, not only will you understand the solution better, but you'll feel alot better about doing it yourself too.
Good Luck!
These simple problems are common to all games and sometimes even the simplest problem can take quite a bit of effort to overcome. You problably could have figured this out, though, as it is not very complex. Next time I recomend you think on it a while before asking. You'll come acrossed the answer. Even if you don't get it right away, keep working at it. Sleep on it! I can't stress that enough. Whenever I come across something I can't solve, I think on it for an hour or two and if I'm still no where, I put it down for the day and I'll be damned if 9 out of 10 times I don't think of the answer the next morning in the shower or on my way to work or something. So while I encourage asking as many questions as you can, when it comes to things like this, try for 2 or 3 days on your own before asking. When you come across the answer, not only will you understand the solution better, but you'll feel alot better about doing it yourself too.
Good Luck!
Without order nothing can exist - without chaos nothing can evolve.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement