In view cone snippet: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
Thunder4ik (talk | contribs) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
bool CMyEnt::InViewCone( const Vector &vecSpot, float | {{Multiple issues| | ||
{{Dead end|date=January 2024}} | |||
{{Orphan|date=January 2024}} | |||
}} | |||
== Snippet == | |||
<source lang="cpp"> | |||
bool CMyEnt::InViewCone( const Vector &vecSpot, float flTolerance ) | |||
{ | { | ||
Vector | //Get the direction of origin in front of us | ||
Vector vDirection = ( vecSpot - GetAbsOrigin() ); | |||
VectorNormalize( | VectorNormalize( vDirection ); | ||
if ( flDot > | //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 true; | ||
return false; | return false; | ||
} | } | ||
</source> | |||
== Extra == | |||
To pass in an angle of degrees, you must convert it. | |||
<source lang="cpp"> | |||
//90 Degrees | |||
flTolerance = cos( 90 ); | |||
bool bInCone = InViewCone( vOrigin, flTolerance ); | |||
</source> | |||
[[Category:Snippets]] |
Latest revision as of 10:05, 21 January 2024

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)

This article has no
links to other VDC articles. Please help improve this article by adding links
that are relevant to the context within the existing text.
January 2024


January 2024

This article is an orphan, meaning that few or no articles link to it.
You can help by
adding links to this article from other relevant articles.
January 2024
You can help by

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