0

Can someone please explain to me how to detect collision with another image? The tutorial I was watching explained how to detect if a collision occurs between two rectangles but I would like to use two images instead. Please see the code below.

public void paintComponent(Graphics g){ 
//g.drawImage(yellowBall, xCoor, yCoor, this);
Rectangle r1 = new Rectangle((boardXSize/2), (boardYSize/2),50, 50);
Rectangle r2 = new Rectangle(200, (250),50, 50);
g.setColor(Color.red);
g.fillRect(r1.x, r1.y, r1.width, r1.height);
g.setColor(Color.blue);
g.fillRect(r2.x, r2.y, r2.width, r2.height);
if(r1.intersects(r2)){
 System.out.println("Collision Detected"); 
 }
repaint();
}

How can I detect if r1 intersects yellowBall instead of r2? Please keep responses at the beginner level but of course, any assistance is greatly appreciated. Thanks!!

asked Jun 18, 2014 at 23:28
0

3 Answers 3

1

When you use rectangles to detect collisions, it is assumed that the rectangle contains the object you're testing for. The more closely the rectangle encloses the object, the more accurate the collision detection is. Any part of the object outside the rectangle doesn't get tested for collision.

So your real question may be, "how do i draw a rectangle around my circle?"

In your case, the ball presumably starts at (xCoor,yCoor) and extends out from there according to the size of the image.

answered Jun 18, 2014 at 23:57
2
  • Good work with the teaching approach, rather than the telling approach. Commented Jun 19, 2014 at 0:51
  • why does that matter? Why not both? Commented Jun 21, 2014 at 19:14
1

If you are good with trigonometry here is my suggestion on collision detection for a ball and a rectangle:

find out the distance between the 2 centers. from here on i will call this the "connection"

if the distance is greater than ball.radius + rect.diagonal/2 then they don't intersect

if the distance is smaller than either ball.radius or rect.height/2 then they are guaranteed to intersect.

else use trigonometry to calculate the connection segment from the rectangle's center to its perimeter. if ball.radius + segment < connection then they don't intersect, if it is = then they touch each other, and if> then they intersect

This should work with any shape actually, as long as it is on a 2D plane. in 3D things will get much more complex.

Also this algorithm will always give you an almost perfect (i.e. computing round up errors, which would be minimal) solution but it will not be very fast, so if you are writing something like a game, it's better off to just create a collision circle using diagonal/2 as radius around the rectangle, it will be much faster but inaccurate.

answered Jun 19, 2014 at 2:44
0

To continue the approach begun by tylerl, this is typically done in two stages.

The first is to create a "bounding box" that surrounds each object, and test whether any two boxes intersect. Testing if boxes intersect is quick.

The second stage is employed when we know the boxes intersect. If they don't intersect, then we've finished. To test whether the two objects intersect is in general harder, but you have chosen one of the "not too hard" cases.

If you had two circles, you would just calculate the distance between their centers. Are your geometry skills good for that?

But you have a slightly harder case, a rectangle and a circle. Using geometry, how can we determine that part of the rectangle overlaps?

answered Jun 19, 2014 at 1:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.