pub struct PathFollower { /* private fields */ }Expand description
Steers a character through world-space waypoints.
The follower only chooses velocities; collision stays with
Body2D::move_and_slide or Body3D::move_and_slide. A waypoint is
reached within arrive_radius. Each step is shortened so it ends on the
current waypoint instead of jumping past it, keeping the character close
to the path’s segments, which smooth_path checked
for clearance. Collision moves along each axis in turn, so keep steps
(speed * dt) well under a cell. Any waypoint but the last also counts once the character is
beyond it along its segment and within arrive_radius of that segment’s
line: the incoming segment, or the outgoing one for the first waypoint, so
a character on the path ahead of its starting cell’s center does not step
back. A character pushed farther aside returns to the waypoint first.
use rayengine_core::collision::Body2D;
use rayengine_core::glam::{UVec2, Vec2};
use rayengine_core::pathfinding::{GridLayout, PathFollower};
let layout = GridLayout::new(Vec2::ZERO, Vec2::ONE);
let mut follower = PathFollower::new(0.05);
follower.set_cells(&layout, &[UVec2::new(0, 0), UVec2::new(3, 0)]);
let mut body = Body2D::new(Vec2::splat(0.5), Vec2::splat(0.6));
for _ in 0..120 {
follower.steer_body_2d(&mut body, 4.0, 1.0 / 60.0);
body.move_and_slide(1.0 / 60.0, &[]);
}
assert!(follower.is_finished());
assert!((body.position - Vec2::new(3.5, 0.5)).length() < 0.05);Implementations§
Source§impl PathFollower
impl PathFollower
Sourcepub fn new(arrive_radius: f32) -> Self
pub fn new(arrive_radius: f32) -> Self
Creates a finished follower. Panics unless arrive_radius is finite
and positive.
Sourcepub fn set_cells(&mut self, layout: &GridLayout, cells: &[UVec2])
pub fn set_cells(&mut self, layout: &GridLayout, cells: &[UVec2])
Follows the centers of cells, reusing waypoint capacity.
Sourcepub fn set_waypoints(&mut self, points: impl IntoIterator<Item = Vec2>)
pub fn set_waypoints(&mut self, points: impl IntoIterator<Item = Vec2>)
Follows world-space points, reusing waypoint capacity. Panics for a nonfinite point.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Whether every waypoint has been reached (or none were given).
Sourcepub fn remaining(&self) -> &[Vec2]
pub fn remaining(&self) -> &[Vec2]
Waypoints not reached yet, starting with the current target.
Sourcepub fn next_waypoint(&self) -> Option<Vec2>
pub fn next_waypoint(&self) -> Option<Vec2>
Current target waypoint.
Sourcepub fn velocity(&mut self, position: Vec2, speed: f32, dt: f32) -> Vec2
pub fn velocity(&mut self, position: Vec2, speed: f32, dt: f32) -> Vec2
Velocity toward the current waypoint at speed units per second.
Advances past reached waypoints first, returns zero once finished, and
slows down so one step of dt seconds does not overshoot the current
waypoint. Panics for a nonfinite position, a negative or nonfinite
speed, or a negative or nonfinite dt.
Sourcepub fn steer_body_2d(&mut self, body: &mut Body2D, speed: f32, dt: f32)
pub fn steer_body_2d(&mut self, body: &mut Body2D, speed: f32, dt: f32)
Sets a top-down body’s velocity toward the path; see
velocity.
Sourcepub fn steer_body_3d(&mut self, body: &mut Body3D, speed: f32, dt: f32)
pub fn steer_body_3d(&mut self, body: &mut Body3D, speed: f32, dt: f32)
Sets a 3D body’s horizontal velocity, reading waypoints as world
(x, z). Vertical velocity (gravity, jumps) is kept.