From 20fe6c4877524e9b39aafa8cc8da58b945538b63 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 4 Mar 2023 17:32:05 +0800 Subject: [PATCH] Fixed incorrect distances for AABB raycast results --- SHADE_Engine/src/Math/Geometry/SHAABB.cpp | 5 +++++ SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Math/Geometry/SHAABB.cpp b/SHADE_Engine/src/Math/Geometry/SHAABB.cpp index f5d9fd60..99e0aebe 100644 --- a/SHADE_Engine/src/Math/Geometry/SHAABB.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHAABB.cpp @@ -155,6 +155,11 @@ namespace SHADE SHRaycastResult result; result.hit = Intersects(ray.position, ray.direction, result.distance); + + // Negative distances are invalid, therefore false + if (result.distance < 0.0f) + result.hit = false; + if (result.hit) { result.position = ray.position + ray.direction * result.distance; diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp index 508f8807..d996c470 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp @@ -197,7 +197,9 @@ namespace SHADE // Load start and end points into the container for debug drawing #ifdef SHEDITOR - SHVec3 endPos = info.ray.position + info.ray.direction * SHRay::MAX_RAYCAST_DIST; + // Default end pos is how far the ray was casted. + const float DISTANCE = info.distance == std::numeric_limits::infinity() ? SHRay::MAX_RAYCAST_DIST : info.distance; + SHVec3 endPos = info.ray.position + info.ray.direction * DISTANCE; if (!results.empty()) endPos = results.back().position;