Click here to Skip to main content
12,550,593 members (44,282 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ Game
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 ?
Posted 16-Jan-13 22:11pm
Rate this: bad
Please Sign up or sign in to vote.

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 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.
Rate this: bad
Please Sign up or sign in to vote.

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..!!! :)
Rate this: bad
Please Sign up or sign in to vote.

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[^]



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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161021.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100