Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9100029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:45:58+00:00 2026-06-17T00:45:58+00:00

This is more of a programming problem than a technical question but since it

  • 0

This is more of a programming problem than a technical question but since it relates to programming and math, I’m hoping to get some helpful feedback.

I have two rectangles: rectangle1 and rectangle2.

Both are defined by four float values:

float left;
float right;
float top;
float bottom;

For the sake of this example, say that each rectangle is 100 x 100 and that rectangle1 is:

float left = 100.0;
float right = 200.0;
float top = 500.0;
float bottom = 600.0;

And rectangle2 is:

float left = 150.0;
float right = 250.0;
float top = 550.0;
float bottom = 650.0;

When a collision occurs, I am trying to determine which side of rectangle1 hit which side of rectangle2 using the eight float values.

If my question was answered, I might be able to determine something like the following when a collision occurs:

rectangle1’s right side hit rectangle2’s left side

So far, I have tried using simple math to determine the distance between each possibility:

float distance1 = rectangle2.left - rectangle1.right;
float distance2 = rectangle2.top - rectangle1.bottom;
float distance3 = rectangle2.right - rectangle1.left;
float distance4 = rectangle2.bottom - rectangle1.top;

And then taking the minimum of those values to determine which side of each rectangle was involved in the collision. It doesn’t seem so simple, though. There were two basic problems with this attempt:

1) The rectangles will already be overlapping by the time the collision code is reached.

2) If multiple rectangles are stacked on top of each other, the calculation will produce strange results (ie, even if rectangle1 is moving in the upper right direction and should hit both rectangles on their left side, it may actually hit one rectangle on the bottom and the other on the left.)

(This is because the rectangles are overlapping when the collision code is reached AND because distance1 and distance4 will be close or equal in this case.)

Is there a better way to answer this question using simple math? Any help would be appreciated.

And, before it’s mentioned, this is not homework but a real problem I’m trying to solve for my game.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:46:00+00:00Added an answer on June 17, 2026 at 12:46 am

    If you can correctly identify the collision, then to determine the faces draw a line joining the center of the 2 rectangles and check for intersection with the faces of each rectangle. At any given time the line will intersect 2 faces as long as the 2 rectangles are not overlapping or to be precise, as long as the center of 1 is not inside the other.

    When they are overlapping, you can get 1 or 0 intersection.
    case 0: 1 rectangle is totally inside the other so you can decide how you want to decide which sides are hitting, may be choose the one closest to each center.

    case 1: rectangle is totally or partially inside the other. In either case, continue to extend your line joining the centers until you cross the outer (containing) rectangle. If u cross the contained rectangle before again, just change the hit face to the newly crossed face.

    Hope its not too confusing 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is more like Math question rather than programming but since I'm
This may be a math question more than a programming question, but we'll see.
This is more of a maths question than programming but I figure a lot
Excuse if this is more a file format conversion question rather than programming, but
This is more an algorithm question than programming, but it's implemented in programming so
This is not really a programming question, more of an algorithmic one. The problem:
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
This is primarily a question of possibilities more than instructions. I'm a programming consultant
This is more a programming exercise than a real-world problem: I am looking for
This may be a more math focused question, but wanted to ask here because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.