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.
This commit is contained in:
Kawe Mazidjatari 2024-08-13 23:26:31 +02:00
parent 3e7ca699e0
commit 47bb578c03
2 changed files with 29 additions and 0 deletions

View File

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

View File

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