pub struct DistanceField { /* private fields */ }Expand description
Cost from every cell to the nearest of one or more goals (a Dijkstra map).
One computation serves any number of agents chasing the same target:
each agent calls next_step for its cell. Distances use
the same costs and corner rules as PathFinder, so a
chain of next steps follows an optimal path. Unlike PathFinder, which
lets an agent leave a blocked start cell, blocked cells have no distance
and no next step. Buffers are reused; computing
again for a grid no larger than before does not allocate, apart from the
queue growing past its previous peak.
use rayengine_core::glam::UVec2;
use rayengine_core::pathfinding::{CostGrid, DistanceField, Neighborhood};
let mut grid = CostGrid::new(UVec2::new(3, 3), 1.0);
grid.block_rect(UVec2::new(1, 0), UVec2::new(1, 1));
let mut field = DistanceField::new();
field.compute(&grid, [UVec2::new(2, 0)], Neighborhood::Four).unwrap();
assert_eq!(field.distance(UVec2::new(0, 0)), Some(6.0));
assert_eq!(field.next_step(&grid, UVec2::new(0, 0)), Some(UVec2::new(0, 1)));
assert_eq!(field.distance(UVec2::new(1, 0)), None);Implementations§
Source§impl DistanceField
impl DistanceField
Sourcepub fn compute<G: NavGrid + ?Sized>(
&mut self,
grid: &G,
goals: impl IntoIterator<Item = UVec2>,
neighborhood: Neighborhood,
) -> Result<(), PathError>
pub fn compute<G: NavGrid + ?Sized>( &mut self, grid: &G, goals: impl IntoIterator<Item = UVec2>, neighborhood: Neighborhood, ) -> Result<(), PathError>
Recomputes distances to the nearest walkable goal.
Blocked goals are ignored; with no walkable goals every cell is unreachable. Costs must be finite and positive. On error the field is left empty, as if computed for a zero-sized grid.
Sourcepub fn distance(&self, cell: UVec2) -> Option<f32>
pub fn distance(&self, cell: UVec2) -> Option<f32>
Cost from cell to the nearest goal, or None when the cell is
blocked, unreachable, or outside the computed grid.
Sourcepub fn next_step<G: NavGrid + ?Sized>(
&self,
grid: &G,
cell: UVec2,
) -> Option<UVec2>
pub fn next_step<G: NavGrid + ?Sized>( &self, grid: &G, cell: UVec2, ) -> Option<UVec2>
The neighbor to move to from cell along an optimal path, or None
at a goal, from an unreachable cell, or for a grid of another size.
Pass the grid the field was computed from. Exact ties prefer orthogonal moves in the order +x, +y, -x, -y.