FOV / Field of Vision for NPC AI


In the playground below, click on the ground to rotate the green cube. This will change visibility of the red cube depending on green’s fov.

In terms of code, the important bit is at the top “VisibilityController”.

FOV for NPCs is important for answering the question: can agent A see other agent B? You need to know this for

a) AI because an enemy should not shoot at the player if the enemy cannot see him
b) rendering, e.g., when player agents all face away from an enemy, he should not be visible

Why do we need this here? There is tons of examples all over the internet. But this one here works in Babylon right of the bat.

Plus if you are a bit thick as I was, you know, I knew all of this: Linear algebra for game developers ~ part 2 - Wolfire Games Blog

And yet I could not get it to work in Babylon. It turned out that I kept feeding in the mesh’s rotation vector (of axis angles) whereas I should have fed the direction vector!

The other tiny bit I missed was that the dot product returns a cosine - not an angle (yeah I know, it says it right there in the link above).

Improvements, critique, etc. welcome :slight_smile:

Best wishes


Hey Joe!

You might want to check these two examples created using the YUKA AI library:

Much more is coming in a day or two. Watch out for the release of the full YUKA + BabylonJS examples on the forum.




Found a bug. Arccos needs to be in range -1 to 1 (Inverse trigonometric functions - Wikipedia). I guess due to some Javascripts rounding/floating point shenanigans, the way I use BABYLON.Vector3.Dot() can lead to returning something like 1.000000002. This throws off JavaScripts Math.acos which returns a NaN.

Fix here: