Field of view check

Started by
2 comments, last by Paradigm Shifter 11 years ago

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?

Advertisement

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Thanks for the info

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

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 >=.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement