We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Collision detection for rotated rectangles: ideas
Page Index Toggle Pages: 1
Collision detection for rotated rectangles: ideas? (Read 886 times)
Collision detection for rotated rectangles: ideas?
Jul 11th, 2006, 3:13pm
 
I want to perform collision detection on rotated rectangles.

It's easy when the rectangles aren't rotated:

Code:

boolean isOverlapped(Shape s1, Shape s2) { //is Shape 2 overlapping Shape 1

if ((s1.x + s1.w/2) > (s2.x - s2.w/2)) { return true;} //s1 right edge versus s2 left edge
else if ((s1.y + s1.h/2) > (s2.y - s2.h/2)) {return true;} //s1 bottom edge verus s2 top
else if ((s1.x - s1.w/2) < (s2.x + s2.w/2)) { return true;} //s1 left edge verus s2 right edge
else if ((s1.y - s1.h/2) < (s2.y + s2.h/2)) { return true;} //s1 top edge versus s2 bottom edge
else {return false;}

}


But when rectangles are rotated (I am using pushMatrix() and popMatrix() to rotate), it's hard to know their exact coordinates. Should I "rotate" my test? I can't get this to work. Is there another technique for testing overlap / collision?
Re: Collision detection for rotated rectangles: id
Reply #1 - Jul 11th, 2006, 4:24pm
 
a quick google search brought up this as possible solution to your problem:

http://www.ragestorm.net/tutorial?id=22 (incl. a java demo)

if performance isn't an issue you could also try splitting your rectangles into triangles and check them for intersection...

hth!
Re: Collision detection for rotated rectangles: id
Reply #2 - Jul 12th, 2006, 2:16pm
 
good example above!

using Java 'Areas' is also possible in this case

Code:

boolean isOverlapped(Shape s1, Shape s2)
{
// create Areas from your Java Shapes
Area s1Area = new Area(s1);
Area s2Area = new Area(s2);

//translate according to the current rotate angle and the shapes position
AffineTransform s1Transformation = AffineTransform.getRotateInstance(rotateAngle1, s1.x, s1.y);
s1Area.transform(s1Trans);
AffineTransform s2Transformation = AffineTransform.getRotateInstance(rotateAngle2, s2.x, s2.y);
s2Area.transform(s2Trans);

// test if they overlap
s1Area.intersect(s2Area);

if (!s1.isEmpty())
{
return true;
}
else
{
return false;
}
}
Re: Collision detection for rotated rectangles: id
Reply #3 - Jul 13th, 2006, 6:51pm
 
I have just about finished implementing oren's method, just as an exercise. I'll share it here once I get his permission.

But the "areas" thing is very nice as well! Thanks!
Page Index Toggle Pages: 1