# Stanford CS106A Breakout Game Sticky Paddle Glued Ball Solution

10 Nov

Just when I thought I was almost done, there was an additional complication in the Breakout game! As outlined in Assignment 3, Finishing Up includes making sure the ball doesn’t get “glued” to the “sticky” paddle if you move the paddle really fast as the ball is approaching: Of course, this was happening to my ball and paddle, so I set out on a mission to fix it! Here is my solution:

I first used a “trick” that my developer friend showed but was unfortunately not covered in class. To see what was happening when the ball got “glued” to the paddle, I asked the Console (below your code in Eclipse) to print out the velocity and coordinates of the ball when the ball collided with the paddle by just adding a println command:

println (“vx: ” + vx + “, vy: ” + vy + “, ballX: ” + ball.getX() + “, ballY: ” +ball.getY());

After playing the Breakout game until I got the ball to stick to the paddle, I looked at the results in the Console, and found that sometimes the ball went “below” the paddle surface, so as it was going up, it was still colliding with the paddle, so the velocity kept changing by -1, keeping the ball on the paddle. I also noticed that the Y coordinate of the ball is counted in segments of 4, instead of 1. So to get the ball to only change velocity at one point of collision, instead of two, you just need to add a simple if statement.

The most common point of collision between the ball and the paddle is when the y coordinate of the ball is equal to the height of the paddle top plus the ball height (ball radius * 2). However, since the program counts in increments of 4 instead of 1, you have to get the range for that number, not the exact number. So I set that value to be higher or equal to the height of the paddle top plus the ball height but less than height of the paddle top plus the ball height – 4. Here is my code, which you can also find on gist:

```<pre>
if (collider == paddle) {
/* We need to make sure that the ball only bounces off the top part of the paddle
* and also that it doesn't "stick" to it if different sides of the ball hit the paddle quickly and get the ball "stuck" on the paddle.
* I ran "println ("vx: " + vx + ", vy: " + vy + ", ballX: " + ball.getX() + ", ballY: " +ball.getY());"
* and found that the ball.getY() changes by 4 every time, instead of 1,
* so it never reaches exactly the the height at which the ball hits the paddle (paddle height + ball height),
* therefore, I estimate the point to be greater or equal to the height at which the ball hits the paddle,
* but less than the height where the ball hits the paddle minus 4.
*/

if(ball.getY() &gt;= getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT - BALL_RADIUS*2 &amp;&amp; ball.getY() &lt; getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT - BALL_RADIUS*2 + 4) {
vy = -vy;
}
}
</pre>
```

### 2 Responses to “Stanford CS106A Breakout Game Sticky Paddle Glued Ball Solution”

1. fsfsf April 26, 2012 at 3:07 pm #

if(ball.getY() >= getHeight() – PADDLE_Y_OFFSET – PADDLE_HEIGHT – BALL_RADIUS*2 && ball.getY() < getHeight() – PADDLE_Y_OFFSET – PADDLE_HEIGHT – BALL_RADIUS*2 + 4)
WTF???!?

2. Eva June 20, 2012 at 8:32 am #

Dude,

```if (collider == paddle)
{
vy = -1 * Math.abs(vy)
}
```

Duh.