# Thread: Rotated rectangle collision detection

1. ## 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:
Java 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:

So I need help to make code checking if that blue rectangle is inside between black rectangle side lines.

2. ## Re: Rotated rectangle collision detection

How would you calculate whether the rectangles intersect, without using a computer?

db

db

4. ## Re: Rotated rectangle collision detection

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

5. ## 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

6. ## Re: Rotated rectangle collision detection

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). :)
Last edited by Daslee; 10-25-2012 at 02:57 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•