1

When I run the following code the ball bounces just above the paddle (around 10 pixels above the paddle). I haven't messed with the detect-collision function and following are my initPaddle() and code to change direction after hitting paddle

GRect initPaddle(GWindow window)
{

    GRect paddle = newGRect(0, 0, PWIDTH, PHEIGHT);
    setFilled(paddle, true);
    setColor(paddle,"BLUE");
    setLocation(paddle,WIDTH/2 - PWIDTH/2,HEIGHT- PHEIGHT - 50);
    add(window, paddle);
    return paddle ;
}

And the code

else if(collision != NULL)
    {
        string temp = getType(collision);
        //If collision(GObject) hit paddle
        if(collision == paddle)
        {
            velocityY = -velocityY;
        }
        //If collision hit brick
        else if((strcmp(temp,"GRect")==0) && collision != paddle)
        {
             velocityY = -velocityY;
             points++;
             bricks--;
             sprintf(s, "%i", points);
             setLabel(label,s);
             removeGWindow(window,collision); 
        }

Any Help will be appreciated

PS: Here is the complete code http://pastebin.com/JRkYST2g

  • No answers ? :/ – Ibrahim Dec 25 '14 at 7:07
  • I'm not sure if this is the whole problem but it is most likely part of the problem. In your "GRect initPaddle(GWindow window)" function you need something after return. Like maybe paddle? – Steve B Dec 27 '14 at 18:00
  • I had somehow missed return paddle in above code. The original code returns paddle. Everything works fine except bouncing of ball from paddle. I'd really appreciate any help. – Ibrahim Dec 30 '14 at 20:02
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.

| improve this answer | |
  • Thank you so very much. I've been trying to find this error since weeks. Thank you very much. – Ibrahim Dec 31 '14 at 5:21

You must log in to answer this question.

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