Skip to main content

Collision detection & response

Axially aligned bounding box (AABB)

Closest Point in an AABB

def cpbbox(
		x,y,
		xmin,ymin,xmax,ymax
	)
	if x<xmin
		x=xmin
	elsif x>=xmax
		x=xmax-1
	end
	if y<ymin
		y=ymin
	elsif y>=ymax
		y=ymax-1
	end	
	return x,y
end
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;
}

 

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.

Intersection of a Sphere and AABB

def circbbox(
		x,y,r,
		xmin,ymin,xmax,ymax
)
	cx,cy=cpbbox(x,y,xmin,ymin,xmax,ymax)
	(x-cx)**2+(y-cy)**2<=r**2
end

def circbbox2(
		x,y,r,
		bx,by,bw,bh
)
	cx,cy=cpbbox(x,y,bx,by,bx+bw,by+bh)
	(x-cx)**2+(y-cy)**2<=r**2
end

 

... 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 bboxbbox(
		aminx,aminy,amaxx,amaxy,
		bminx,bminy,bmaxx,bmaxy
	)
	not (
		aminx>=bmaxx || amaxx<=bminx ||
		aminy>=bmaxy || amaxy<=bminy
	)
end

def bboxbbox2(
		ax,ay,aw,ah,
		bx,by,bw,bh
	)
	not (
		ax>=bx+bw || ax+aw<=bx ||
		ay>=by+bh || ay+ah<=by
	)
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.