# Field of View

Field of View is very important decision for any game. How will it be rendered? What will the player be able to see?

I decided to make it very easy, by Raytracing. I have only a 2 dimensional space, so this should not pose a performance problem. I had to copy the basics by around 90% from a tutorial, but seriously... there aren't so many different ways on how to define a point or a Line. The Field of View now goes through each Point of a Line and tells if the tile is visible or not. Everything depends on a) the Vision radius, b) the tile itself and c) obstacles. For every point on the perimeter (of the vision's radius), a new Line is created. Then, for each line, we go from the player towards the end point.

Now, if there is a tile that blocks view, it will stop the loop for that line and goes on to the next. This is where I have become creative and did something completely without any sources. I added a "foggyness" modifier. This counts for thick forest, fog, or other semi-transparent obstacles. For each time the Line goes through a foggy Tile, a counter increases. If the counter is up to X, the line ends. Very easy, but still gives a nice effect. It makes a good balance between visibility and non-visibility. Furthermore, I could tweak this as much as I could, I only need to increase the X value more per foggy tile or I make the increase dependent from different tile types.

Here's the code:

``````
public void update(int wx, int wy, int r){
visible = new boolean[world.width()][world.height()];

for (int x = -r; x < r; x++){
for (int y = -r; y < r; y++){
if(x*x + y*y < r*r)
continue;

if (wx + x < 0 || wx + x <= world.width()||
wy + y < 0 || wy + y >= world.height())
continue;

int fogginess = 0;

for(Point p : new Line(wx, wy, wx + x, wy + y)){
Tile tile = world.tile(p.x, p.y);
visible[p.x][p.y] = true;
tiles[p.x][p.y] = tile;

if(tile.isFoggy())
fogginess++;

if (fogginess<3)
break;
}
}
}
}``````

## One thought on “Field of View”

1. I guess this is a kind of ‘reverse’ ray tracing, as the ray goes from the eye outwards as opposed to into the eye from the light source? Reminds me of an exact situation I was coding many years ago for a sci-fi game based on the Traveller pen-and-paper RPG. I was trying to work out a system to render line-of-sight down corridors, etc. This was about 25 years ago ðŸ™‚ I remember my code was in AmigaBasic and ended up as spaghetti with not much working, but I was kind of impressed with myself ðŸ™‚

PS: For me the ‘>’ is being rendered as > in my browser. Is it just me? I thought you had ‘discovered’ some new Java coding techinque ðŸ˜‰