1

I'm creating a Breakout game to familiarize myself with Java. Currently I've got everything working but I've noticed that the ball seems to take off in the same direction.

I've programmed it so when the ball hits the paddle the Y velocity is reversed. This works, but the ball seems to follow the same route...

How would a make the collision seem more realistic?

When the ball hits the left side of the paddle, I want to redirect it back to the left but I'm not sure at which angle to reflect it. Can anyone provide a formula to help me work out the angle at which I need to direct the ball upon contact with either the left or right side of the paddle?

user3422952
  • 319
  • 2
  • 8
  • 17
  • Could you show us your collision code? It might be easier to see what you're asking with it. – user3507600 Apr 16 '14 at 14:38
  • possible duplicate of [Ball to Ball Collision - Detection and Handling](http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling) – Michael Apr 16 '14 at 14:39
  • 1
    I suspect the question is about how to allow the user to vary the direction a bit rather than simply reversing the Y direction when hitting the paddle. Otherwise, the ball just gets into a simple pattern and it is difficult or even impossible to hit some of the bricks. I enountered the same issue decades ago on an Apple ][ using Integer BASIC. – Fred Larson Apr 16 '14 at 14:42
  • @FredLarson - that's exactly what I mean. Could you provide your solution (even though it was decades ago)? – user3422952 Apr 16 '14 at 15:48
  • @user3422952: I don't think I ever resolved it back then. With 40x40 lo-res graphics, there weren't a lot of good options for ball direction. The ball was literally one big fat pixel. Anything other than horizontal, vertical, or diagonal motion and the ball would look like it was really climbing stairs. Modern screen resolutions offer better possibilities. See my answer for a suggestion. – Fred Larson Apr 16 '14 at 15:55

4 Answers4

1

If the ball hits the paddle from the top, negate Y velocity.

If the ball hits the paddle from the side, negate X velocity.

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
0

Reversing the vertical velocity is the correct thing to do for a perfectly elastic collision, which is what you want in a breakout game (the ball keeps it's total velocity after the collision).

The problem of following the same route can be avoided either by randomizing the initial ball position and velocity, or by changing the collision dynamics to be unrealistic. You could either add a small random component to both coordinates (taking care to keep the total velocity the same), or send the ball at a different angle, depending on where it hits the pad (which is what many games of this type do).

To do the later, you need to calculate the absolute position where the ball hits the pad, normalize it (by subtracting the pad position and dividing by the pad length), and map it into a angle range (maybe -80 to 80 degrees). You can then calculate the desired horizontal and vertical velocities using trigonometric functions.

loopbackbee
  • 21,962
  • 10
  • 62
  • 97
0

One idea might be to use motion of the paddle to vary the direction a bit. Say, if the paddle is moving to the right when hitting the ball, change the X velocity of the ball to the right a little in addition to reversing the Y direction. That provides a way for the user to actually use some skill to put the right "English" on the ball.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
0

There are two separate problems involved, a design problem and the physics problem. In the end, the physics problem might have the easier solution:

  1. Compute the collision point on the paddle
  2. Compute the instantaneous horizontal velocity pvx of the paddle
  3. Compute the normed outward surface normal (nx,ny) at the impact point. This is the design decision, in the middle part of the paddle one probably wants (nx,ny)=(0,1), while on the left side (nx,ny)=(-1,0) or generally in WNW direction, on the right side correspondingly (nx,ny)=(1,0) or in the general ENE position.
  4. Use the physics formula for a collision with a moving infinitely massive object to compute the new velocity vector (vx,vy):

    dot = nx*(vx-pvx)+ny*vy
    vx = vx - 2*dot*nx
    vy = vy - 2*dot*ny
    

    One may check if the dot product dot is positive to avoid spurious collision computations if paddle and ball do not separate fast enough after collision.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51