0

Something rather strange is taking place while I test my collision detection for the ball hitting the walls. The code below to my eye seems correct to make the ball bounce off of all 4 walls of the window continuously, but it doesn't. The ball immediately hits the right wall, bounces, hits the bottom wall, bounces, and then goes straight through the left one. GDB showed that the code is noticing the ball is out of bounds and recalculates it's velocity, but the ball just keeps going regardless of those changes which is odd because the loop does then rerun to issue the move function. I did try changing both the way it checked for being out of bounds, and even tried hard coding a distinct negative integer for a new velocity, neither of which worked.

// initial velocity
int xvelocity = drand48() + 3;
int yvelocity = drand48() + 3;

// keep playing until game over
while (lives > 0 && bricks > 0)
{
    // did mouse do something?
    GEvent event = getNextEvent(MOUSE_EVENT);

    // if it did
    if (event != NULL)
    {
        // was it movement?
        if (getEventType(event) == MOUSE_MOVED)
        {
            // make paddle follows cursor movement
            double x = getX(event) - pwidth / 2;
            setLocation(paddle, x, 550);
        }
    }

    // move circle along x-axis and y-axis
    move(ball, xvelocity, yvelocity);

    // bounce off right edge of window
    if (getX(ball) + getWidth(ball) >= getWidth(window))
    {
        xvelocity = drand48() + 3;
        xvelocity = -xvelocity; 
    }

    // bounce off left edge of window
    if (getX(ball) <= 0)
    {
        xvelocity = drand48() + 3;
        xvelocity = -xvelocity;
    }

    // bounce off bottom edge
    if (getY(ball) + getHeight(ball) >= getHeight(window))
    {
        yvelocity = drand48() + 3;
        yvelocity = -yvelocity;
    }

    // bounce off top edge
    if (getY(ball) <= 0)
    {
        yvelocity = drand48() + 3;
        yvelocity = -yvelocity;
    }

    // linger before moving again
    pause(10);
}
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 then negate it. The net effect is that after hitting a side wall, the ball goes left and after hitting the top or bottom (assuming the kill routine isn't enabled), it will go up.

Try removing the drand() line of code from each of the collision detections and simply negate the x or y velocity that needs to be changed. If you're trying to randomly change the absolute value of the velocity, then you need to preserve the sign of the original velocity, reverse it, and apply it to the new velocity, but that's an exercise for you to figure out. ;-)

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

| improve this answer | |
  • Ahhhh of course. It's always something simple that I seem to miss. Sorry about that. Perhaps not quite the solution you expected, I actually fixed it by making the generated values from the top and left already negative when they are generated, since the ball will only ever be travelling down from the top or right from the left. As always, thank you for the assistance. – OddballGreg Sep 23 '15 at 17:00

You must log in to answer this question.

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