Recast: make proper wrappers for radians and degrees conversion

Avoid bugs where radians gets converted to radians, or degrees to degrees.
This commit is contained in:
Kawe Mazidjatari 2024-09-16 16:29:46 +02:00
parent 10efa56c58
commit a6d4a53021
3 changed files with 19 additions and 9 deletions

View File

@ -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);

View File

@ -41,11 +41,6 @@ feature to find minor members.
/// @param [in] _ Unused parameter
template<class T> 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<class T> inline T rdAbs(T a) { return a < 0 ? -a : a; }
/// @return The square of the value.
template<class T> 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.
/// @{

View File

@ -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;
}