In view cone snippet: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 4: Line 4:
bool CMyEnt::InViewCone( const Vector &vecSpot, float flTolerance )
bool CMyEnt::InViewCone( const Vector &vecSpot, float flTolerance )
{
{
     Vector los = ( vecSpot - GetAbsOrigin() );
    //Get the direction of origin in front of us
    //los.z = 0;  //not needed.
     Vector vDirection = ( vecSpot - GetAbsOrigin() );
     VectorNormalize( los );
     VectorNormalize( vDirection );
    Vector facingDir;
    AngleVectors( GetAbsAngles(), &facingDir );


     float flDot = DotProduct( los, facingDir );
     //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 )
     if ( flDot > flTolerance )
return true;
return true;

Revision as of 19:01, 12 October 2009

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 );