0

For my program, I implemented AABB collision which works fine. When AABB collision detects a collision between 2 boxes, I want to find the surface area of portion where the 2 boxes are overlapping. Is there a simple equation to use to find out that surface area?

enter image description here The main issue: So it was really easy for me to point out places to calculate to find my overlapping X and Y values to find my surface are. Just like "(|6-3|)*(|6-4|)". My issue is that I'm trying to have no conditional problems. So I was wondering if there is a formula where you can plug in all the edge points of both boxes and it will always give the surface area that is overlapping no matter how they overlap. This will save me a lot of time and be more efficient when writing my code.

NOTE:

  • Boxes are oriented by the axis; Therefore, the sides will always be parallel to the X and Y axis.
  • The edge points of Box 1 and Box 2 is all that is known; So that being said, there is no co-ordinates of where the lines are intersecting.
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Jul 22 '15 at 15:09
  • |(4-6)| * |(6-3)|? – user40980 Jul 22 '15 at 15:13
  • @MichaelT Works for this case, but needs some extra thought when Box2 is not on the bottom right. – LorToso Jul 22 '15 at 15:17
  • 1
    @LorToso it was an attempt to get the OP to explain what he has (and hasn't) done and think about the problem a bit more. – user40980 Jul 22 '15 at 15:19
  • 1
    @AMDee write a class that has a method that does what you were describing. Call it "IntersectFinder". Have it take the two rectangles as a constructor. Have a method in it titled "surfaceArea". Get that class working. Then post the completely working class to CodeReview and see what suggestions they have. – user40980 Jul 22 '15 at 15:31
3

Since a long time ago I had the same problem, I came up with this simple solution:

  1. (In case you haven't) Create java.awt.Rectangle-Objects with correct height and width
  2. Move the Rectangles to the coordinates your objects are located (using Rectangle.setLocation(x,y)
  3. Call Rectangle1.intersection(Rectangle2) and let the library do your computations

Even easier: Step 1 and 2 can be done by the constructor of java.awt.Rectangle

Rectangle r1 = new Rectangle(x1, y1, width1, height1);
Rectangle r2 = new Rectangle(x2, y2, width2, height2);
Rectangle intersectingRectangle = r1.intersection(r2);
  • 4
    Worth noting that this class has some nasty static stuff in the initializer - Toolkit.loadLibraries(); for example. Hence this is not a lightweight solution. However the code from the intersetcion method can be used with no problems. – Ordous Jul 22 '15 at 15:20
1

You can use this pseudo-code even without detecting collision first (or use the it to detect collision, if inter_area==0 then there is no collision or tangent at most)

double intersection_area(rectangle box1,rectangle box2) {
    double inter_area=0.0
    double box1_top=box1.get_top() //10
    double box1_bottom=box1.get_bottom() //3
    double box2_top=box2.get_top() //6
    double box2_bottom=box2.get_bottom() //2
    double inter_height=min(box1_top,box2_top)-max(box1_bottom,box2_bottom) //6-3==3
    if (inter_height)>0 {
        double box1_right=box1.get_right() //6
        double box1_left=box1.get_left() //2
        double box2_right=box2.get_right() //8
        double box2_left=box2.get_left() //3
        double inter_width=min(box1_right,box2_right)-max(box1_left,box2_left)//6-3==3
        if (inter_width>0)
            inter_area = inter_height x inter_width //3x3==9
    }
    return inter_area
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.