From a6d4a5302118cf3dc39237bc12e3f137b23168c6 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 16 Sep 2024 16:29:46 +0200 Subject: [PATCH] Recast: make proper wrappers for radians and degrees conversion Avoid bugs where radians gets converted to radians, or degrees to degrees. --- .../recast/Detour/Source/DetourNavMesh.cpp | 4 ++-- .../recast/Shared/Include/SharedCommon.h | 20 ++++++++++++++----- .../recast/Shared/Source/SharedCommon.cpp | 4 ++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/thirdparty/recast/Detour/Source/DetourNavMesh.cpp b/src/thirdparty/recast/Detour/Source/DetourNavMesh.cpp index 0a4dd919..82a900e7 100644 --- a/src/thirdparty/recast/Detour/Source/DetourNavMesh.cpp +++ b/src/thirdparty/recast/Detour/Source/DetourNavMesh.cpp @@ -2198,12 +2198,12 @@ float dtCalcOffMeshRefYaw(const float* spos, const float* epos) const float dy = epos[1]-spos[1]; const float yawDeg = rdMathAtan2f(dy, dx); - return yawDeg * (180.0f/RD_PI); + return rdDegToRad(yawDeg); } void dtCalcOffMeshRefPos(const float* spos, float yawRad, float offset, float* res) { - const float yawDeg = yawRad * (180.0f/RD_PI); + const float yawDeg = rdRadToDeg(yawRad); const float dx = offset*rdMathCosf(yawDeg); const float dy = offset*rdMathSinf(yawDeg); diff --git a/src/thirdparty/recast/Shared/Include/SharedCommon.h b/src/thirdparty/recast/Shared/Include/SharedCommon.h index c0a37c2a..0733e51a 100644 --- a/src/thirdparty/recast/Shared/Include/SharedCommon.h +++ b/src/thirdparty/recast/Shared/Include/SharedCommon.h @@ -41,11 +41,6 @@ feature to find minor members. /// @param [in] _ Unused parameter template void rdIgnoreUnused(const T&) { } -/// Tests a specific bit in a bit cell -/// @param[in] i The bit number -/// @return The offset mask for the bit. -inline int rdBitCellBit(const int i) { return (1 << ((i) & (RD_BITS_PER_BIT_CELL-1))); } - /// Swaps the values of the two parameters. /// @param[in,out] a Value A /// @param[in,out] b Value B @@ -80,6 +75,21 @@ template inline T rdAbs(T a) { return a < 0 ? -a : a; } /// @return The square of the value. template inline T rdSqr(T a) { return a * a; } +/// Converts value from Degrees to Radians. +/// @param[in] x The value to convert. +/// @return The input value as Radians. +inline float rdDegToRad(const float x) { return x * (RD_PI/180.0f); } + +/// Converts value from Radians to Degrees. +/// @param[in] x The value to convert. +/// @return The input value as Degrees. +inline float rdRadToDeg(const float x) { return x * (180.0f/RD_PI); } + +/// Tests a specific bit in a bit cell +/// @param[in] i The bit number +/// @return The offset mask for the bit. +inline int rdBitCellBit(const int i) { return (1 << ((i) & (RD_BITS_PER_BIT_CELL-1))); } + /// @} /// @name Vector helper functions. /// @{ diff --git a/src/thirdparty/recast/Shared/Source/SharedCommon.cpp b/src/thirdparty/recast/Shared/Source/SharedCommon.cpp index 5b18f0f3..48658206 100644 --- a/src/thirdparty/recast/Shared/Source/SharedCommon.cpp +++ b/src/thirdparty/recast/Shared/Source/SharedCommon.cpp @@ -29,7 +29,7 @@ float rdCalcSlopeAngle(const float* v1, const float* v2) const float horizontalDistance = rdMathSqrtf((deltaX*deltaX)+(deltaY*deltaY)); const float slopeAngleRadians = rdMathAtan2f(deltaZ, horizontalDistance); - const float slopeAngleDegrees = slopeAngleRadians*(180.0f/RD_PI); + const float slopeAngleDegrees = rdRadToDeg(slopeAngleRadians); return slopeAngleDegrees; } @@ -469,7 +469,7 @@ float rdCalcEdgeOverlap2D(const float* edge1Start, const float* edge1End, float rdCalcMaxLOSAngle(const float ledgeSpan, const float objectHeight) { const float angleRad = rdMathAtan2f(objectHeight, ledgeSpan); - const float angleDeg = angleRad * (180.0f/RD_PI); + const float angleDeg = rdRadToDeg(angleRad); return angleDeg; }