# Field of view check

This topic is 1945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a small function:

bool CanSee(UnitInstance * Me, Vector3& Him)
{
Vector3 NN1 = Me->Pos - Him;
Vector3 NN2 = Me->PrevVel;

NN1.normalize();
NN2.normalize();

return(Vector3::dot(NN1, NN2) < 0.0f);
}


This gives me too wide of a field of view check, this is between enemy and player used for stealth system.

How can I make it a lesser FOV?

##### Share on other sites

I don't understand why you use velocity for this. If your velocity is zero, you can't normalise it.

Normally a FOV check is done by taking the dot product between your (normalised) relative position from an enemy and their facing direction.

The dot product of 2 normalised vectors returns the cosine of the angle between them (angle is in radians of course), the check is normally something like

if(dot(normalisedRelativePosition, enemyFacing) >= cos(HalfAngleOfVisionConeInRadians)) /* in cone of vision */;

The check uses >= since with a 0 radian view angle you are only visible if your position is dead ahead of the enemy facing (i.e. the dot product is 1, which is cos(0)), and if the angle is pi then you are always in the cone of vision (since cos(pi) = -1).

EDIT: Note - the cone angle is the half angle (i.e. angle from the direction vector to the cone edge).

EDIT2: Several corrections ;)

##### Share on other sites

Thanks for the info

PrevVel is actually a velocity vector for the main player, it is assumed to be above 0.

##### Share on other sites

Oh I see, you are using your velocity as your facing, and the cone check is for you being able to see the enemy... is that what you want?

What I said still applies anyway, use the cosine of the half cone angle. Since you calculated the vector from the enemy to you, which is opposite direction to the facing if the enemy is in front of you, reverse the sign of the dot product check so use <= instead of >=.

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 21
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631763
• Total Posts
3002186
×