In view cone snippet: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Added Degrees conversion method)
m (clean up, added orphan, deadend tags)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Multiple issues|
{{Dead end|date=January 2024}}
{{Orphan|date=January 2024}}
}}
== Snippet ==
== Snippet ==
<pre>


<source lang="cpp">
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;
     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;
Line 17: Line 29:
}
}


</pre>
</source>
 


== Extra ==
== Extra ==
To pass in an angle of degrees, you must convert it.
To pass in an angle of degrees, you must convert it.
<pre>


<source lang="cpp">
//90 Degrees
//90 Degrees
flTolerance = cos( 90 );  
flTolerance = cos( 90 );


bool bInCone = InViewCone( vOrigin, flTolerance );
bool bInCone = InViewCone( vOrigin, flTolerance );


</pre>
</source>


[[Category:Snippets]]
[[Category:Snippets]]

Latest revision as of 10:05, 21 January 2024

Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

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