cs106a – Assignment #3 – Extra Task #3

The complete specification of assignment #3 can be found as part of the stream at iTunes.

Improve the user control over bounces

The program gets rather boring if the only thing the player has to do is hit the ball. It is far more interesting, if the player can control the ball by hitting it at different parts of the paddle. The way the old arcade game worked was that the ball would bounce in both the x and y directions if you hit it on the edge of the paddle from which the ball was coming. The web version implements this feature.

First calculate the position where the ball hits the paddle so that -1 signifies a hit on the left corner and +1 a hit on the right corner. Change the horizontal speed so that a hit in the center (0) does not do anything, a hit in the corner from the direction from which the ball arrived changes the direction and a hit on the other corner increases the horizontal speed. Finally to keep the ball from speeding up, adjust the vertical speed so that the overall velocity stayes the same:

			double hitPosition = (2 * (x - paddle.getX()) + d - PADDLE_WIDTH) / (d + PADDLE_WIDTH);
			double vSquare = vx * vx + vy * vy;
			if (vx < 0) {
				vx *= -2 * hitPosition + 1;
			} else {
				vx *= 2 * hitPosition + 1;
			}
			vy = -Math.sqrt(vSquare - vx * vx);

The code for this assignment is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *