Collision detection & response
Axially aligned bounding box (AABB)
Closest Point in an AABB
This is done by “pushing” into along each axis in turn. Notice that if the point is already inside the box, this code returns the original point.
if (x < minX) {
x = minX;
} else if (x > maxX) {
x = maxX;
}
if (y < minY) {
y = minY;
} else if (y > maxY) {
y = maxY;
}
if (z < minZ) {
z = minZ;
} else if (z > maxZ) {
z = maxZ;
}
Intersection of a Sphere and AABB
... we first find the point on the box that is closest to the center of the sphere by using the... Closest Point in an AABB... We compute the distance from this point to the center of the sphere and compare this distance with the radius. (Actually, in practice we compare the distance squared against the radius squared to avoid the square root in the distance computation.) If the distance is smaller than the radius, then the sphere intersects the AABB.
Intersection of Two AABBs
bool aabbsOverlap(const AABB3 &a, const AABB3 &b) {
// Check for a separating axis.
if (a.min.x >= b.max.x) return false;
if (a.max.x <= b.min.x) return false;
if (a.min.y >= b.max.y) return false;
if (a.max.y <= b.min.y) return false;
if (a.min.z >= b.max.z) return false;
if (a.max.z <= b.min.z) return false;
// Overlap on all three axes, so their
// intersection must be non-empty
return true;
}
def boxbox(lx,ly,lw,lh,rx,ry,rw,rh)
not (
lx+lw<rx or # on right
rx+rw<lx or # on left
ly>ry+rh or # on top
ry>ly+lh # on bottom
)
end
Test if left is on the right or right on the left or top below bottom or bottom above top (same front and back for 3D) - not overlapping.
There is a dynamic test described which tells interval of time when the two boxes overlap when the collision starts given velocity vector.