First of all I would like to point out that all my values are correct in boxes[5][4] which contains the two x and y values needed for each box. boxes[0][0] and boxes[0][1] are the x and y value for the top left of the rectangle. boxes[0][2] and boxes[0][3] are the x and y vales for the bottom right of the rectangle. This function should see if my ball has hit any of the five boxes. When running my ball just looks like its bouncing off of countless "ghost" boxes and I cannot figure out why. Any help I would be grateful of, and if you need any more code or any more info on the program just ask.

``````void checkbox(){
//check for collision of box one
if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2]){
box[0] = 0;
dirx = dirx * -1;
}
else if (bally1 >= boxes[0][1] && bally1 <= boxes[0][3]){
box[0] = 0;
diry = diry * -1;
}

//check for collision of box two
else if (ballx1 >= boxes[1][0] && ballx1 <= boxes[1][2]){
box[1] = 0;
dirx = dirx * -1;
}
else if (bally1 >= boxes[1][1] && bally1 <= boxes[1][3]){
box[1] = 0;
diry = diry * -1;
}

//check for collision of box three
else if (ballx1 >= boxes[2][0] && ballx1 <= boxes[2][2]){
box[2] = 0;
dirx = dirx * -1;
}
else if (bally1 >= boxes[2][1] && bally1 <= boxes[2][3]){
box[2] = 0;
diry = diry * -1;
}

//check for collision of box four
else if (ballx1 >= boxes[3][0] && ballx1 <= boxes[3][2]){
box[3] = 0;
dirx = dirx * -1;
}
else if (bally1 >= boxes[3][1] && bally1 <= boxes[3][3]){
box[3] = 0;
dirx = dirx * -1;
}

//check for collision of box five
else if (ballx1 >= boxes[4][0] && ballx1 <= boxes[4][2]){
box[4] = 0;
dirx = dirx * -1;
}
else if (bally1 >= boxes[4][1] && bally1 <= boxes[4][3]){
box[4] = 0;
dirx = dirx * -1;
}
}``````

## All 4 Replies

where is the position of the box relative to ? Its center ? Edge ?

This is code absolutely begging for a loop!!

Your actual problem is that you test x then y. That is incorrect. (I think)

Consider a box with coordinates (0,0) and (10,10). I.e it is a square of area 10. Now what happens when you ball is at
(5,23) obviously that misses BUT in your code the first test accept the
point as if only checks the x value and not the x and y simultantiously

You need

``````if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2] &&
bally1 >= boxes[0][1] && bally1 <= boxes[0][3])
{ }``````

Then just put all 5 box test in a loop please!!

The reason that you should use a loop is that in the copy paste you have made a mistake with boxes[4] and boxes[3].
Note: that line 49 says dirx and I am 99% sure that should say diry.
same with line 39.

where is the position of the box relative to ? Its center ? Edge ?

the first x and y value for each box is the top left corner
boxes[0][0] and boxes[0][1]
and the second x and y value is the bottom right corner
boxes[0][2] and boxes[0][3]

This is code absolutely begging for a loop!!

Your actual problem is that you test x then y. That is incorrect. (I think)

Consider a box with coordinates (0,0) and (10,10). I.e it is a square of area 10. Now what happens when you ball is at
(5,23) obviously that misses BUT in your code the first test accept the
point as if only checks the x value and not the x and y simultantiously

You need

``````if (ballx1 >= boxes[0][0] && ballx1 <= boxes[0][2] &&
bally1 >= boxes[0][1] && bally1 <= boxes[0][3])
{ }``````

Then just put all 5 box test in a loop please!!

The reason that you should use a loop is that in the copy paste you have made a mistake with boxes[4] and boxes[3].
Note: that line 49 says dirx and I am 99% sure that should say diry.
same with line 39.

the problem with checking them both at the same time is i cannot update the direction as needed ( the variable dirx and diry )

but I understand my mistake now, thank you I should need no more help now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.