0

I'm having trouble getting collisions to work in breakout. I wrote my own code, which despite a lot of fixes didn't work, so I attempted to just copy-paste the bounce.c code in an effort to figure out what was wrong with mine. Bounce compiles and runs normally, but when I use the bounce code (with needed alterations) in breakout, the ball just goes out past the edge of the window (the velocity never reverses). I tried a printf statement to make sure the loop was running and it was, but the ball still doesn't bounce. Here's the relevant portion of the code;

enter image description here

ETA: Here is the entire while loop enter image description here

enter image description here

7
  • Except for the pause, this is exactly the same code that I used successfully. It's likely that the issue is with the surrounding code or with the pause itself (I doubt the latter.) You used a printf to verify that the loop was executing. Have you tried printf statements inside the if and elseif code blocks to see if they are executing, or that you're getting inside those code blocks at all? I'm wondering if the ball is either not bouncing, or if it is bouncing back and forth repeatedly and very fast at the edge of the window. Also, does the ball not bounce off both sides, or just one side?
    – Cliff B
    May 26 '15 at 20:54
  • @CliffB Printf statements inside the if/else if confirm that they're running, but I think it ends up in an infinite loop (ie constantly reversing) because it keeps printing out "left"/"right" depending on which side. This does happen on both sides. It seems strange to me that this would happen with my code but not with the bounce code. I'll add the entire while loop to the original question for you to see. May 27 '15 at 20:12
  • I still can't see anything that would cause what you're seeing, but I have a couple ideas on how to make it work. This looks like some kind of timing issue that is letting the ball get past the window edge. Once it does, it just keeps reversing direction before it can get back inside the window. So, let's apply a little logic to the design to force it back. Think about the sign of the velocity. If it hits the left side of the window, the velocity has to be negative. If it hits the right side, it has to be positive. So, instead of reversing the sign of the velocity, do the following (cont)
    – Cliff B
    May 27 '15 at 21:21
  • When the ball hits the left side, velocity = abs(velocity); and when it hits the right side, velocity = -abs(velocity); (remember #include stdlib.h ) This should kill the infinite loop because it will always force the ball to go back into the window. It won't matter how fast the loops are running because the ball will only reverse direction once at each edge. If this doesn't give you a workaround, let us know what it does. Also, once you get all of the code for breakout finished, you may find that the current code may actually work, due to timing issues being altered by added code.
    – Cliff B
    May 27 '15 at 21:27
  • I tried the abs value code, but the code behaves the exact same way as before. I'm befuddled. May 28 '15 at 17:16
0

I figured it out - thanks to @wujie for answering someone else's question about the same problem. I was initializing velocity inside my while loop, which meant the velocity switch never happened. After moving it outside the loop, the code works as it should

You must log in to answer this question.

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