How to calculate closest distance between two AABBs (vector form)? I found a solution here. Is it possible extract a vector form of this? I know a vector form of AABB - point distance.
Candidate:
Float distance(const Box & a, const Box & b) {
return Vector::zero.max(a.min - b.max).max(b.min - a.max).magnitude();
}
The distance between two axis-aligned bounding boxes (AABB) can be computed as follows:
where
min
andmax
are two corner points of a box.The function returns zero if input boxes touch or intersect.
The code above was taken from MeshLib and it works both for 2D (
V::elements=2
) and 3D (V::elements=3
).It is not quite clear what you mean by "vector form", since the distance is a scalar. If one wants a more concise implementation of
getDistanceSq
, then in addition tointersection
the second part of the function can be named somehow.