# To know which side of the rectangle was hit by another rectangle??

This topic is 3348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have one rectangle which is stationary and another rectangle which is moving through the whole area. Both having almost same dimensions. The movable rectangle is not rotating and all, just moving only. It can hit the stationary rectangle in 4 ways and the language provides me a function to know when it is hit. 1. Stationary Rect,left and Movable Rect, right 2. Stationary Rect,right and Movable Rect, left 3. Stationary Rect,top and Movable Rect, bottom 4. Stationary Rect,bottom and Movable Rect, top. I am able to get the coordinates of each edge of the rectangle and also the centres. Is there a way to know which of the above hits take place by using maths or anything?? I am a beginner in iphone app programming and is doing all this in Obj-C. [Edited by - wolverine121 on October 14, 2009 4:57:53 AM]

##### Share on other sites
Quote:
 // assume top-left screen coord is (0, 0), and bottom-right screen coord is like (1024, 768)bool rectangleCollision(float left0, float top0, float right0, float bottom0, float left1, float top1, float right1, float bottom1, float& intersectX, float& intersectY){ float intersectLeft = (right1 - left0); float intersectRight = (right0 - left1); float intersectTop = (bottom1 - top0); float intersectBottom = (bottom1 - top0); // no intersection if(intersectLeft < 0 || intersectRight < 0) return false; // no intersection if(intersectTop < 0 || intersectBottom < 0) return false; // amount of intersection on the X axis // sign indicates which side intersects. if(intersectLeft < intersectRight) intersectX = intersectLeft; else intersectX = -intersectRight; // amount of intersection on the Y axis // sign indicates which side intersects. if(intersectTop < intersectBottom) intersectY = intersectTop; else intersectY = -intersectBottom; return true;}

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633708
• Total Posts
3013468
×