From: frohlich Date: Sun, 2 May 2010 20:14:39 +0000 (+0000) Subject: Provide a more exact sphere/box test. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=490dad7838f917e3d603a7a08c38cef7cfbaeecf;p=simgear.git Provide a more exact sphere/box test. Modified Files: simgear/math/SGIntersect.hxx simgear/math/SGBox.hxx --- diff --git a/simgear/math/SGBox.hxx b/simgear/math/SGBox.hxx index 8024a131..dfa50275 100644 --- a/simgear/math/SGBox.hxx +++ b/simgear/math/SGBox.hxx @@ -71,6 +71,14 @@ public: (pt[2] > center[2]) ? _min[2] : _max[2]); } + // return the closest point to pt still in the box + template + SGVec3 getClosestPoint(const SGVec3& pt) const + { + return SGVec3((pt[0] < _min[0]) ? _min[0] : ((_max[0] < pt[0]) ? _max[0] : T(pt[0])), + (pt[1] < _min[1]) ? _min[1] : ((_max[1] < pt[1]) ? _max[1] : T(pt[1])), + (pt[2] < _min[2]) ? _min[2] : ((_max[2] < pt[2]) ? _max[2] : T(pt[2]))); + } // Only works for floating point types SGVec3 getCenter() const diff --git a/simgear/math/SGIntersect.hxx b/simgear/math/SGIntersect.hxx index 533bd2dc..24741f64 100644 --- a/simgear/math/SGIntersect.hxx +++ b/simgear/math/SGIntersect.hxx @@ -38,25 +38,11 @@ intersects(const SGBox& box, const SGSphere& sphere) { if (sphere.empty()) return false; - // Is more or less trivially included in the next tests - // if (box.empty()) - // return false; - - if (sphere.getCenter().x() < box.getMin().x() - sphere.getRadius()) - return false; - if (sphere.getCenter().y() < box.getMin().y() - sphere.getRadius()) - return false; - if (sphere.getCenter().z() < box.getMin().z() - sphere.getRadius()) + if (box.empty()) return false; - if (box.getMax().x() + sphere.getRadius() < sphere.getCenter().x()) - return false; - if (box.getMax().y() + sphere.getRadius() < sphere.getCenter().y()) - return false; - if (box.getMax().z() + sphere.getRadius() < sphere.getCenter().z()) - return false; - - return true; + SGVec3 closest = box.getClosestPoint(sphere.getCenter()); + return distSqr(closest, SGVec3(sphere.getCenter())) <= sphere.getRadius2(); } // make it symmetric template