From Valve Developer Community
Jump to: navigation, search

UTIL_AngleDistance does a similar thing to UTIL_AngleDiff, but does not clamp in a range from -180° to 180°. Instead, it brings them up to 360° closer to these values.

Note.png Note: You'll probably want to use UTIL_AngleDiff instead!


// Purpose: Returns the difference between two values as a float. 
// Input  : next - Value "B" to compare against
// Input  : cur - Value "A" to compare with
// Output : float - Difference between the two values, interpreted as an angle
inline float UTIL_AngleDistance( float next, float cur )


//Note! In this example they used the UNCLAMPED version (UTIL_AngleDistance is UNCLAMPED),
//Instead of the pre-clamped version (UTIL_AngleDiff)
//They then clamp the value. This is only provided for documentation's sake
//But it's probably just easier to use UTIL_AngleDiff which will always be between -180 and 180.

// Move toward target at rate or less
float distX = UTIL_AngleDistance( angles.x, GetLocalAngles().x );
vecAngVel.x = distX  * 10;
vecAngVel.x = clamp( vecAngVel.x, -m_pitchRate, m_pitchRate );
SetLocalAngularVelocity( vecAngVel );