In view cone snippet: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
Frying1Pans (talk | contribs) m (Fixes page formatting to allow code to be properly shown.) |
||
Line 1: | Line 1: | ||
== Snippet == | == Snippet == | ||
<source lang="cpp"> | |||
bool CMyEnt::InViewCone( const Vector &vecSpot, float flTolerance ) | bool CMyEnt::InViewCone( const Vector &vecSpot, float flTolerance ) | ||
{ | { | ||
Line 24: | Line 24: | ||
} | } | ||
</ | </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. | ||
<source lang="cpp"> | |||
//90 Degrees | //90 Degrees | ||
flTolerance = cos( 90 ); | flTolerance = cos( 90 ); | ||
Line 36: | Line 36: | ||
bool bInCone = InViewCone( vOrigin, flTolerance ); | bool bInCone = InViewCone( vOrigin, flTolerance ); | ||
</ | </source> | ||
[[Category:Snippets]] | [[Category:Snippets]] |
Revision as of 14:52, 1 May 2021
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 );