# Rotated rectangle collision detection

• 10-21-2012, 09:53 PM
Daslee
Rotated rectangle collision detection
Hello. I tried to make collision detection for rotated rectangle in java graphics, but now i have problem. When rectangle is rotated, im getting all of rotated rectangle corners x, y positions and then creating four lines. And now for collision detection I was tried to check for intersection with other rectangle and all rotated rectangle side lines, but lines is only 1 px thickness, so if I have 200x200 pixels rotated rectangle and 10x10 pixels rectangle goes inside rotated rectangle, my application says that there is no collision detection, cause 10x10 size rectangle not collides rotated rectangle side lines. So I need to make somehow to check if that 10x10 is inside rotated rectangle. This is how I tried:
Code:

```double minX = 10000, minY = 10000, maxX = 0, maxY = 0;                 for(int i=0; i<side_lines.length; i++){                         //Get lowest and highest X                         if(side_lines[i].getX1() < minX){ minX = side_lines[i].getX1(); }                         if(side_lines[i].getX2() < minX){ minX = side_lines[i].getX2(); }                         if(side_lines[i].getX1() > maxX){ maxX = side_lines[i].getX1(); }                         if(side_lines[i].getX2() > maxX){ maxX = side_lines[i].getX2(); }                         //Get lowest and highest Y                         if(side_lines[i].getY1() < minY){ minY = side_lines[i].getY1(); }                         if(side_lines[i].getY2() < minY){ minY = side_lines[i].getY2(); }                         if(side_lines[i].getY1() > maxY){ maxY = side_lines[i].getY1(); }                         if(side_lines[i].getY2() > maxY){ maxY = side_lines[i].getY2(); }                 }                 Rectangle temp_rect = new Rectangle((int)minX, (int)minY, (int)(maxX-minX), (int)(maxY-minY));                 if(temp_rect.intersects(r)){                         return true;                 }                 return false;```
Now this says that there is collision, but even if it will be like that:
Attachment 4172

So I need help to make code checking if that blue rectangle is inside between black rectangle side lines.
• 10-22-2012, 03:53 AM
DarrylBurke
Re: Rotated rectangle collision detection
How would you calculate whether the rectangles intersect, without using a computer?

db
• 10-22-2012, 03:54 AM
DarrylBurke
Re: Rotated rectangle collision detection

db
• 10-22-2012, 09:50 AM
Daslee
Re: Rotated rectangle collision detection
Quote:

Originally Posted by DarrylBurke
How would you calculate whether the rectangles intersect, without using a computer?

db

Do not know, poor logic at me.. :D
• 10-23-2012, 02:41 PM
DarrylBurke
Re: Rotated rectangle collision detection
Well, you need to understand the mathematics behind it before you even attempt to code it up. So I suggest you focus on that first and come back if/when you have a Java question.

db
• 10-25-2012, 01:30 PM
Daslee
Re: Rotated rectangle collision detection
Quote:

Originally Posted by DarrylBurke
Well, you need to understand the mathematics behind it before you even attempt to code it up. So I suggest you focus on that first and come back if/when you have a Java question.

db

Finally! I turned on my brains for a while, and finally realized how to check for that intersection. Before some time I was thinking to check if any corner point from rect1 is inside rect2, but I knew only one method how to check if some point is inside some area, it was similarly like contains method in Polygon class. But when I thought about that idea, it looked little bit weird for me...
But anyway, now to check for intersection I create new Polygon class which has 4 points(all rotated rectangle(rect1) corner points) and then I check if that polygon contains any corner point from other rotated rectangle(rect2). :)