0

On Pset4 "bounce" Can anyone please help me in bouncing the ball at angles after it touches the window edges and paddle?! I mean is what all the math computation involved in it?? a pseudocode highly recommended... Thanks in advance

1

It's not too bad on the math side. In summary, you just reverse the values of the x and y velocity when it contacts the object.

For instance, if the ball hits the right edge of the window.

the x-velocity variable (controlling up and down motion) would remain unchanged while the y-velocity variable would be reversed;

//if collision between ball and right edge of window
   yvelocity = -yvelocity

In the case of the paddle or a brick, you would modify the xvelocity

//if collision between ball and brick
  xvelocity = -xvelocity
  yvelocity = -yvelocity

The part that takes a little adjustment on your part is to figure out how to modify the xvelocity just slightly every time the ball contacts the paddle so that there is a bit of randomization and that the user doesn't get stuck in an endless loop with a ball that won't hit certain spots. You can try using a random number as a multipler and adding a constant to it.

  //if collision between ball and paddle
      xvelocity = (-xvelocity * drand48()) + some constant number
      yvelocity = -yvelocity

The formula is really personal preference and can be adjusted as you see fit to make the game more or less difficult and unpredictable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .