## Rotated Rectangle to Rectangle Collision

While recently working on a 3D crazy taxi like game, I had to program the collision checking code between a vehicle and other vehicles or buildings. Since the project didn't have the car moving in the vertical axis, collision is essentially 2D. Unfortunately I couldn't find any support for rotated rectangle collision in XNA, only collision between two parallel rectangles, so needed to code this collision detection from scratch.

There are two criteria for a collision between two rectangles:

- A rectangle line intersects the other rectangles line
- The entire rectangle is contained in the other rectangle (a rectangle point is contained in the other rectangle)

Because the collision detection rate was so high, and time was slim I decided not to bother with the latter as most objects were of comparable size any way. As such collision between the two rectangles can be done by testing each line in the rectangle against each line in the other rectangle. Similarly, this could be applied to any shape (uniform or otherwise).

For checking the line to line collision detection I coded a dedicated function. The function essentially worked like this:

First of all we represent each line as two points, stored as a Vector2. For line A we have A1 and A2, for line B we have B1 and B2.

Next we translate all points so that A1 is at 0,0.

Finally we then rotate all points so that A2 is on the X axis. (and as such line A goes along the X axis from the origin to a certain point at (whatever, 0)

Now that we have the lines in this state it is simple to work out whether line B intersects line A, firstly B1 and B2 must be on opposite sides of the x axis, and the point where line B crosses the X axis must be between A1 and A2.

Now all that's necessary is to iterate through each line and test its collision against each line from the other rectangle using this line-to-line collision method.

*As usual comments and criticisms are appreciated. I can post a proof of concept project with the code if someone wants it*