12,550,593 members (44,282 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.

Thanks. But I Want rotate it to any direction ( 360 degree )
Richard MacCutchan 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.

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 50 Suvendu Shekhar Giri 30 Nathan Minier 30 Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ) 28 Afzaal Ahmad Zeeshan 25
 OriginalGriff 3,901 Suvendu Shekhar Giri 1,743 John Simmons / outlaw programmer 1,687 ppolymorphe 1,571 Karthik Bangalore 1,210