Saturday, February 16, 2013

Field Of View (FOV) in ActionScript

One of the simplest ways of calculating Field Of View (FOV) in a roguelike is also my favorite. There's several different algorithms but I like to keep it simple. Just check every tile within a view radius of the player. If you know how to calculate Line Of Sight, then it's very easy. If the line to the end point isn't blocked, then the end point is visible. Once you can check line of sight between arbitrary points, then it's easy.

Here's a Flash implementation. Use your mouse to see what's viewable from a location.

It's probably the least efficient way to do it - which is why it takes a second or so to calculate. It is super simple to understand and implement though. I also think it looks the best. Not only can this be used for seeing which tiles are visible from a specific tile but it can also be used for seeing what's affected by an explosion or other effect.

Once you have performance problems you can switch to a more efficient algorithm or rely on caching.

2 comments:

  1. Some random thoughts:

    Dungeon Crawl Stone Soup uses a different approach to FOV calculation, so a character can look into corridors while standing around a corner, like so:

    ###x#######
    ++++>>>>>>>
    ###########

    I think this amounts to allowing multiple "paths" to target tiles when determining visibility.

    Second, have you considered using a square FOV? If moving diagonally is the same distance as horizontally in your game, it makes sense.

    Curious what you come up with for the 7DRL

    ReplyDelete
    Replies
    1. I always forget about square FOV. It makes more sense, but it always bugs me a little. Good point though.

      Delete