From b1f9f44bbee2d521808454a320089a64ffbb7526 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sat, 7 Mar 2026 20:20:06 +0100 Subject: [PATCH] sphere box test --- interfaces/Crafter.Math-Intersection.cppm | 28 +++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/interfaces/Crafter.Math-Intersection.cppm b/interfaces/Crafter.Math-Intersection.cppm index 545e8b9..bf003bb 100755 --- a/interfaces/Crafter.Math-Intersection.cppm +++ b/interfaces/Crafter.Math-Intersection.cppm @@ -197,17 +197,41 @@ namespace Crafter { constexpr bool IntersectionTestSphereOrientatedBox(Vector spherePos, T sphereRadius, Vector boxSize, MatrixRowMajor boxMat) { Vector d = spherePos - Vector(boxMat.m[0][3], boxMat.m[1][3], boxMat.m[2][3]); - T distSq = 0.0f; + T distSq = T(0); for (std::uint32_t i = 0; i < 3; ++i) { Vector axis(boxMat.m[0][i], boxMat.m[1][i], boxMat.m[2][i]); T dist = Vector::Dot(d, axis); T excess = std::fabs(dist) - boxSize.v[i]; - excess = std::max(excess, 0.0f); + excess = std::max(excess, T(0)); distSq += excess * excess; } + // Check sphere axes (world axes) + for (std::uint32_t i = 0; i < 3; ++i) { + Vector axis = Vector(0,0,0); + axis.v[i] = T(1); + T dist = Vector::Dot(d, axis); + T excess = std::abs(dist) - sphereRadius; + excess = std::max(excess, T(0)); + distSq += excess * excess; + } + + // Check cross product axes + for (std::uint32_t i = 0; i < 3; ++i) { + for (std::uint32_t j = 0; j < 3; ++j) { + Vector boxAxis(boxMat.m[0][i], boxMat.m[1][i], boxMat.m[2][i]); + Vector sphereAxis = Vector::Zero(); + sphereAxis.v[j] = T(1); + Vector axis = Vector::Cross(boxAxis, sphereAxis); + T dist = Vector::Dot(d, axis); + T excess = std::abs(dist) - (boxSize.v[(i+1)%3] * sphereRadius + boxSize.v[(i+2)%3] * sphereRadius); + excess = std::max(excess, T(0)); + distSq += excess * excess; + } + } + return distSq <= sphereRadius * sphereRadius; } } \ No newline at end of file