Skip to main content

PathFollower

Struct PathFollower 

Source
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

Source

pub fn new(arrive_radius: f32) -> Self

Creates a finished follower. Panics unless arrive_radius is finite and positive.

Source

pub fn set_cells(&mut self, layout: &GridLayout, cells: &[UVec2])

Follows the centers of cells, reusing waypoint capacity.

Source

pub fn set_waypoints(&mut self, points: impl IntoIterator<Item = Vec2>)

Follows world-space points, reusing waypoint capacity. Panics for a nonfinite point.

Source

pub fn clear(&mut self)

Stops following and forgets the waypoints.

Source

pub fn is_finished(&self) -> bool

Whether every waypoint has been reached (or none were given).

Source

pub fn waypoints(&self) -> &[Vec2]

All waypoints of the current path.

Source

pub fn remaining(&self) -> &[Vec2]

Waypoints not reached yet, starting with the current target.

Source

pub fn next_waypoint(&self) -> Option<Vec2>

Current target waypoint.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for PathFollower

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PathFollower

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PathFollower

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for PathFollower

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Component for T
where T: Send + Sync + 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.