From b868e31cb3cbcd703f647db6fc766ae97ccd8a03 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sat, 7 Mar 2026 12:09:00 +0100 Subject: [PATCH] sphere box test --- interfaces/Crafter.Math-Intersection.cppm | 18 ++++++++ interfaces/main.cpp | 54 +---------------------- 2 files changed, 20 insertions(+), 52 deletions(-) diff --git a/interfaces/Crafter.Math-Intersection.cppm b/interfaces/Crafter.Math-Intersection.cppm index 14b0aa9..d278878 100755 --- a/interfaces/Crafter.Math-Intersection.cppm +++ b/interfaces/Crafter.Math-Intersection.cppm @@ -192,4 +192,22 @@ namespace Crafter { return true; } + + export template + constexpr bool IntersectionTestSphereOrientatedBox(Vector spherePos, T sphereRadius, Vector boxSize, MatrixRowMajor boxMat) { + Vector d = spherePos - Vector(boxMat[4][0], boxMat[4][1], boxMat[4][2]); + + T distSq = 0.0f; + + for (std::uint32_t i = 0; i < 3; ++i) + { + Vector axis(boxMat[i][0], boxMat[i][1], boxMat[i][2]); + T dist = Vector::Dot(d, axis); + T excess = std::fabs(dist) - boxSize.v[i]; + excess = std::max(excess, 0.0f); + distSq += excess * excess; + } + + return distSq <= sphereRadius * sphereRadius; + } } \ No newline at end of file diff --git a/interfaces/main.cpp b/interfaces/main.cpp index b7e3b23..57997c6 100644 --- a/interfaces/main.cpp +++ b/interfaces/main.cpp @@ -1,59 +1,9 @@ +#include import Crafter.Math; import std; using namespace Crafter; int main() { - // Intersection Test - - // Define Box A (size and matrix for transformation) - Vector sizeA(1.0f, 1.0f, 1.0f); // Box A size - MatrixRowMajor boxA; - boxA.m[0][0] = 1.0f; boxA.m[1][0] = 0.0f; boxA.m[2][0] = 0.0f; - boxA.m[1][0] = 0.0f; boxA.m[1][1] = 1.0f; boxA.m[2][1] = 0.0f; - boxA.m[2][0] = 0.0f; boxA.m[1][2] = 0.0f; boxA.m[2][2] = 1.0f; - - // Define Box B (size and matrix for transformation) - Vector sizeB(1.0f, 1.0f, 1.0f); // Box B size - MatrixRowMajor boxB; - boxB.m[0][0] = 1.0f; boxB.m[1][0] = 0.0f; boxB.m[2][0] = 0.0f; - boxB.m[1][0] = 0.0f; boxB.m[1][1] = 1.0f; boxB.m[2][1] = 0.0f; - boxB.m[2][0] = 0.0f; boxB.m[1][2] = 0.0f; boxB.m[2][2] = 1.0f; - - // Apply a small translation to box B (so that they overlap) - boxB.m[0][3] = 0.5f; - boxB.m[1][3] = 0.5f; - boxB.m[2][3] = 0.0f; - - // Check if the boxes intersect (they should, as they're overlapping) - bool result = IntersectionTestOrientedBoxOrientedBox(sizeA, boxA, sizeB, boxB); - std::cout << result << std::endl; - - - // Miss Test - - // Define Box A (size and matrix for transformation) - Vector sizeC(1.0f, 1.0f, 1.0f); // Box C size - MatrixRowMajor boxC; - boxC.m[0][0] = 1.0f; boxC.m[1][0] = 0.0f; boxC.m[2][0] = 0.0f; - boxC.m[1][0] = 0.0f; boxC.m[1][1] = 1.0f; boxC.m[2][1] = 0.0f; - boxC.m[2][0] = 0.0f; boxC.m[1][2] = 0.0f; boxC.m[2][2] = 1.0f; - - // Define Box D (size and matrix for transformation) - Vector sizeD(1.0f, 1.0f, 1.0f); // Box D size - MatrixRowMajor boxD; - boxD.m[0][0] = 1.0f; boxD.m[1][0] = 0.0f; boxD.m[2][0] = 0.0f; - boxD.m[1][0] = 0.0f; boxD.m[1][1] = 1.0f; boxD.m[2][1] = 0.0f; - boxD.m[2][0] = 0.0f; boxD.m[1][2] = 0.0f; boxD.m[2][2] = 1.0f; - - // Apply a large translation to box D (so that they do not intersect) - boxD.m[0][3] = 3.0f; - boxD.m[1][3] = 3.0f; - boxD.m[2][3] = 0.0f; - - // Check if the boxes do not intersect (they shouldn't, as they are far apart) - bool missResult = IntersectionTestOrientedBoxOrientedBox(sizeC, boxC, sizeD, boxD); - std::cout << missResult << std::endl; - - return 0; + } \ No newline at end of file