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!!
3 Answers 3
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.
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.
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?