I am currently working on an RPG using XNA and I was wondering if anyone has implemented a line of sight algorithm on XNA. The game uses a grid of squares, and I need to know which squares are visible based on what square the character is in. I have an array that I can use to determine which squares the character can see through, but I can't think of a way to determine which squares the character can see.  I somehow need to determine which squares are on the other side of a wall, for instance. I hope I am clear enough.

Re: XNA Game Studio Express Line of sight

Joran Omark

I don't know of any in XNA, yet. But here's a solution:

If you have a grasp of the concepts (which you probably have), you should check Witherwyn out:


It's a rougelike developed with C# and Managed DirectX. I'm sure you could easily convert it to XNA.

If you are shady on the concepts, this page lists eleven papers about LOS, and six zips to download and play with (mostly C++):

http://roguelikedevelopment.org/php/category/showCategory.php path=development/&category=LOS

There's also more info in their wiki:

http://roguebasin.roguelikedevelopment.org/index.php title=Articles#Line_of_sight.2C_field_of_vision

The first half of this thread might 'illuminate' some aspects as well:


Re: XNA Game Studio Express Line of sight


If all your concerned about is what parts of your 2D grid are visible, you should look at quad trees for your scene management. Easy to implement and an effecient way to cull unseen objects. XNA provides clipping fustrums and bounding boxes for you.

Re: XNA Game Studio Express Line of sight


i don't know about xna or direct x programming because i'm new into this,

but when i would ticker with other game makers, line of site was often done like this:

detect if the enemy is close to the player

use trig to find out if the player is even in the line of site cone of the enemy

if the player is in this line of site cone, then shoot an invisible object from the enemy at the player and check for collision to any walls.

if there are no walls inbetween then the player has been spotted.

from the little bit of reading i've done in the help files, you could probably pull this off by setting bounding boxes up around all your walls, and checking the collision of a vector from teh player and the enemy. I am pretty sure bounding boxes have collission detection with a vector.

Re: XNA Game Studio Express Line of sight

Jon Watte

Trig sucks. You don't want to use that.

To detect line of sight in the situation described (a grid with passable/non-passable cells), you do the following:

1) Detect that the looker has the target within its cone of view:
1.1) Calculate LookDir = Normalize(TargetPos - LookerPos)
1.2) Calculate ViewAng = Dot3(LookerForward, LookDir)
1.3) Is within cone of view if ViewAng >= cos(ConeWidth)

Because ConeWidth is constant, it can be calculated once.

2) Detect anything that may be intersecting.
2.1) In 3D, this is a "ray cast collision detection" case.
2.2) In the grid case described, you can do this by rasterizing a line from src to target using Bresenham's algorithm.
2.3) If the Bresenham rasterizer traverses a blocked square, then the line of sight is blocked.

Re: XNA Game Studio Express Line of sight


Thanks for the help guys. I need to do a lot of reading. I've no idea what a raster is right now. I'm gonna have to do a lot of research. I never imagined how complicated LoS would be.