I used the code given in the set

GObject object = detectCollision(window, ball);
if(object !=NULL)
  if(object== paddle)

else if (strcmp(getType(object), "GRect") == 0) { removeGWindow(window, object); y_velocity = -y_velocity; } }

after that I check to see if it is bouncing off the sides...

This is all within the while loop, but when I run the breakout.c, the ball doesn't bounce / deflect until it reaches the bottom of the window. :(

Oddly enough, the code to detect for GRects (if(strcmp(getType(object), “GRect”)==0)) does bounce off the bricks at the top. I got rid of the (object==paddle) part of the code and only checked for GRects (and made the y-velocity change), but the ball still didn't deflect from the paddle, even though it still did for the bricks?? It’s like the code doesn't know that the paddle is an GObject / GRect? Any suggestions??


  • It sounds like the "bounce off the wall" code is working fine, but not the "bounce off an object" code. Since thecode following the detectCollision(window,ball) line isn't posted, what do you do with the result that is stored in object? detectCollision doesn't affect the velocity or direction of the ball, it just tells you what it hit. Are you taking action based on what you hit? If so, what? If this doesn't get you going, can you please post the code that follows, the code that controls the actions of the ball when a collision is detected? – Cliff B Apr 5 '15 at 4:02
  • Are sure you're returning the paddle from initPaddle? 'cause it seems like paddle (in main) doesn't hold the correct value. – Kareem Apr 5 '15 at 5:55
  • That would make sense, but I’m pretty sure I am. GRect initPaddle(GWindow window) {GRect paddle = newGRect(175, 575, 100, 10); setColor(paddle, “GREEN”); setFilled(paddle, true); add(window, paddle); return paddle;} The code up at the top right underneath main hasn’t been edited. Any other advice?? I suppose I can just submit it and see if it works for the Harvard folks?? Thanks! – kames Apr 7 '15 at 6:05

I had this problem before, and I did exactly what you tried to fix it. The part about your paddle is (somewhat) correct. (Somewhat because it has nothing to do with the ball bounces off the paddle or not, it is another issue)

Your issue lies somewhere in the lower part with (strcmp(getType(object), "GRect") == 0). Perhaps you should post that part up here so people could give you some ideas how to fix it.

| improve this answer | |
  • I added the strcmp(getType.... code up at the top. Thanks for your help! – kames Apr 16 '15 at 4:05
  • @kames I recommend you use if(object== paddle) for the paddle only and use (strcmp(getType(object), "GRect") == 0) for the bricks – Viet Nguyen Apr 17 '15 at 7:00
  • I thought that’s what I did? In my initial comment some of the code wasn’t highlighted, but first I checked for the paddle and then I checked for the objects (which would be bricks). – kames Apr 24 '15 at 4:36

You must log in to answer this question.

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