# Calculating amount of overlap/penetration of two 3D AABBs

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

## Recommended Posts

Hey folks, I have two 3D AABBs (lets call them boxA and boxB) and I was wondering if there was a way of calculating the amount of overlap/penetration between them in order to resolve interpenetrations. The AABBs are min/max representations although I've added some trivial functions to calculate the "half-widths"/"half-extents" if they are needed.

[If it's helpful to know,] for the contact normal I'm simply using the direction from boxB to boxA (unless one object is the ground, in which case I use the grounds normal). This is possibly incorrect but it is only particle physics being used here so complete 100% accuracy isn't a necessity.

Also, I've read up on the separating axis theorem but I was wondering if this may be slightly overkill for strictly AABBs since it can seemingly be applied to many generic polyhedra.

The only bit of information I need at this time is the overlap/penetration depth.

Here's what I have come up with so far (note: I quite often try to solve the 2D varient first since it'll be easier and then see from that how I can possibly extend it to 3D):

So possibly something along the lines of:
 float dx = abs(boxA.min.x - boxB.max.x); float dy = abs(boxA.min.y - boxB.max.y); float dz = abs(boxA.min.z - boxB.max.z);
And then possibly the penetration depth is the smallest of these three? (and then the two objects are pushed apart by this distance in the direction of the contact normal although what % of the penetration depth each object moves by is determined by the mass of the objects, heavier objects don't get pushed as far etc).

I'm likely nowhere near correct but it's starting to drive me cuckoo

##### Share on other sites
I'm half asleep (shouldn't post when tired lol) but hopefully this should put you on the right track:

 bool Calculate_1DOverlap(float aMin, float aMax, float bMin, float bMax, float &oMin, float &oMax) { // providing they DO overlap, then the highest of the 2 minimums must be the minimum of the overlap // and the lowest of the two maximums must be the overlap max if (!Test_1DOverlap(aMin, aMax, bMin, bMax)) return false; if (aMin > bMin) oMin = aMin; else oMin = bMin; if (aMax > bMax) oMax = bMax; else oMax = aMax; return true; } 

Just do a similar routine for each axis (x y and z) and you've got your overlap.

##### Share on other sites
and

 bool Test_1DOverlap(float aMin, float aMax, float bMin, float bMax) { if (aMin >= bMax) return false; if (bMin >= aMax) return false; return true; } 

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633687
• Total Posts
3013327
×