Sign in to follow this  
wildboar

Field of view check

Recommended Posts

wildboar    320

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 this post


Link to post
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 ;)

Edited by Paradigm Shifter

Share this post


Link to post
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 >=.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this