From 47bb578c036ed0bfe0eb71f725ee10a1b930b7af Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 13 Aug 2024 23:26:31 +0200 Subject: [PATCH] Recast: add helper functions for ledge spans rdCalcMaxLOSAngle: calculate max angle before LOS gets blocked. rdCalcLedgeSpanOffsetAmount: calculate offset towards ledge to keep object in LOS. --- .../recast/Shared/Include/SharedCommon.h | 13 +++++++++++++ .../recast/Shared/Source/SharedCommon.cpp | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/thirdparty/recast/Shared/Include/SharedCommon.h b/src/thirdparty/recast/Shared/Include/SharedCommon.h index bbbe3707..270d45ec 100644 --- a/src/thirdparty/recast/Shared/Include/SharedCommon.h +++ b/src/thirdparty/recast/Shared/Include/SharedCommon.h @@ -454,6 +454,19 @@ void rdPerpDirEdge2D(const float* dir, const bool inner, float* out); /// @param[out] out The resulting direction. [(x, y)] void rdPerpDirPtEdge2D(const float* v1, const float* v2, const bool inner, float* out); +/// Derives the maximum angle in which an object on an elevated surface can be seen from below. +/// @param[in] ledgeSpan The distance between the edge of the object and the edge of the ledge. +/// @param[in] objectHeight The height of the object. +/// @return The maximum angle before LOS gets blocked. +float rdCalcMaxLOSAngle(const float ledgeSpan, const float objectHeight); + +/// Determines the amount we need to offset an object to maintain LOS from an angle, with a maximum. +/// @param[in] ledgeSpan The distance between the edge of the object and the edge of the ledge. +/// @param[in] slopeAngle The slope angle to test. +/// @param[in] maxAngle The maximum angle in degrees. +/// @return The amount we need to offset to maintain LOS. +float rdCalcLedgeSpanOffsetAmount(const float ledgeSpan, const float slopeAngle, const float maxAngle); + unsigned char rdClassifyPointOutsideBounds(const float* pt, const float* bmin, const float* bmax); unsigned char rdClassifyPointInsideBounds(const float* pt, const float* bmin, const float* bmax); diff --git a/src/thirdparty/recast/Shared/Source/SharedCommon.cpp b/src/thirdparty/recast/Shared/Source/SharedCommon.cpp index 957c352a..d00edefc 100644 --- a/src/thirdparty/recast/Shared/Source/SharedCommon.cpp +++ b/src/thirdparty/recast/Shared/Source/SharedCommon.cpp @@ -432,6 +432,22 @@ void rdPerpDirPtEdge2D(const float* v1, const float* v2, const bool inner, float rdPerpDirEdge2D(dir, inner, out); } +float rdCalcMaxLOSAngle(const float ledgeSpan, const float objectHeight) +{ + const float angleRad = rdMathAtan2f(objectHeight, ledgeSpan); + const float angleDeg = angleRad * (180.0f/RD_PI); + + return angleDeg; +} + +float rdCalcLedgeSpanOffsetAmount(const float ledgeSpan, const float slopeAngle, const float maxAngle) +{ + const float clampedAngle = rdClamp(slopeAngle, slopeAngle, maxAngle); + const float offset = ledgeSpan * (clampedAngle / maxAngle); + + return offset; +} + static const unsigned char XP = 1 << 0; static const unsigned char ZP = 1 << 1; static const unsigned char XM = 1 << 2;