Skip to content

Instantly share code, notes, and snippets.

@ThePhD
Created January 20, 2014 02:23
Show Gist options
  • Select an option

  • Save ThePhD/8513917 to your computer and use it in GitHub Desktop.

Select an option

Save ThePhD/8513917 to your computer and use it in GitHub Desktop.
I HOPE YOU'RE HAPPY
template <typename T>
Fur::optional<RHit<T>> intersect( const Fur::TRay3<T>& ray, const RSphere<T>& target ) {
Fur::TVector3<T> ray2sphere = target.position - ray.Origin;
T dist = ray2sphere.Dot( ray.Direction );
T ray2spheresquared = ray2sphere.Dot( ray2sphere );
T radiussquared = Radius * Radius;
if ( dist < 0
&& ray2spheresquared > radiussquared ) {
return nullopt;
}
T raycrossessphere = ray2spheresquared - dist * dist;
if ( raycrossessphere > radiussquared ) {
return nullopt;
}
T q = static_cast<T>( sqrt( radiussquared - raycrossessphere ) );
RHit<T> hit;
if ( ray2spheresquared > radiussquared )
hit.distance0 = dist - q;
else
hit.distance0 = dist + q;
hit.contact = ray.Parametric( hit.distance0 );
hit.normal = target.position - hit.contact;
hit.normal.Normalize();
return hit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment