Evil Science A whole load of stuff

12Dec/070

Line of Sight

The most fundamental thing in a roguelike is line of sight (LOS), that is, the determination of what the player (and monsters) can see in their environment.

The method I am using for LOS is ray casting. This is where a line is drawn between two points using Bresenham's Line Algorithm.

Calculating a sight zone

In a roguelike the area that a player can "see" is commonly within the boundaries of a circle which the player is the centre of. This application, called circle and pictured below, contains an example of this using an implementation of Bresenham's alogrithm. The blue square represents the player, the white circle surrounding it is the area the player can see , the black blocks are barriers that the player cannot see through.

circle.PNG

It works by calculating all the points that lie within a circle of radius r around the player (x,y). If a point is within a circle whose centre point is the player the formula ((x1-x)^2 + (y1-y)^2) < r^2 is true.

Then a Bresenham line (BL) is drawn from the player to the location being examined, one point at a time. If that point is unoccupied it is added to a list and the next point is examined; if it is occupied it not added to the list and BL is abandoned, and so on. Upon completion or abandoning of a line, the points can be seen by the player and are drawn.

Using the application

Compile and run the application using C# express.

The spinbox in the top right hand corner is used to adjust the radius of the player sight.

Pressing the W key moves the player up, S moves the player down, A moves the player left and D moves the player right.

Right click anywhere within the form to add a barrier, and right click it again to remove it.

Observe how the player sight zone changes when you add and remove barriers and move around.

Filed under: Roguelike Leave a comment
Comments (0) Trackbacks (1)

Leave a comment