Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static bool TestSphereAABB (Vector3 sphereCoords, double sphereRadius, Vector3 boxCoords, Vector2 boxSize) {
- // Compute squared distance between sphere center and AABB
- // the sqrt (dist) is fine to use as well, but this is faster.
- float sqDist = 0.0f;
- let diffCoords = level.Vec3Diff (sphereCoords, boxCoords);
- let bMin = (
- diffCoords.X - boxSize.X,
- diffCoords.Y - boxSize.X,
- diffCoords.Z
- );
- let bMax = (
- diffCoords.X + boxSize.X,
- diffCoords.Y + boxSize.X,
- diffCoords.Z + boxSize.Y
- );
- // For each axis count any excess distance outside box extents
- // X
- if (bMin.X > 0) sqDist += (bMin.X) * (bMin.X);
- if (bMax.X < 0) sqDist += (bMax.X) * (bMax.X);
- // Y
- if (bMin.Y > 0) sqDist += (bMin.Y) * (bMin.Y);
- if (bMax.Y < 0) sqDist += (bMax.Y) * (bMax.Y);
- // Z
- if (bMin.Z > 0) sqDist += (bMin.Z) * (bMin.Z);
- if (bMax.Z < 0) sqDist += (bMax.Z) * (bMax.Z);
- // Sphere and AABB intersect if the (squared) distance between them is
- // less than the (squared) sphere radius.
- return sqDist <= sphereRadius * sphereRadius;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement