add rayHitsSphere() util method

This commit is contained in:
Andrew Meadows 2018-02-23 16:08:31 -08:00
parent 81de668c1e
commit 79b9fec900
2 changed files with 11 additions and 0 deletions

View file

@ -40,6 +40,14 @@ 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,6 +19,9 @@ 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