0

I have a ball that's moving up and down, bouncing off the window's edges. The bricks and pedal are also implemented. However, the detection collision function doesn't work as intended. It never returns NULL, even if the ball doesn't collide with anything.

So I went into the function itself to try something and I changed the lines from

// check for collision at ball's top-left corner
    object = getGObjectAt(window, x, y);
    if (object != NULL)
    {
        return object;
    }

to

// check for collision at ball's top-left corner
    object = getGObjectAt(window, x, y);
    if (object != NULL && strcmp(getType(object), "GOval") != 0)
    {
        return object;
    }

and it worked and returned NULL when there was no collision, unlike before when it never returned NULL. So basically the function thinks the ball is constantly colliding with itself.

I can't imagine the staff missed this one. So I'm guessing I am doing something wrong? This is the relevant part of my main function.

GObject object = detectCollision(window, ball);
if (object != NULL)
{
    //TODO
    printf("not Null\n");
}
else
    printf("Null\n");

Before changing detectCollision() myself, it always returned object thus printing out "not Null". Even if there was no collision.

1

Please, don't go changing the detectCollision function that works perfectly fine as it is.

Have just tried it myself, without any modifications to the detectCollision function. I have lots of "NULL" printed on my screen, and "NOT" printed only when the ball collides with something.

So, my guess, is that you changed something else in the code that prevents the function from working correctly . Maybe you're initializing your ball wrong and have more than one ball (both at the same spot). Or maybe it's something else.

The function, as it comes in the distribution code, works fine

| improve this answer | |
  • That's what I figured. I just edited it so see if it fixes the bug so that I know what the reason for it might've been. This is my code for the ball: GOval initBall(GWindow window) { GOval ball = newGOval((WIDTH - 50) / 2, (HEIGHT - 50) / 2, 50, 50); setFilled(ball, true); setColor(ball, "BLACK"); add(window, ball); return ball; } – Tiku Sep 2 '15 at 2:43
  • That looks ok, but is there any chance that you're actually calling the function more than once? You know, maybe the function call got copied twice during some copying and pasting, or something like that? – Irene Sep 2 '15 at 2:50
  • I had some nice fella from reddit check in and he solved it: I called 50 as the width and height of my ball when in fact I should've been using RADIUS (the constant defined at the top). By my code, the ball was always colliding with itself. – Tiku Sep 2 '15 at 2:51
0

Okay, I got it. Accidentally I didn't use the RADIUS constant in my declaration of the ball, instead I used some random numbers. So of course the ball would collide with itself. Good night and thanks :)

| improve this answer | |

You must log in to answer this question.

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