Skip to content

Commit

Permalink
Add Is_Nan function to build with/without gamemath enabled (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
abramann authored May 24, 2023
1 parent c8b118e commit 5529abc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
12 changes: 8 additions & 4 deletions src/game/common/thing/thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void Thing::Set_Position_Z(float z)
React_To_Transform_Change(&old_tm, &old_pos, old_angle);
}

captainslog_dbgassert(!gm_isnanf(Get_Position()->x) && !gm_isnanf(Get_Position()->y) && !gm_isnanf(Get_Position()->z),
captainslog_dbgassert(
!GameMath::Is_Nan(Get_Position()->x) && !GameMath::Is_Nan(Get_Position()->y) && !GameMath::Is_Nan(Get_Position()->z),
"Drawable/Object position NAN! \'%s\'",
m_template->Get_Name().Str());
}
Expand Down Expand Up @@ -175,7 +176,8 @@ void Thing::Set_Orientation(float angle)
m_cachedPos = pos;
m_cacheFlags &= ~VALID_DIRVECTOR;
React_To_Transform_Change(&old_tm, &old_pos, old_angle);
captainslog_dbgassert(!gm_isnanf(Get_Position()->x) && !gm_isnanf(Get_Position()->y) && !gm_isnanf(Get_Position()->z),
captainslog_dbgassert(
!GameMath::Is_Nan(Get_Position()->x) && !GameMath::Is_Nan(Get_Position()->y) && !GameMath::Is_Nan(Get_Position()->z),
"Drawable/Object position NAN! \'%s\'",
m_template->Get_Name().Str());
}
Expand All @@ -198,7 +200,8 @@ void Thing::Set_Position(const Coord3D *pos)
React_To_Transform_Change(&old_tm, &old_pos, old_angle);
}

captainslog_dbgassert(!gm_isnanf(Get_Position()->x) && !gm_isnanf(Get_Position()->y) && !gm_isnanf(Get_Position()->z),
captainslog_dbgassert(
!GameMath::Is_Nan(Get_Position()->x) && !GameMath::Is_Nan(Get_Position()->y) && !GameMath::Is_Nan(Get_Position()->z),
"Drawable/Object position NAN! \'%s\'",
m_template->Get_Name().Str());
}
Expand All @@ -216,7 +219,8 @@ void Thing::Set_Transform_Matrix(const Matrix3D *mx)
m_cacheFlags = 0;
React_To_Transform_Change(&old_tm, &old_pos, old_angle);

captainslog_dbgassert(!gm_isnanf(Get_Position()->x) && !gm_isnanf(Get_Position()->y) && !gm_isnanf(Get_Position()->z),
captainslog_dbgassert(
!GameMath::Is_Nan(Get_Position()->x) && !GameMath::Is_Nan(Get_Position()->y) && !GameMath::Is_Nan(Get_Position()->z),
"Drawable/Object position NAN! \'%s\'",
m_template->Get_Name().Str());
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/logic/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ bool Angle_Changed(float f1, float f2)

void Object::React_To_Transform_Change(const Matrix3D *tm, const Coord3D *pos, float angle)
{
if (gm_isnanf(Get_Position()->x) || gm_isnanf(Get_Position()->y) || gm_isnanf(Get_Position()->z)) {
if (GameMath::Is_Nan(Get_Position()->x) || GameMath::Is_Nan(Get_Position()->y) || GameMath::Is_Nan(Get_Position()->z)) {
captainslog_dbgassert(false, "Object pos is nan.");
g_theGameLogic->Destroy_Object(this);
}
Expand Down
12 changes: 7 additions & 5 deletions src/game/logic/object/update/physicsupdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ UpdateSleepTime PhysicsBehavior::Update()
tm.Adjust_Z_Translation(m_vel.z);
}

if (gm_isnanf(tm.Get_X_Translation()) || gm_isnanf(tm.Get_Y_Translation()) || gm_isnanf(tm.Get_Z_Translation())) {
if (GameMath::Is_Nan(tm.Get_X_Translation()) || GameMath::Is_Nan(tm.Get_Y_Translation())
|| GameMath::Is_Nan(tm.Get_Z_Translation())) {
captainslog_dbgassert(false, "Object position is NAN, deleting.");
g_theGameLogic->Destroy_Object(obj);
}
Expand Down Expand Up @@ -570,7 +571,8 @@ void PhysicsBehavior::On_Collide(Object *other, Coord3D const *loc, Coord3D cons
force.x = f3 * center_diff.x / f1;
force.y = f3 * center_diff.y / f1;
force.z = f3 * center_diff.z / f1;
captainslog_dbgassert(!gm_isnanf(force.x) && !gm_isnanf(force.y) && !gm_isnanf(force.z),
captainslog_dbgassert(!GameMath::Is_Nan(force.x) && !GameMath::Is_Nan(force.y)
&& !GameMath::Is_Nan(force.z),
"PhysicsBehavior::onCollide force NAN!");
Apply_Force(&force);
}
Expand Down Expand Up @@ -726,10 +728,10 @@ void PhysicsBehavior::Apply_Gravitational_Forces()

void PhysicsBehavior::Apply_Force(const Coord3D *force)
{
captainslog_dbgassert(
!gm_isnanf(force->x) && !gm_isnanf(force->y) && !gm_isnanf(force->z), "PhysicsBehavior::applyForce force NAN!");
captainslog_dbgassert(!GameMath::Is_Nan(force->x) && !GameMath::Is_Nan(force->y) && !GameMath::Is_Nan(force->z),
"PhysicsBehavior::applyForce force NAN!");

if (!gm_isnanf(force->x) && !gm_isnanf(force->y) && !gm_isnanf(force->z)) {
if (!GameMath::Is_Nan(force->x) && !GameMath::Is_Nan(force->y) && !GameMath::Is_Nan(force->z)) {
float mass = Get_Mass();
Coord3D new_force = *force;

Expand Down
45 changes: 27 additions & 18 deletions src/w3d/math/gamemath.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,6 @@ extern const Array<float, ARC_TABLE_SIZE> _FastAsinTable;
extern const Array<float, SIN_TABLE_SIZE> _FastSinTable;
extern const Array<float, SIN_TABLE_SIZE> _FastInvSinTable;

inline float Normalize_Angle(float angle)
{
captainslog_dbgassert(!gm_isnanf(angle), "Angle is NAN in normalizeAngle!\n");
if (gm_isnanf(angle)) {
return 0.0f;
}

while (angle > GAMEMATH_PI) {
angle = angle - GAMEMATH_PI * 2;
}

while (angle <= -GAMEMATH_PI) {
angle = angle + GAMEMATH_PI * 2;
}

return angle;
}

namespace GameMath
{
void Init();
Expand Down Expand Up @@ -547,4 +529,31 @@ inline int Lrintf(float val)
// return true;
//}

inline bool Is_Nan(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_isnanf(val) != 0;
#else
return std::isnan(val);
#endif
}

} // namespace GameMath

inline float Normalize_Angle(float angle)
{
captainslog_dbgassert(!GameMath::Is_Nan(angle), "Angle is NAN in normalizeAngle!\n");
if (GameMath::Is_Nan(angle)) {
return 0.0f;
}

while (angle > GAMEMATH_PI) {
angle = angle - GAMEMATH_PI * 2;
}

while (angle <= -GAMEMATH_PI) {
angle = angle + GAMEMATH_PI * 2;
}

return angle;
}

0 comments on commit 5529abc

Please sign in to comment.