Added math helpers for checking if a number is infinity or invalid

This commit is contained in:
Diren D Bharwani 2023-03-04 19:50:53 +08:00
parent 5a2401bec2
commit 9b2c5a1804
3 changed files with 24 additions and 9 deletions

View File

@ -105,6 +105,12 @@ namespace SHADE
template <IsFloatingPoint T = float>
[[nodiscard]] static bool CompareFloat (T lhs, T rhs, T absTolerance = EPSILON, T relTolerance = EPSILON);
template <IsArithmetic T>
[[nodiscard]] static bool IsInfinity (T value);
template <IsArithmetic T>
[[nodiscard]] static bool IsNaN (T value);
private:
/*---------------------------------------------------------------------------------*/
/* Static Data Members */

View File

@ -126,4 +126,21 @@ namespace SHADE
return std::fabs(lhs - rhs) <= Max(absTolerance, RTOL);
}
template <IsArithmetic T>
bool SHMath::IsInfinity(T value)
{
const float MAX_VALUE = std::numeric_limits<T>::max();
const float MIN_VALUE = std::numeric_limits<T>::min();
return !(MIN_VALUE <= value && value <= MAX_VALUE);
}
template <IsArithmetic T>
bool SHMath::IsNaN(T value)
{
return value != value;
}
} // namespace SHADE

View File

@ -18,14 +18,6 @@
#include "Physics/SHPhysicsConstants.h"
#include "Physics/Collision/Narrowphase/SHCollisionUtils.h"
bool IsInfinity(float value)
{
const float MAX_VAL = std::numeric_limits<float>::max();
const float MIN_VAL = -MAX_VAL;
return !(MIN_VAL <= value && value <= MAX_VAL);
}
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
@ -227,7 +219,7 @@ namespace SHADE
errorBias = -SHPHYSICS_BAUMGARTE * INV_DT * std::max(0.0f, contact.penetration - SHPHYSICS_LINEAR_SLOP);
// TODO: This should never occur. Move this check into the ConvexVSConvex to find the error.
if (IsInfinity(errorBias))
if (SHMath::IsInfinity(errorBias))
errorBias = 0.0f;
// Warm starting