Archived

This topic is now archived and is closed to further replies.

FlorianapoliS

Pong Question

Recommended Posts

Hi, if someone could tell me how to work out what angle the ball bounces off the paddle in pong it would be a great help. I track the angle in which the ball is travelling eg. if its going straight across the screen to the right it would be 90 degrees, and if it was going to the left it would be 270 etc... Although I seem to have a problem of working which way it is going to bounce off the paddle, so if some could help me I would be greatly appreciative.

Share this post


Link to post
Share on other sites
To write a pong game, you don''t need the angle. For example, if you want the ball to bounce in the opposite direction & angle, just reverse the velocity:

if velocity = dx;

dx = -dx;

Share this post


Link to post
Share on other sites
Yeah, but what if you want to change the balls angle of motion? Let''s say the ball comes in at a 45 degree angle and then due to the way it hits the paddle it bounces off at a 90 degree angle. The ball isn''t just going to bounce back and forth, so the angle is going to have to change more than just its direction.

Here''s what I''d suggest. When figuring out the balls speed and direction take into account the following factors: speed and direction of the ball, speed and direction of the paddle, and possibly where on the paddle the ball hits.

Example 1: ball is moving at 1/2 max. vel. at 45 degree angle moving from top right to bottom left of screen. The paddle is moving at 1/2 max. vel. towards the top of the screen. The ball strikes the paddle towards the top end. Result - the ball travels at 1/2 max. vel. at 35 degree angle from the x axis moving towards the bottom right of the screen.

Example 2: ball is moving at 1/3 max. vel. at 65 degree angle from the x axis moving from top right to bottom left of screen. The paddle is moving at 2/3 max. vel. towards the bottom of the screen. The ball strikes the paddle towards the bottom end. Result - the ball travels at 1/2 max. vel. at 75 degree angle from the x axis moving towards the bottom right of the screen.

Basically, if the ball and paddle are moving in the same y direction the balls speed increases and the angle to the x-axis increases. If they are going in opposite directions the opposite is true. If you decide to have the location of the strike affect ethe ball then I''d suggest striking the end at the direction of motion increases velocity and angle changes, center has no effect, other end decreases velocity and angle changes.

You''ll have to work out the details yourself. If any of my explaination was unclear feel free to ask for clarification. I hope that helps. Good luck.

Share this post


Link to post
Share on other sites
Thanks for that. I understand what your saying, its just i don''t know how to put it into code. I made it so that it detects where the ball hits on the paddle eg. top, center etc..., but I don''t know how to make angle of motion for the ball correct?

Share this post


Link to post
Share on other sites
nanobyte waz on the right track... lets say you have two variables that hold the vertical velocity and the horizontal velcoity (ie if the hvariable was 5 and the vvariable was 5, the ball would move at a perfect up-right diagonal. if it hits a wall, depending on whether it''s the two walls on the left and right sides or the two on the top on bottem, one of these variables should be changed to negative. here''s some code that will also react to wall colisions:
int vspeed; // Vertical speed
int hspeed; // Horizontal speed
...
// collison found
...
if(collision= vert) // the two walls on the right and left
{
hspeeed = -hspeed;
}
else if(colisson = hztal) // top and bottom walls and paddles
{
vspeed = -vspeed
}

UpdateBallPosition();
etc...

hope this helps!
~~KaMiKaZ

Share this post


Link to post
Share on other sites
What do you mean correct? Certainly you are not trying to model correct physics in a pong game? Make something up. When I wrote pong I didn''t have different angles. Sure the game was a little too simple, but if you want different angles, you can either fudge or try some trigonometry.

Share this post


Link to post
Share on other sites
KaMiKaZ, your method looks like it''ll work fine, but its a little too simple. You need some way to increase or decrease the speed too. I wouldn''t suggest actually trying to model correct physics. Just decide under what circumstances you want the speeds to increase or decrease and put in reasonable values.

I''m pretty sure my previous method will work fine, but it might be a little complicated. KaMiKaZ''s looks simpler and I think it would only take a little tweaking to allow for angle changes. You don''t necessarily have to worry about the angles. If, let''s say, you want to decrease the angle to the x axis, just increase the horizontal velocity.

Share this post


Link to post
Share on other sites
yeah, it is a little simple. plus, if that''s all that is used, the ball will hit the same four spots every time! there is also some other modifications... so here''s some code modifications:

int hdirect,
vdirect;

float speed,
temp;
...
// collison found
...
if(collision == vert) // the two walls on the right and left
{
hdirec = -hdirec;
temp = rand{}%11 - 5; // temp is a value from -5 to 5
hdirect = hdirect + temp; // direction is randomized a little
temp = 0; // resets temp
temp = rand()%6 + 1;
temp = temp / 10; // temp can equal from 0.1 to 0.6
if(speed + temp !< MAX_SPEED) // if result isn''t over max speed
{
speed = speed + temp; // adds a value from 0.1 to 0.6 to
temp = 0; // the speed
}
}
else if(collison == hztal) // top and bottom walls and paddles
{
vdirect = -vdirect;
temp = rand{}%11 - 5; // temp is a value from -5 to 5
vdirect = vdirect + temp; // direction is randomized a little
temp = 0; // resets temp

}



UpdateBallPosition();
etc...

this is pretty straightforward code, and should work pretty well. enjoy!
~~KaMiKaZ~~

Share this post


Link to post
Share on other sites