Determining 3D Field of View by position

XNA Mini-Lesson -- Difficulty: Easy

July 14th, 2007 -- Determining 3D Field of View by position

In some of the previous lessons I'm mentioned that you can use field-of-view to cull out 3D objects from view for optimization. However I need really elaborated on how that is done. Well, it is actually quite simple:

/// <summary>
/// Checks whether an object is in the field of view of another
/// </summary>
/// <param name="posFirst">Source object 3d's position</param>
/// <param name="facingFirst">Source object's facing direction vector</param>
/// <param name="posSecond">Target object 3d's position</param>
/// <param name="fov">Field of view angle of source object in radians</param>
/// <returns>True/false whether or not the second object is in the FOV of the first</returns>

public bool IsSecondInFOVofFirst(Vector3 posFirst, Vector3 facingFirst, Vector3 posSecond, double fov)
{
    // Find direction from the first position to the second
    Vector3 tempVect = posSecond - posFirst;
    tempVect.Normalize();

    // Returns if the target is in the FOV of the source, here the FOV is the 'fov' parameter.
    return
Vector3.Dot(facingFirst, tempVect) >= Math.Cos(fov);
}

- posFirst would be the position of the reference object, usually the active player.
- facingFirst is the forward vector of the reference object.
- posSecond is the position of the target object.
- 'fov' is the angle in radians from the center of the source's view. For a 90 degree field of view, you would use 45 degrees as the parameter. This is because it would mean 45 degrees from the center of view, in both directions.

In the figure above you can see that, in this situation, using just this simple function we can eliminate 3 of the models (the blue ones) that would've been sent to the graphics card.

Field-of-view has many uses, using it for view culling is just one of them. I use it for an aim-assist in one of my games. You simply lower the FOV angle in the function to 3 or 4 and if the player is reasonably on target then you can have the vector of the weapon aim changed to your difference in vectors (which is actually inside our function above as 'tempVect'. Field-of-view is also used in A.I. A bot uses its senses to determine its wants and needs, using field of view you can help the bot determine what objects it sees, and then go from there.

UPDATE (08-28-07):
Tip: This method is useful when using positions to determine what should be in view, and as such is not very math intensive. For something more accurate with 3D models you may use the models bounding spheres against a bounding frustum to determine true visibility. Bounding frustums must be re-calculated anytime the player's view or camera view changes, however it is fairly quick comparitively when used against bounding spheres, and slightly slower I believe against bounding boxes. Unforunately I don't have time for a lesson on that yet, but I do use frustum view culling for my quad-tree terrain in the scene creator sample. I also go into some depth on this in the View Culling lesson.

Next