Calculate Arc from Center of One AABB to Intersections of Another

45 Views Asked by At

I am working in the XY plane with two AABBs: the play space and a game object. My goal is to generate a random direction along their intersection going from the center of the game object in towards the play space. They should always intersect, but in the unexpected case that they don't, I will choose the direction from the game object's center to the play space's center.

I can think of 6 different intersection cases which I've illustrated in the attached image. The blue arcs represent the direction I want to randomly choose from and the green box is the play space.

So far my approach is to generate an AABB for the area where they intersect by taking the maximum of their minimum coordinates and the minimum of their maximum coordinates. I then test the four corners of the intersection AABB to see if they intersect the play space to get a list of intersection points. I can determine if the intersection points are on the edges of or inside the play space, but this is where I am stuck.

Can you please help me figure out to what to do next to get a random direction from one AABB in towards another?

[Edit:] I realized my first set of diagrams was incorrect to the results I wanted to model. These diagrams correctly show the wider available direction range when the game object intersects the play space but its center is not contained. This helped me see that there are actually only 3 cases: when the game object center is outside the play space, when the game object center is in the play space and their edges intersect, and when the game object is wholly within the play space.

1

There are 1 best solutions below

0
On BEST ANSWER

I figured out an algorithm to solve my problem.

  • If the game object is wholly within the play bounds
    • Return a random direction 0 to 360 degrees centered at the game object center
  • Else if the game object's center is outside the play bounds
    • The arc side endpoints are the corners of the play bounds side the game object is outside of
      • If the game object extends to a perpendicular side, include that side's opposite corner
    • Return a random direction based on the arc endpoints and centered at the game object center
  • Else
    • The arc side endpoints are the intersection points with the play bounds
      • If the game object extends to a perpendicular side, extend the arc side endpoint to that side's intersection
    • Return a random direction based on the arc endpoints and centered at the game object center

To return a random direction based on the arc endpoints:

  • Use Atan2 to find the angles from the game object center to the arc endpoints
    • Convert those angles from -PI to PI to 0 to 2PI for ease of understanding
  • Treat the minimum as the start angle and the maximum as the end angle
  • Use Atan2 to find the angle from the game object center to the play bounds center
  • If that angle adjusted to 0 to 2PI is not between the start and end angles
    • The end angle becomes the start angle and the start angle becomes -2PI + end angle
  • Return a random direction from start angle to end angle