0

My detect collision or detect walls algorithm does not seem to be working. The ball does not seem to be bouncing off the walls or detecting objects. What's annoying is that I was able to get the ball to bounce randomly on bounce.c but I can't recreate it in breakout.

Here is the code,

   double heightvelocity = lrand48() %2+2.0; //random number between 2 and 3

  double widthvelocity = (3.0-heightvelocity);  //random number between 1 and 3

   // move circle along x-axis

        move(ball, widthvelocity, heightvelocity);

        GObject object = detectCollision(window, ball);

        if (object != NULL)
        {
            if (object==paddle)
            {
                heightvelocity= -heightvelocity;
            }
    /*left side of ball has been touched right side of object or right side of the ball has been touched left side of object*/
            else if  (getX(ball) == getX(object)+getWidth(object)|| getX(ball)+getWidth(ball) == getX(object))
            {
                widthvelocity = -widthvelocity;
            }
      //top or bottom side of ball had been touched by the bottom or top side of ball
            else if (getY(ball)== (getY(object)+getHeight(object))|| (getY(ball)+getHeight(ball))== getY(object))
            {
                heightvelocity = -heightvelocity;
            }

        }

    // bounce off bottom edge of window
        else if (getY(ball) + getHeight(ball) >= getHeight(window))
        {

           lives--;
        //put ball back at start.  Wait for click// 
        }

    // bounce off left edge of window
        else if (getY(ball) <= 0)
        {
            heightvelocity = -heightvelocity;
        }

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

    // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            widthvelocity = -widthvelocity;
        }



    // linger before moving again
    pause(5);    
    }

2 Answers 2

1

Try to change the == in the object detection section with either >= or <=

1

One thing that had me for a bit was my functions for instantiating the paddle and the ball.

since we have to program movement inside of main we want ball and paddle exist inside of that function.

make sure your initPaddle and initBall RETURN paddle and ball respectively.

That might help with some of the issues you are having with movement.

You must log in to answer this question.

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