Comparative study of field of view algorithms for 2D grid based worlds
v1.2 J.C.Wilk, February 2009 (The original FOW study in a PDF located here.)
Computing field of view is a frequent problem in video games. It consists of the determination of which part of the world is visible from a certain position. This paper focus on 2D grid based worlds, in other words, worlds represented by a 2D grid of square cells. This type of worlds can be found in a range of video games, including but not limited to 2D isometric games and text console based games like roguelikes.
While there are lots of different algorithms to solve this problem, not much has been written about the difference between those algorithms, in term of gameplay or speed.
This is not a comprehensive study of all available algorithms. Instead, I will focus on following popular or innovative algorithms:
- basic raycasting [BASIC]
- diamond raycasting [DIAMOND]
- recursive shadowcasting [SHADOW]
- precise permissive fov [PERMISSIVE]
- digital fov [DIGITAL]
Note that basic raycasting alone generates too many artifacts to be really usable. The algorithm tested here use a post- processing step to remove most wall lighting artifacts.
In this version of the study, permissive has been replaced by an enhanced version of the algorithm, from the same author, with a variable permissiveness parameter. This parameter can take values in the range 0-8, 0 being the less permissive and 8 being equivalent to the standard precise permissive algorithm.
All tests below are done using the C++ wrapper for the Doryen library [LIBTCOD] and the fov algorithms implementations it contains. Note that digital fov is no more in the library, but its source code is still on libtcod's svn repository.
We call a wall cell surrounded with empty cells a pillar. First, let's see how the fov looks when the viewer's cell is adjacent to the pillar:
DIAMOND and DIGITAL result in a shadow limited to a single line, which makes it almost impossible for a creature to sneak from the east side of the map to the @ position without being noticed.
PERMISSIVE offers any shadow angle. In particular, PERMISSIVE0 has the same shadow angle as SHADOW.
PERMISSIVE2 the same as BASIC.
Now let's see the behavior when the viewer is a few cells away from the pillar:
BASIC and SHADOW still have a triangular shadow, but the cells near the pillar are now completely in the field of view.
DIAMOND still has a shadow limited to a single line.
DIGITAL don't even have a line shadow.
Once gain PERMISSIVE is very similar to SHADOW/BASIC with low permissiveness and is equivalent to DIGITAL with maximum permissiveness.
The conclusion is that if you want to use pillars for stealth gameplay, you have to choose either BASIC or SHADOW or PERMISSIVE with low permissiveness. You can still use the other algorithms provided you use 2x2 pillars instead of single cell pillars. Not that no algorithm provides a good looking shadow.
Corner peeking involves seeing a corridor when you're standing at a T junction.
BASIC and SHADOW don't allow corner peeking. You have to step into the corridor to be able to see it. The other algorithms allow corner peeking.
Now let's see the opposite case. If you're in the corridor, will you be able to see someone hiding in the T junction ? When on a red cell, you don't see him. When on a blue cell, you see him.
|BASIC||DIAMOND||SHADOW, PERMISSIVE, DIGITAL|
For BASIC, the cell might or might not be visible, depending on the viewer position in the corridor. Most of the time, it's not in fov, but at certain position where a ray pass exactly through the T junction cell, it is in fov. This is clearly not an acceptable behavior.
DIAMOND has the better behavior here: the cell is not visible until the viewer is really close to the junction.
For other algorithms, the cell is always visible, which is counter-intuitive, but acceptable.
Most roguelikes allow diagonal movements. We can then expect the field of view to go through diagonal walls too.
BASIC and some of the PERMISSIVE have good result.
DIAMOND completely blocks the field of view, which might be a real issue if your game allows diagonal movement. Also note that if you want the field of view to be blocked by diagonal walls, you'll have to tweak any algorithm except DIAMOND and PERMISSIVE0.
SHADOW through diagonal walls is limited to a thick line, which is not very natural.
PERMISSIVE8 and DIGITAL have a 90Â° field of view through the wall, which is fine.
The measures are done on following maps:
- â€?Outdoorâ€? maps (empty maps with random 1x1 obstacles): 100x100
- â€?Indoorâ€? maps (random cave levels using algorithm [BSPDUNGEON]): 40x40
Symmetry is measured by calculating a field of view F0 on a random map from a random position P0. Then, for each cell Pi in F0, we calculate the field of view Fi from the position Pi and check that P0 is inside Fi. If not, we increase an error counter. We do this for several random maps and get the average number of error per map cell.
Green cells have no symmetry error.
Orange cells have less than 1% symmetry errors.
Red cells have more than 1% symmetry errors.
|Algorithm||Error / cell â€“ indoor (%)||Error / cell â€“ outdoor (%)|
- Obviously, symmetric algorithms have no errors.
- On permissive, symmetry is inversely proportional to permissiveness. The symmetry is really bad with permissiveness <= 4.
- All other algorithms have equivalent error rates, acceptable for indoor, but not for outdoor. The outdoor error rate is high enough to be a gameplay issue.
- Error rate is higher in outdoor maps
|Algorithm||1x1 pillar near||1x1 pillar away||Corner peeking||Inverted corner peeking||Diagonal walls||Symmetry indoor||Symmetry outdoor|
- there is no perfect algorithm amongst the ones observed
- each algorithm has its own weakness
- the resulting ranking is rather arbitrary. You should carefully check every algorithm features to see if it can fit your game.
The measures are done on following maps:
- Empty maps (worst case): 600x600, 100x100, 20x20
- Maps full of wall (best case): 600x600, 100x100, 20x20
- â€?Outdoorâ€? maps (empty maps with random 1x1 obstacles): 600x600, 100x100, 20x20
- â€?Indoorâ€? maps (random cave levels using algorithm [BSPDUNGEON]): 80x80, 40x40, 20x20
For each map type, we run 50 tests on 50 different random maps (for the worst/best cases, there's only one map for all 50 tests). For each random map, we run a number (between 10 and 2000 depending on the current test's average speed) of fov computations from different positions in the map. Each algorithm runs through the exact same set of maps/viewer positions. The cumulative time is calculated for each algorithm and the average time per computation is deduced.
The absolute speed values are not really significant. More important is the difference of speed between two algorithms on the same map and the same computer.
The color code uses following convention:
- algorithms with total time lower than 2x the fastest are green
- algorithms with total time higher than 5x the fastest are red
- the others are orange
|Empty map||600x600 (Âµs)||100x100 (Âµs)||20x20 (Âµs)|
|Full map||600x600 (Âµs)||100x100 (Âµs)||20x20 (Âµs)|
|Outdoor map||600x600 (Âµs)||100x100 (Âµs)||20x20 (Âµs)|
|Indoor map||80x80 (Âµs)||40x40 (Âµs)||20x20 (Âµs)|
- With usual visible map size in games (between 20x20 and 40x40), any algorithm but DIGITAL is fast enough for most usages.
- SHADOW is the fastest on indoor maps and the best overall choice for performances.
- BASIC is the fastest on outdoor maps
- DIGITAL is way slower than the others.
|To understand||To implement|
- There is no big winner, but SHADOW and BASIC are particularly adapted to most FOV usages except if symmetry is mandatory. They also happen to be the simplest to understand and implement.
- The new permissive fov is pretty handy and can adapt to any usage, but no permissiveness level gives perfect results and the symmetry gets really bad for low permissiveness.
- While having a reputation for being the slowest, BASIC is indeed one of the fastest.
- While it does not rank very well in this study, DIAMOND has some very interesting and unique features and it's definitely worth digging more into it to see if it can be improved.
- The final conclusion is that there is still lot of room for improvement in FOV algorithms, especially on the gameplay side...
- [BASIC] J.C.Wilk, Sep 2007. Piece of cake visibility determination algorithm: http://jice.nospam.googlepages.com/visibilitydetermination
- [DIAMOND] Modeling Rays for Line of Sight in an Object-Rich World: http://www.geocities.com/temerra/los_rays.html
- [SHADOW] Bj?¶rn Bergstr?¶m, 2001. FOV using recursive shadowcasting: http://roguebasin.roguelikedevelopment.org/index.php?title=FOV_using_recursive_shadowcasting
- [PERMISSIVE] Jonathon Duerig. Precise Permissive Field of View: http://roguebasin.roguelikedevelopment.org/index.php?title=Precise_Permissive_Field_of_View Enhanced version: http://groups.google.com/group/rec.games.roguelike.development/msg/b77fe6999651d023
- [DIGITAL] Digital field of view: http://roguebasin.roguelikedevelopment.org/index.php?title=Digital_field_of_view
- [LIBTCOD] J.C.Wilk, 2007-2009. The Doryen library: http://thedoryenlibrary.appspot.com
- [BSPDUNGEON] J.C.Wilk, Sep 2007. Basic dungeon generation: http://jice.nospam.googlepages.com/basicdungeongeneration
Some nifty screenshots.