move rayHitsSphere logic to AABox method

This commit is contained in:
Andrew Meadows 2018-03-01 16:15:26 -08:00
parent 2750f3ee71
commit 7a19f48b9c
5 changed files with 11 additions and 12 deletions

View file

@ -645,7 +645,7 @@ EntityItemID EntityTreeElement::findDetailedRayIntersection(const glm::vec3& ori
if (!success) {
return;
}
if (!rayHitsSphere(origin, direction, entityBox.calcCenter(), 0.25f * glm::length2(entityBox.getScale()))) {
if (!entityBox.rayHitsBoundingSphere(origin, direction)) {
return;
}

View file

@ -287,6 +287,15 @@ bool AABox::findRayIntersection(const glm::vec3& origin, const glm::vec3& direct
return false;
}
bool AABox::rayHitsBoundingSphere(const glm::vec3& origin, const glm::vec3& direction) const {
glm::vec3 localCenter = calcCenter() - origin;
float distance = glm::dot(localCenter, direction);
const float ONE_OVER_TWO_SQUARED = 0.25f;
float radiusSquared = ONE_OVER_TWO_SQUARED * glm::length2(_scale);
return (glm::length2(localCenter) < radiusSquared
|| (glm::abs(distance) > 0.0f && glm::distance2(distance * direction, localCenter) < radiusSquared));
}
bool AABox::touchesSphere(const glm::vec3& center, float radius) const {
// Avro's algorithm from this paper: http://www.mrtc.mdh.se/projects/3Dgraphics/paperF.pdf
glm::vec3 e = glm::max(_corner - center, Vectors::ZERO) + glm::max(center - _corner - _scale, Vectors::ZERO);

View file

@ -71,6 +71,7 @@ public:
bool expandedIntersectsSegment(const glm::vec3& start, const glm::vec3& end, float expansion) const;
bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
BoxFace& face, glm::vec3& surfaceNormal) const;
bool rayHitsBoundingSphere(const glm::vec3& origin, const glm::vec3& direction) const;
bool touchesSphere(const glm::vec3& center, float radius) const; // fast but may generate false positives
bool touchesAAEllipsoid(const glm::vec3& center, const glm::vec3& radials) const;
bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration) const;

View file

@ -40,14 +40,6 @@ glm::vec3 computeVectorFromPointToSegment(const glm::vec3& point, const glm::vec
}
}
bool rayHitsSphere(const glm::vec3& rayStart, const glm::vec3& rayDirection,
const glm::vec3& sphereCenter, float sphereRadiusSquared) {
glm::vec3 center = sphereCenter - rayStart;
float distance = glm::dot(center, rayDirection);
return (glm::length2(center) < sphereRadiusSquared
|| (glm::abs(distance) > 0.0f && glm::distance2(distance * rayDirection, center) < sphereRadiusSquared));
}
// Computes the penetration between a point and a sphere (centered at the origin)
// if point is inside sphere: returns true and stores the result in 'penetration'
// (the vector that would move the point outside the sphere)

View file

@ -19,9 +19,6 @@ class Plane;
glm::vec3 computeVectorFromPointToSegment(const glm::vec3& point, const glm::vec3& start, const glm::vec3& end);
bool rayHitsSphere(const glm::vec3& rayStart, const glm::vec3& rayDirection,
const glm::vec3& sphereCenter, float sphereRadiusSquared);
/// Computes the penetration between a point and a sphere (centered at the origin)
/// \param point the point location relative to sphere center (origin)
/// \param defaultDirection the direction of the pentration when the point is near the origin