Skip to content

Commit

Permalink
server: func_tank: prevent domain error on barrel adjust (#328)
Browse files Browse the repository at this point in the history
When target is too close to the tank origin, `d2 - r2` expression may become negative
causing domain error on square root, and poisoning other fields and even other entities with NaN
  • Loading branch information
a1batross authored Sep 18, 2022
1 parent 99dc2c5 commit 1394637
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dlls/func_tank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,14 @@ void CFuncTank::AdjustAnglesForBarrel( Vector &angles, float distance )
if( m_barrelPos.y )
{
r2 = m_barrelPos.y * m_barrelPos.y;
angles.y += ( 180.0f / M_PI_F ) * atan2( m_barrelPos.y, sqrt( d2 - r2 ) );
if( d2 > r2 )
angles.y += ( 180.0f / M_PI_F ) * atan2( m_barrelPos.y, sqrt( d2 - r2 ) );
}
if( m_barrelPos.z )
{
r2 = m_barrelPos.z * m_barrelPos.z;
angles.x += ( 180.0f / M_PI_F ) * atan2( -m_barrelPos.z, sqrt( d2 - r2 ) );
if( d2 > r2 )
angles.x += ( 180.0f / M_PI_F ) * atan2( -m_barrelPos.z, sqrt( d2 - r2 ) );
}
}
}
Expand Down

0 comments on commit 1394637

Please sign in to comment.