Jump to content
  • Advertisement
Sign in to follow this  
wolverine121

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!