RLJLogo-small.jpg  FOV and LOS code examples
Main
Fov/Los
Tutorial
Results
Download

Demos
    Fov/Los Demo
    CharDesigner Demo


Sourceforge Project Page

Fov calculation


First, the data structure that you are using to represent the map, must implement ILosBoard.

public interface ILosBoard
{

      public boolean contains(int x, int y);

      public boolean isObstacle(int x, int y);

      public void visit(int x, int y);
}

Then create the Fov object and ask for Fov calculation.

IFovAlgorithm a=new ShadowCasting();
a.visitFieldOfView(board, originX, originY, radius);

board.isObstacle() will be called to decide if the location can be seen through. board.visit() will be called on each visible board location. For the algorithms implemented here, it is guaranteed that

  1. board.visit() will be called on each board location only once.
  2. board.visit() call will precede any calls to board.isObstacle(). This helps in situations where you are using Fov to simulate the area of effect of a fireball and can decide, when the explosion hits an iron pillar, whether the pillar stops the explosion or whether the iron pillar melts thus transmitting the explosion to further areas.
board.isObstacle() may be called multiple times on each location. (It usually is not)

Available are the following algorithms :
ShadowCasting: 
The algorithm checks in a circle in increasing radius for obstaces. If an obstacle is found, further scans avoid the angular span corresponding to the obstacle. I got the code from the RL-NG project.
IFovAlgorithm a=new ShadowCasting();
Precise Permissive:
The algorithm is the one proposed by Jonathon Duerig, see here for details.
IFovAlgorithm a=new PrecisePermissive();

Conic Fov calculation

For breath weapon calculation, directional field of vision, etc. Mostly same as above, just call
a.visitConeFieldOfView(b, 10, 10, 9, startAngle, finishAngle);
Algorithms:
ShadowCasting: 
IConeFovAlgorithm a=new ShadowCasting();
PrecisePermissive: 
IConeFovAlgorithm a=new ConePrecisePremisive();

Los Calculation and Projection

The same ILosBoard data structure is used in Los calculations too. However, board.visit() will never be called. board.isObstacle() will be called on probable locations that may be on the path from point A to B.

Code:

ILosAlgorithm a=new ShadowCasting();
boolean los=a.existsLineOfSight(b, 10, 10, x1, y1, true);

The signature of the call is 

public abstract boolean existsLineOfSight(ILosBoard b, int startX,
                  int startY, int endX, int endY, 
                  boolean calculateProject);

calculateProject, if true, will also (try to) calculate a path to the target location. That path can be retrieved by calling 

List<Point2I> path = a.getProjectPath();

immediately after calling the existsLineOfSight().

Available are the following algorithms:

Bresenham:
Whether LOS exists along a simple bresenham line.
ILosAlgorithm a=new BresLos();
Symmetric Bresenham:
Whether LOS exists along the two possible bresenham lines. LOS must exist entirely along one of the lines. 
ILosAlgorithm a=new BresLos(); 
a.SYMMETRIC_ENABLED=true;
Opportunistic Bresenham:
Whether some LOS path exists along both Bresenham lines.
ILosAlgorithm a=new BresOpportunisticLos();
ShadowCasting:
Whether shadowcating algorithm allows the target point to be seen from the start location.
ILosAlgorithm a=new ShadowCasting();
Precise Permissive:
Whether shadowcating algorithm allows the target point to be seen from the start location.
ILosAlgorithm a=new PrecisePermissive();

For further examples, browse the src/rlforj/examples directory.