1

I’m having some problem with the movement and collisions of the ball. I’ve searched for answers here but I still cannot figure out how to do what they suggested.

  1. The ball gets stuck in the paddle when the paddle hits the side of the ball. Someone suggested that I should do:

    if (ball moving down) 
    {
        bounce ball up
    }
    

    I did try with velocity, getY(ball), gety(paddle) with many different combinations, but either the ball still gets stuck or the paddle disappears when the ball hits it.

  2. The ball does pause sometimes when it hits the bottom edge of the screen, waits for my click, then starts over again.

    However, if I move the paddle (without any clicks, I’m sure), whilst the ball is going down in the gap between the paddle and the bottom edge, then, the ball just starts over and falls right away, waitForClick doesn't work in that case.


  3. Also, I cannot decrease lives. I’ve tried lives = lives - 1, and lives-- when ball hits the bottom, but I still can play more than 3 times. lives is set with 3, and I haven’t changed anything about it.

Any suggestion will be much appreciated!

Thank you so much for your time!

Update: 08/04/2015 Please help. Since I posted the question here, I've been attempting to fix the problems but I'm still stuck.

Update: 03/04/2015 Thank you so much for your answers, guys! Here's my original code, it complies and runs, I've removed parts that don't work but it still has the problems mentioned above:

// click to start game  
waitForClick();

// instantiate balls' velocities
double velocityX = drand48() + 0.5, velocityY = drand48() + 2.5;

// START PADDLE MOVE ----------------------------------

// paddle follows mouse forever
while (true)
{
    // check for mouse event
    GEvent event = getNextEvent(MOUSE_EVENT);

    // if we heard one
    if (event != NULL)
    {
        // if the event was movement
        if (getEventType(event) == MOUSE_MOVED)
        {
            // ensure middle of slider follows cursor
            double x = getX(event) - getWidth(paddle) / 2;
            double y = 540;
            setLocation(paddle, x, y);

            // stop at right edge of window
            if (getX(paddle) + getWidth(paddle) >= getWidth(window))
            {
                x = getWidth(window) - getWidth(paddle);
                setLocation(paddle, x, y);
            }
            // stop at left edge of window
            else if (getX(paddle) <= 0)
            {
                x = 0;
                setLocation(paddle, x, y);
            }
        }
    }
    // END PADDLE MOVE -------------------------------------

    // START BALL MOVE ----------------------------------------

    // move ball
    move(ball, velocityX, velocityY);

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

    // bounce off top edge of window
    else if (getY(ball) <= 0)
    {
        velocityY = - velocityY;
    }          
    // hits the bottom edge of window
    else if (getY(ball) + getHeight(ball) >= getHeight(window))
    {
        waitForClick();
        setLocation(ball, 190, 290);
    }

    // linger before moving again
    pause(10);
    // END BALL MOVE --------------------------------

    // DETECT COLLISION -----------------------------------
    GObject object = detectCollision(window, ball);

    if (object != NULL)
    {
        // ball hits paddle
        if (object == paddle)
        {
            velocityY = - velocityY;
        }
        // ball hits bricks
        else if (strcmp(getType(object), "GRect") == 0)
        {
            velocityY = - velocityY;
            GRect bricks = object;
            removeGWindow(window, bricks);
        }
    }
    // END DETECT COLLISION -------------------------------------
}
return 0;
// END TODO
5
  • In Point #1, your If block looks correct. The only thing I had that could be different is you have a space between your "-" and velocity which I do not have in mine...minor issue and most likely won't affect the outcome. Problem is more likely in your implementation of detectCollision. Commented Apr 2, 2015 at 4:16
  • My guess for the ball getting stuck in the paddle would be the ball is not clearing that paddle after a collision. There's more than one way to solve this but the most simplistic is that your velocity is too slow. Basically your ball bounces up when it hits the side, but is still overlapping the paddle when it checks for a collision again (since the ball started lower) it then reverses velocity and ends up back where it started which is in the paddle so it bounces again. This loop would cause a "stuck ball". Commented Apr 2, 2015 at 4:49
  • Probably a more complete version of your code would be better! Also make sure you format your post well per How do I format my posts?!
    – kzidane
    Commented Apr 2, 2015 at 15:56
  • Hi guys. I know you're busy, I hope you could spend some minutes to look at my code above and tell me what I did wrong. I really want to make it work like you guys did. Thank you so much! Commented Apr 8, 2015 at 3:36
  • @Kareem Hi. I hope you could take a look at my code above and give me some ideas to solve my problems. Thank you so much! Commented Apr 8, 2015 at 14:28

2 Answers 2

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)
{
    remove object
    // some code
}

if you were doing that, then the only case in which the else if block is executed is when the first condition is evaluated to false. My guess is that

  1. part of the ball dives into the paddle
  2. the first condition evaluates to true
  3. the velocity on the y-axis changes to be negative
  4. the ball moves up (but still part of it collides with the paddle)
  5. the first condition evaluates to false because the velocity on the y-axis is negative
  6. the the second condition (i.e., the else if) evaluates to true because paddle is still a GRect, hence the body is executed and the paddle gets removed

The obvious fix is to write it like that

if (object is paddle)
{
    if (ball moves down)
    {
        move the ball up
    }
}

Apparently you omitted the part of your code that is relevant to the second and the third points. You may update your question and notify me in a comment!

1
  • Thank you! Error 1 is fixed! You're my saviour! And thank you for correcting my post too! Commented Apr 10, 2015 at 9:22
0

The funny thing is I've found out that the staff's breakout has the same error like bullet 2 I wrote above. It must be a glitch somewhere above my coding skill.

You must log in to answer this question.

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