Function torchbearer::fov::field_of_view
source · Expand description
An implementation of the field of view algorithm using basic raycasting. Returns a vector containing all points visible from the starting position, including the starting position.
Adapted the algorithm found on the visibility determination. For a comparison of the different raycasting types, advantages and disavantages, see roguebasin’s comparison
It differs from the original algorithm in the way it decides where to cast rays: It uses a circle around the center instead of a square, which avoids the necessity of error correction.
The result is more homogeneous and faster.
Arguments
map
- A struct implementing theVisionMap
trait.from
- The origin/center of the field of vision.radius
- How far the vision should go. Should be higher or equal to 0 (If 0 or less, you only see yourself).
Panics
Panics if from
is out of the map bounds.
Examples
use torchbearer::{
fov::{field_of_view, VisionMap},
Point,
};
struct SampleMap {
width: i32,
height: i32,
transparent: Vec<bool>,
}
impl SampleMap {
fn new(width: i32, height: i32) -> Self {
// (…)
}
}
impl VisionMap for SampleMap {
fn dimensions(&self) -> (i32, i32) {
(self.width, self.height)
}
fn is_transparent(&self, (x, y): Point) -> bool {
self.transparent[(x + y * self.width) as usize]
}
}
let sample_map = SampleMap::new(16, 10);
// (…) You probably want at this point to add some walls to your map.
let from = (1, 1);
let radius = 5;
let visible_positions = field_of_view(&sample_map, from, radius);
for visible_position in visible_positions {
// (…)
}