[Closed] Primitive volume checks maths?
Ok guys,
Im fairly rusty at my maths…
Is there a way to check if a sphere intersects with a box or bounding volume easily within max?
Thanks for the help
This is a function to test if an axis-aligned bounding box (AABB) and a sphere intersects.
-- Taken from the article "Simple Intersection Tests For Games" by Miguel Gomez
-- http://www.gamasutra.com/features/19991018/Gomez_1.htm
fn AABBOverlapsSphere bbmin bbmax r C = (
local s = 0, d = 0
-- Find the square of the distance from the sphere to the box
for i = 1 to 3 do (
if C[i] < bbmin[i] then (
s = C[i] - bbmin[i]
d += s * s;
)
else if C[i] > bbmax[i] do (
s = C[i] - bbmax[i]
d += s * s;
)
)
return (d <= r * r)
)
so what if the bounding box is not aligned to an axis. For example a rotated box in 3d max would not have an axis aligned bounding box correct?
Ok im using $.min and $.max and $.center to get the bounding box…
In this case you have to do the calculations in OBB’s (oriented bounding box) local-space, space where the box is an AABB. This should work:
fn OBBOverlapsSphere obbMatrix obbSize sphR sphC = (
local s = 0, d = 0
-- This matrix will transform world-space to OBB's local-space
local inv_xform = inverse obbMatrix
-- Calculate OBB min and max in local-space (axes are aligned) --
local obbSizeOver2 = obbSize * 0.5
local bbmin = -obbSizeOver2
local bbmax = obbSizeOver2
-- Transform the sphere center to OBB's local-space
sphC = sphC * inv_xform
-- Find the square of the distance from the sphere to the box
for i = 1 to 3 do (
if sphC[i] < bbmin[i] then (
s = sphC[i] - bbmin[i]
d += s * s;
)
else if sphC[i] > bbmax[i] do (
s = sphC[i] - bbmax[i]
d += s * s;
)
)
return (d <= sphR * sphR)
)
One important thing. The OBB should have the pivot point in its center so its position (stored in the 4th row in the obb’s transformation matrix) and center are the same. That’s because the calculations expect the center of the box.
Example:
OBBOverlapsSphere $Box01.transform [$Box01.width, $Box01.length, $Box01.height] $Sphere01.radius $Sphere01.center
Remember, the pivot point in the center!.
Hope that helps.
Of course, the min and max properties from MAX are refered to the AABB. But obviously an OBB is more precise because it adapts better to the rotated object. So if you need to use OBBs, you can use the function above.