pub struct FlowField {
pub width: usize,
pub height: usize,
pub cell_size: f32,
pub costs: Vec<f32>,
pub integration: Vec<f32>,
pub directions: Vec<Vec2>,
pub origin: Vec2,
}Expand description
A grid-based flow field.
Build order:
set_costfor any impassable/expensive cells.build_integration_field— BFS/Dijkstra from goals.build_direction_field— gradient from integration field.get_direction— per-agent query.
Fields§
§width: usize§height: usize§cell_size: f32§costs: Vec<f32>Per-cell traversal cost (>= 1.0; INFINITY = impassable).
integration: Vec<f32>Integration (distance) field — lowest value near goals.
directions: Vec<Vec2>Direction field — normalised Vec2 pointing toward nearest goal.
origin: Vec2Implementations§
Source§impl FlowField
impl FlowField
Sourcepub fn new(width: usize, height: usize, cell_size: f32) -> Self
pub fn new(width: usize, height: usize, cell_size: f32) -> Self
Create a new uniform-cost flow field.
pub fn with_origin(self, origin: Vec2) -> Self
Sourcepub fn set_cost(&mut self, x: usize, y: usize, cost: f32)
pub fn set_cost(&mut self, x: usize, y: usize, cost: f32)
Set the traversal cost of a cell. Use f32::INFINITY for impassable.
Sourcepub fn reset_costs(&mut self)
pub fn reset_costs(&mut self)
Reset all costs to 1.0.
Sourcepub fn world_to_grid(&self, pos: Vec2) -> (usize, usize)
pub fn world_to_grid(&self, pos: Vec2) -> (usize, usize)
World position → grid cell.
Sourcepub fn grid_to_world(&self, x: usize, y: usize) -> Vec2
pub fn grid_to_world(&self, x: usize, y: usize) -> Vec2
Grid cell → world-space centre.
Sourcepub fn build_integration_field(&mut self, goals: &[(usize, usize)])
pub fn build_integration_field(&mut self, goals: &[(usize, usize)])
Build the integration (distance-weighted) field from a set of goal cells using a priority-queue Dijkstra.
Sourcepub fn build_direction_field(&mut self)
pub fn build_direction_field(&mut self)
Build the direction field by gradient-descent from the integration field.
Sourcepub fn get_direction(&self, world_pos: Vec2) -> Vec2
pub fn get_direction(&self, world_pos: Vec2) -> Vec2
Sample the flow field at a world position using bilinear interpolation.
Sourcepub fn get_integration(&self, world_pos: Vec2) -> f32
pub fn get_integration(&self, world_pos: Vec2) -> f32
Get the integration value at a world position.
Sourcepub fn is_passable(&self, x: usize, y: usize) -> bool
pub fn is_passable(&self, x: usize, y: usize) -> bool
Check if a cell is passable.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for FlowField
impl RefUnwindSafe for FlowField
impl Send for FlowField
impl Sync for FlowField
impl Unpin for FlowField
impl UnsafeUnpin for FlowField
impl UnwindSafe for FlowField
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.