From 7a4ca89b14b98bc672d75ef76d905659073210dd Mon Sep 17 00:00:00 2001 From: Wellinator Date: Sun, 14 Aug 2022 16:52:06 -0300 Subject: [PATCH] refactor: implroves ray box collision accuracy --- engine/src/physics/ray.cpp | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/engine/src/physics/ray.cpp b/engine/src/physics/ray.cpp index 6fafbaa..94430ab 100644 --- a/engine/src/physics/ray.cpp +++ b/engine/src/physics/ray.cpp @@ -33,41 +33,42 @@ float Ray::distanceToPoint(const Vec4& point) const { bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner, float* outputDistance) const { - auto inv = invDir(); - inv.normalize(); + float t1 = (min->x - origin->x) / dir->x; + float t2 = (max->x - origin->x) / dir->x; + float t3 = (min->y - origin->y) / dir->y; + float t4 = (max->y - origin->y) / dir->y; + float t5 = (min->z - origin->z) / dir->z; + float t6 = (max->z - origin->z) / dir->z; - float tmin = (minCorner.x - this->origin.x) * inv.x; - float tmax = (maxCorner.x - this->origin.x) * inv.x; - float tymin = (minCorner.y - this->origin.y) * inv.y; - float tymax = (maxCorner.y - this->origin.y) * inv.y; - - if ((tmin > tymax) || (tymin > tmax)) { - return false; - } - - if (tymin > tmin) tmin = tymin; - - if (tymax < tmax) tmax = tymax; - - float tzmin = (minCorner.z - this->origin.z) * inv.z; - float tzmax = (maxCorner.z - this->origin.z) * inv.z; - - if ((tmin > tzmax) || (tzmin > tmax)) { - return false; - } - - if (tzmin > tmin) tmin = tzmin; - - if (tzmax < tmax) tmax = tzmax; + float tmin = MAX(MAX(MIN(t1, t2), MIN(t3, t4)), MIN(t5, t6)); + float tmax = MIN(MIN(MAX(t1, t2), MAX(t3, t4)), MAX(t5, t6)); + // if tmax < 0, ray (line) is intersecting AABB, but whole AABB is behing us if (tmax < 0) { + if (outputDistance != nullptr) { + *outputDistance = -1.0f; + } return false; } + // if tmin > tmax, ray doesn't intersect AABB + if (tmin > tmax) { + if (outputDistance != nullptr) { + *outputDistance = -1.0f; + } + return false; + } + + if (tmin < 0) { + if (outputDistance != nullptr) { + *outputDistance = tmax; + } + return true; + } + if (outputDistance != nullptr) { - *outputDistance = tmin >= 0 ? tmin : tmax; + *outputDistance = tmin; } - return true; }