diff --git a/interfaces/Crafter.Math-Ray.cppm b/interfaces/Crafter.Math-Ray.cppm index e6115e9..e238641 100755 --- a/interfaces/Crafter.Math-Ray.cppm +++ b/interfaces/Crafter.Math-Ray.cppm @@ -54,4 +54,25 @@ namespace Crafter { return inverse_determinant * Vector::Dot(edge2, q); } + + export template + constexpr T IntersectionTestRaySphere(Vector position, T radius, Vector rayOrigin, Vector rayDir) { + T a = Vector::Dot(rayDir, rayDir); + T b = Vector::Dot(rayDir, (T(2) * (rayOrigin - position))); + T c = Vector::Dot(position, position) + Vector::Dot(rayOrigin, rayOrigin) - T(2) * Vector::Dot(rayOrigin, position) - radius * radius; + T d = b * b + (T(-4)) * a * c; + + if (d < 0) { + return std::numeric_limits::max(); + } + + d = std::sqrt(d); + + float t = (T(-0.5)) * (b + d) / a; + if (t > T(0)) { + return t; + } else { + return std::numeric_limits::max(); + } + } } \ No newline at end of file diff --git a/interfaces/Crafter.Math-Vector.cppm b/interfaces/Crafter.Math-Vector.cppm index b087be4..2cb8378 100755 --- a/interfaces/Crafter.Math-Vector.cppm +++ b/interfaces/Crafter.Math-Vector.cppm @@ -347,4 +347,10 @@ struct std::formatter> : std::formatter +constexpr Crafter::Vector operator*(BT b, const Crafter::Vector& v) { + return v * b; +} \ No newline at end of file diff --git a/interfaces/main.cpp b/interfaces/main.cpp index af1b2c5..b2562fc 100644 --- a/interfaces/main.cpp +++ b/interfaces/main.cpp @@ -4,10 +4,5 @@ import std; using namespace Crafter; int main() { - Vector test(1,2,3); - Vector test2(1,2,3); - Vector test3(1,2,3); - Vector test4(1,2,3); - Vector test5(1,2,3); - std::cout << IntersectionTestRayTriangle(test, test2, test3, test4, test5) << std::endl; + std::cout << IntersectionTestRaySphere({0,0,0}, 10, {0,0,-100}, {0,1,0}) << std::endl; } \ No newline at end of file