Click here to Skip to main content
15,616,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?

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.
Share this answer
mohammadali1375 17-Jan-13 8:10am    
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.
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..!!! :)
Share this answer
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[^]



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.
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900