# Stanford CS106A Breakout Game Checking For Collisions Solution

Just when I felt optimistic, like I can conquer the world, I found this to be even harder than all the other parts of the Breakout game to date! I’ve had a pretty hard time finding the answer that I could understand online, so I have to admit that I got a developer friend to help me out a bit, which I am really really grateful for. Here is the next step in the Breakout game:

Here is my solution:

Ok, so while the instructions were helpful, I think they were also confusing, especially the “private GObject getCollidingObject()” and the “GObject collider = getCollidingObject()” parts. I put the GObject collider outside of any method, and that cause some serious bug that I couldn’t figure out how to fix! The other confusing part is figuring out that “collider == brick” only returns the last brick in your brick wall, so to get the brick you need to say “if (collider != null)”.

Anyway, I don’t think I could have figured this out without some help from my friend. Here is my solution, also available on gist.

```private void moveBall() {
ball.move(vx, vy);
//check for walls
//need to get vx and vy at the point closest to 0 or the other edge
if ((ball.getX() - vx &lt;= 0 &amp;&amp; vx &lt; 0 )|| (ball.getX() + vx &gt;= (getWidth() - BALL_RADIUS*2) &amp;&amp; vx&gt;0)) {
vx = -vx;
}
//We don't need to check for the bottom wall, since the ball can fall through the wall at that point
if ((ball.getY() - vy &lt;= 0 &amp;&amp; vy &lt; 0 )) {
vy = -vy;
}
//check for other objects
GObject collider = getCollidingObject();
vy = -vy;
}
//since we lay down a row of bricks, the last brick in the brick wall is assigned the value brick.
//so we narrow it down by saying that the collier does not equal to a paddle or null,
//so all that is left is the brick
else if (collider != null) {
remove(collider);
vy = -vy;
}
pause (DELAY);
}

private GObject getCollidingObject() {

if((getElementAt(ball.getX(), ball.getY())) != null) {
return getElementAt(ball.getX(), ball.getY());
}
else if (getElementAt( (ball.getX() + BALL_RADIUS*2), ball.getY()) != null ){
}
else if(getElementAt(ball.getX(), (ball.getY() + BALL_RADIUS*2)) != null ){
}