ray sphere test
This commit is contained in:
parent
4a254a3a66
commit
2daa51680a
3 changed files with 29 additions and 7 deletions
|
|
@ -54,4 +54,25 @@ namespace Crafter {
|
|||
|
||||
return inverse_determinant * Vector<T, 3, 0>::Dot(edge2, q);
|
||||
}
|
||||
|
||||
export template<typename T>
|
||||
constexpr T IntersectionTestRaySphere(Vector<T, 3, 0> position, T radius, Vector<T, 3, 0> rayOrigin, Vector<T, 3, 0> rayDir) {
|
||||
T a = Vector<T, 3, 0>::Dot(rayDir, rayDir);
|
||||
T b = Vector<T, 3, 0>::Dot(rayDir, (T(2) * (rayOrigin - position)));
|
||||
T c = Vector<T, 3, 0>::Dot(position, position) + Vector<T, 3, 0>::Dot(rayOrigin, rayOrigin) - T(2) * Vector<T, 3, 0>::Dot(rayOrigin, position) - radius * radius;
|
||||
T d = b * b + (T(-4)) * a * c;
|
||||
|
||||
if (d < 0) {
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
|
||||
d = std::sqrt(d);
|
||||
|
||||
float t = (T(-0.5)) * (b + d) / a;
|
||||
if (t > T(0)) {
|
||||
return t;
|
||||
} else {
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue