Hot answers tagged

4

detectCollision assumes that the width of your ball is RADIUS*2. You have initialized your ball with a width of RADIUS. So detectCollision will return a collision before the ball hits the paddle.


2

If GDB isn't helping, maybe try using printf directly before and after the move statement, e.g. printf("before: %.2f, %.2f\n", getX(ball), getY(ball)); move(ball, velocity, vertical); printf("after: %.2f, %.2f\n", getX(ball), getY(ball)); If that is inside your main while loop you should see a continuous stream of output as the ball moves. PS: The ...


2

The ball gets stuck in the paddle when the paddle hits the side of the ball. I'm not particularly sure how exactly you tried to implement if the ball is moving down move the ball up but my guess is that you probably did something like if (ball collides with paddle AND ball is moving down) { move the ball up } else if (object is GRect) { ...


1

Your code isn't reversing the direction after bouncing off a wall, it's setting a specific direction. Look at the following: if (getX(ball) <= 0) { xvelocity = drand48() + 3; xvelocity = -xvelocity; } The initial velocities are always set to drand48() + 3. Each time you detect a collision with a wall, you reinitialize the x or y velocity and ...


1

First consider instead of just getY(ball) to also add two times the diameter of the ball so by the time the bottom of the ball hits the bottom of the screen you count a lost life. Also it's not a good practise to use magic numbers (see 600). Instead use a constant declared at top, and use it in your code. Now aside from that, I don't know what setLocation ...


1

You should use the second approach like other people suggested. In your code: GObject object = detectCollision(window, ball) if (strcmp(getType(object), "GRect") == 0) { velocityY=-velocityY; } You didn't state that the object is the bricks. Find a way to state that and you'll be fine. Good luck.


1

I don't understand what you mean by "If I change the ball_diameter value from 24 to something lower (i.e. 20) I get the same segmentation fault error. That makes me think that it is value-related." You changed the value and you get the same result. That probably means that it is NOT value related. After getting my program to mostly work, I believe your ...


1

If the ball is moving out of the window at the bottom edge, it means that your boundaries are likely not set appropriately. The ball Y coordinate is calculated from the top, left corner of the ball (imagine an imaginary square around the ball, it would be the top left corner of that square). So, to accurately calculate the boundary of the ball where it ...


1

The ball is never moving because you're having an infinite loop at the beginning of the outer while loop while (lives > 0 && bricks > 0) { while (true) { // do something } // CONTROL NEVER REACHES THIS LINE BECAUSE IT NEVER LEAVES THE PREVIOUS LOOP } After you fix this, you'll probably have another similar issue with ...


1

Are you sure the ball isn't moving? What happens if you lose 3 lives, do you call removeGwindow? If not, the ball may in fact have moved already, and you've lost 3 lives so your ball is frozen (unless you did call removeGwindow). Just for debugging purposes, try reducing velocity to something like .005 so you can see if it does move.


1

You don't want the ball to move forever, only when lives>0 and bricks>0. If one of those two conditions are false, your ball should stop moving. Hence, no need to have the forever loop. You want the ball to move in the x-axis AND y-axis, so set a velocity in the x-direction and the in the y-direction. Use the bounce file as a preliminary step to get an ...


Only top voted, non community-wiki answers of a minimum length are eligible