2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-25 11:19:39 +01:00

server: func_tank: prevent domain error on barrel adjust (#328)

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
This commit is contained in:
Alibek Omarov 2022-09-18 23:49:58 +03:00 committed by GitHub
parent 99dc2c519f
commit 1394637ce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -626,11 +626,13 @@ void CFuncTank::AdjustAnglesForBarrel( Vector &angles, float distance )
if( m_barrelPos.y ) if( m_barrelPos.y )
{ {
r2 = m_barrelPos.y * m_barrelPos.y; r2 = m_barrelPos.y * m_barrelPos.y;
if( d2 > r2 )
angles.y += ( 180.0f / M_PI_F ) * atan2( m_barrelPos.y, sqrt( d2 - r2 ) ); angles.y += ( 180.0f / M_PI_F ) * atan2( m_barrelPos.y, sqrt( d2 - r2 ) );
} }
if( m_barrelPos.z ) if( m_barrelPos.z )
{ {
r2 = m_barrelPos.z * m_barrelPos.z; r2 = m_barrelPos.z * m_barrelPos.z;
if( d2 > r2 )
angles.x += ( 180.0f / M_PI_F ) * atan2( -m_barrelPos.z, sqrt( d2 - r2 ) ); angles.x += ( 180.0f / M_PI_F ) * atan2( -m_barrelPos.z, sqrt( d2 - r2 ) );
} }
} }