11,705,610 members (47,548 online)
Rate this:
See more:
Hi.
I write a very simple collision for my game :

```bool MRectangle::HitTo( MRectangle other )
{

if (position.X+this->w<other.position.X) return false;
if (position.X>other.position.X+other.w) return false;
if (position.Y+this->h<other.position.Y) return false;
if (position.Y>other.position.Y+other.h) return false;
return true;
}```

Its's work. But if I want rotate the rectangle what should I do ?
Thanks
Posted 16-Jan-13 22:11pm

Rate this:

## Solution 1

It rather depends on how far you rotate it, and in which direction. But, at a guess, you need to keep track of which side or corner is pointing at which edge of your window. So, if you rotate clockwise by 90 degrees, the top becomes the right side, the right becomes the bottom, etc.
mohammadali1375 at 17-Jan-13 8:10am

Thanks. But I Want rotate it to any direction ( 360 degree )
Richard MacCutchan at 17-Jan-13 8:42am

Well, as I said above you need to track whether a side or a corner is the limiting factor. Try drawing a rectangle on paper at various angles of rotation to see how you need to approach the problem.
Rate this:

## Solution 2

if you want to know how to test rotated rect for collisions search for hyperplane separation theorem... i give you the example you need...
if you want to rotate use rotation matrix...
as i see you work in 2d so its easier...

```1)for each rectangle(lets call this own_rect)

2)for each line in the ownRect

3)check the points of the other rectangle(lets call this other_rect)

4)if all the points of the other_rect are on the same side of the line AND
all the points of the own_rect are on the other side of(or on) the line
->there is no collision

5)else continue (there may be a collision but we are not sure yet)
```

Not finding a line that "separates" the rectangles means you have a collision

it is very easy to use this algorithm in 3 dimensions... but instead of lines you have planes...

I hope i helped..!!!
v2
Rate this:

## Solution 3

Checking for collisions of two arbitrarily rotated rectangles is all but trivial. Basically you have to treat them like two convex polygons. There are a couple of algorithms around for checking the overlap of two polygons. The attribute "convex" is in that context, as it makes the process of intersecting polygons easier.

You might want to start here[^]

and

here[^]

Most of these algorithms have been designed with the goal of clipping graphics, but you can use them for the purpose of determining an intersection equally well.

Before you go there, you might want to spend a couple of minutes thinking about the format in which you want to store your rotated rectangles. That might lead you to a polygon representation as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

 0 OriginalGriff 213 1 Sergey Alexandrovich Kryukov 160 2 Inimicos 60 3 ProgramFOX 50 4 Richard MacCutchan 45
 0 OriginalGriff 9,053 1 Sergey Alexandrovich Kryukov 8,347 2 CPallini 5,189 3 Maciej Los 4,726 4 Mika Wendelius 3,626

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 17 Jan 2013