Advertisement

AABB Sphere intersect - illustrating closest point on AABB

Started by June 12, 2015 02:07 PM
1 comment, last by cozzie 9 years, 8 months ago

Hi,

I've added a AABB Sphere intersection tests in my collision 'library'. The approach I'm taking is:

- calculate 'max' vector of sphere center and AABB min (lets call it maxSphereAABB)

- calculate 'min' vector of maxSphereAABB and AABB max

- this results in a vector between the closest point on the AABB to the spherecenter

- now I compare the length of this vector to the sphere radius

Voila, is the distance is lower, we have intersection.

This all works fine I believe, but my questions are:

- how can I logically explain/ draw out how I'm calculating the point closest on the AABB.

I've tried it with drawing an example but that didn't work out:

closestpointonaabb.jpg

Maybe someone can draw me a better example where I can visually see the maxSphereAABB vector etc. to finally find the closestPointOnAABB.

Another question I have, is how do I go from here if I wanted to check for intersection between a OBB and a Sphere.


bool AABBSphereCollision(const AABBWORLD &pAABB, const SPHERE &pSphere)
{
	XMVECTOR maxSphereAABB = XMVectorMax(XMLoadFloat3(&pSphere.WorldCenter), XMLoadFloat3(&pAABB._Min));
	XMVECTOR closestPointInAABB = XMVectorMin(maxSphereAABB, XMLoadFloat3(&pAABB._Max));

	XMVECTOR dist = XMVector3Length(closestPointInAABB - XMLoadFloat3(&pSphere.WorldCenter));

	float distSqr = XMVectorGetX(dist) * XMVectorGetX(dist);

	if(distSqr < pSphere.RadiusSqr()) return true;

	return false;
}

Any input is appreciated.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

It can be explained in 1D. First you take the rightmost point out of AABBmin and Sphere center. Then you take leftmost point out of the result and AABBmax. If you write down all the variants (SphereCenter AABBmin AABBmax; AABBmin SphereCenter AABBmax; AABBmin AABBmax SphereCenter) it would be the best explanation, IMO.

To use this approach to check for intersection between OBB and sphere, you have two options: 1. transform sphere center into OBBs coordinate system, i.e. apply a matrix (that transforms OBB to AABB) to sphere center. This can be costly operation. 2. Calculate AABB that would contain OBB.

Advertisement

Thanks, your hint made me draw it out in 2D and no I get it :)

AABBsphere-OK.jpg

Next stop: OBB Sphere.

For now I'll go for transforming the Sphere center to the OBB space and do the same as with the AABB check.

Maybe later on check if I can find out how to find the closest point on OBB, to save 1 matrix/vector mul.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement