In view cone snippet

From Valve Developer Community
Revision as of 02:01, 13 October 2009 by One3yed (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Snippet


bool CMyEnt::InViewCone( const Vector &vecSpot, float flTolerance )
{
    //Get the direction of origin in front of us
    Vector vDirection = ( vecSpot - GetAbsOrigin() );
    VectorNormalize( vDirection );

    //Get our forward direction
    Vector vForward;
    AngleVectors( GetAbsAngles(), &vForward );

    //Compute angle of both direction vectors
    float flDot = DotProduct( vDirection, vForward );

    //Check if angle is within tolerance 
    //-1 = opposite of forward direction
    //1 = matching forward direction
    //0 = 90 degrees 
    if ( flDot > flTolerance )
	return true;
    return false;
}


Extra

To pass in an angle of degrees, you must convert it.


//90 Degrees
flTolerance = cos( 90 ); 

bool bInCone = InViewCone( vOrigin, flTolerance );