# intersects method

• March 30th, 2013, 03:50 PM
game06
intersects method
Attachment 1969

here is what i trying to do and having no luck with it.

1st check if ball and rectangle are touching
2nd :
if ball intersects rect from top than i want ball to go up.
if ball intersect rect from bottom than i want ball to go down.
if ball intersect rect from left than i want ball to go left.
if ball intersect rect from right than i want ball to go right.

1st part is easy. i'll i have to do is set up a getBounds() method on ball and rect. this is the red box u see on image. than test using intersects() method if two object are touching each other.

2nd part i have no idea how to do. the intersects() method doesnt tell you if ball is touching the rectangle from top,bottom,right,or left. this is the problem!

:confused:
Code :

```if(this.getBounds_ball().intersects(this.getBounds_box())) { //How to test where the ball is touching the box!!! }```
plz any ideas? every one i ask seem to explain to me how to test is two object are touching each other. BUT THIS IS NOT THE PROBLEM :mad: i already know how to do this. i need help with 2nd part. not 1st part.

any ideas? plz let me know.
• March 30th, 2013, 04:04 PM
Norm
Re: intersects method
Quote:

. the intersects() method doesnt tell you if ball is touching the rectangle from top,bottom,right,or left.
You'll have to code that. Look at the locations of the centers of the shapes.
• March 30th, 2013, 04:25 PM
game06
Re: intersects method
ops u can igore the white line in image. i wasnt sure what i was doing there. but to find middle of shapes.

Quote:

(x+width/2)
i am sure how to get started bc i do not even know what is the best approach.
• March 30th, 2013, 04:28 PM
Norm
Re: intersects method
Take a piece of paper and draw the shapes in the different positions and see what the relationships are between their centers.
• March 30th, 2013, 04:29 PM
game06
Re: intersects method
i am lil confuse how will that help me to find:

Quote:

if ball intersects rect from top than i want ball to go up.
if ball intersect rect from bottom than i want ball to go down.
if ball intersect rect from left than i want ball to go left.
if ball intersect rect from right than i want ball to go right.
• March 30th, 2013, 04:35 PM
Norm
Re: intersects method
Look at the values for the x,y locations of the center of each of the shapes drawn on paper.
For example:
when one shape is above the other, what are their x and y values?
when one shape is to the left of the other, what are their x and y values?
• March 31st, 2013, 01:06 AM
game06
Re: intersects method
if some one is having same problem as i do, here is a solution. i am not sure if this is the best way but it seem to be working for me. hope it helps.

Code :

```if(this.getBoundsBall().intersects(this.getBoundsRect())) {   if(ballY+ballHeight >= rectY && ballY <= rectY) { System.out.println("collision from top"); } if(ballY <= rectY+rectHeight && ballY+ballHeight >= rectY+rHeight) { System.out.println("collision from bottom"); } if(ballX+ballWidth >= rectX && ballX <= rectX) { System.out.println("collision from left"); } if(ballX <= rectX+rectWidth && ballX+ballWidth >= rectX+rectWidth) { System.out.println("collision from right"); } }```