mirror of
https://github.com/g-truc/glm.git
synced 2024-11-26 18:24:35 +00:00
Added new ray-sphere intersection
This version uses a geometric method (usually faster) and doesn't calculate the intersection position and normal, only the distance.
This commit is contained in:
parent
f96b0424ec
commit
3a3c1fd8e8
@ -68,6 +68,15 @@ namespace glm
|
||||
genType const & vert0, genType const & vert1, genType const & vert2,
|
||||
genType & position);
|
||||
|
||||
//! Compute the intersection distance of a ray and a sphere.
|
||||
//! The ray direction vector is unit length.
|
||||
//! From GLM_GTX_intersect extension.
|
||||
template <typename genType>
|
||||
bool intersectRaySphere(
|
||||
genType const & rayStarting, genType const & rayNormalizedDirection,
|
||||
genType const & sphereCenter, const typename genType::value_type sphereRadiusSquered,
|
||||
typename genType::value_type & intersectionDistance);
|
||||
|
||||
//! Compute the intersection of a ray and a sphere.
|
||||
//! From GLM_GTX_intersect extension.
|
||||
template <typename genType>
|
||||
|
@ -127,6 +127,27 @@ namespace glm
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename genType>
|
||||
GLM_FUNC_QUALIFIER bool intersectRaySphere
|
||||
(
|
||||
genType const & rayStarting, genType const & rayNormalizedDirection,
|
||||
genType const & sphereCenter, const typename genType::value_type sphereRadiusSquered,
|
||||
typename genType::value_type & intersectionDistance
|
||||
)
|
||||
{
|
||||
typename genType::value_type Epsilon = std::numeric_limits<typename genType::value_type>::epsilon();
|
||||
genType diff = sphereCenter - rayStarting;
|
||||
typename genType::value_type t0 = dot(diff, rayNormalizedDirection);
|
||||
typename genType::value_type dSquared = dot(diff, diff) - t0 * t0;
|
||||
if( dSquared > sphereRadiusSquered )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
typename genType::value_type t1 = sqrt( sphereRadiusSquered - dSquared );
|
||||
intersectionDistance = t0 > t1 + Epsilon ? t0 - t1 : t0 + t1;
|
||||
return intersectionDistance > Epsilon;
|
||||
}
|
||||
|
||||
template <typename genType>
|
||||
GLM_FUNC_QUALIFIER bool intersectRaySphere
|
||||
(
|
||||
|
Loading…
Reference in New Issue
Block a user