Struct Follow2

Source
pub struct Follow2 { /* private fields */ }
Expand description

A planar follow camera. The first valid update initializes directly at its goal; subsequent updates apply the configured dead zone and half-life.

Implementations§

Source§

impl Follow2

Source

pub fn new(settings: Follow2Settings) -> Result<Follow2, CameraError>

Source

pub fn settings(&self) -> Follow2Settings

Source

pub fn set_settings( &mut self, settings: Follow2Settings, ) -> Result<(), CameraError>

Source

pub fn look_ahead_dead_zone(&self) -> Vec2

Per-axis velocity dead zone for prediction, in world units per second. Defaults to zero; see Self::set_look_ahead_dead_zone for tuning examples.

Source

pub fn set_look_ahead_dead_zone( &mut self, dead_zone: Vec2, ) -> Result<(), CameraError>

Suppress low-speed look-ahead without changing positional follow tuning.

Each component must be finite and in 0..=1e6, otherwise this returns CameraError::InvalidSettings without changing any camera state. Below or at the threshold the corresponding velocity component contributes no prediction. Above it, only the excess speed contributes, avoiding a jump at the threshold. The resulting prediction still uses the configured look_ahead_time and radial max_look_ahead limit. Zero restores the original velocity prediction on that axis.

Changing this setting retains the center and any pending cut. It takes effect on the next update or snap, and is retained by reset and Self::set_settings. Prediction has no velocity history, so a stop or reversal immediately changes the goal; positional damping and the positional dead zone still apply and may retain some previous lead.

use ferravex_math::Vec2;
use ferravex_play::camera::{Follow2, Follow2Settings};
let mut camera = Follow2::new(Follow2Settings {
    look_ahead_time: 0.5,
    max_look_ahead: 3.,
    half_life: 0.25,
    ..Default::default()
})?;
camera.set_look_ahead_dead_zone(Vec2::new(2., 0.))?;
assert_eq!(camera.look_ahead_dead_zone(), Vec2::new(2., 0.));
assert_eq!(camera.snap(Vec2::ZERO, Vec2::X * 8.)?.center, Vec2::X * 3.);
// Small residual velocity does not keep the prediction moving forward.
let stopped = camera.update(0.25, Vec2::ZERO, Vec2::X)?;
assert_eq!(stopped.center, Vec2::X * 1.5);
assert!(!stopped.cut);
Source

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

Last successful center, or None before initialization or after Self::reset.

Source

pub fn reset(&mut self)

Forget the current center and tracking history, retaining authored settings and the velocity look-ahead dead zone. The next valid update initializes at its goal, even with zero elapsed time.

use ferravex_math::Vec2;
use ferravex_play::camera::Follow2;
let mut camera = Follow2::default();
camera.snap(Vec2::X * 30., Vec2::ZERO)?;
camera.reset(); // Restart before the new player spawn is available.
assert_eq!(camera.center(), None);
let frame = camera.update(0., Vec2::X * 5., Vec2::ZERO)?;
assert!(frame.cut);
assert_eq!(frame.center, Vec2::X * 5.);
Source

pub fn cut(&mut self)

Request a cut on the next valid update, retaining the current center until then.

Source

pub fn snap( &mut self, subject: Vec2, velocity: Vec2, ) -> Result<Frame2, CameraError>

Immediately place the camera for a spawn, teleport or restart.

Applies offset, velocity look-ahead and limits without positional dead-zone lag or damping. The returned frame is a cut; subsequent updates smooth from this new center. Invalid input leaves all state, including pending cuts, intact.

use ferravex_math::Vec2;
use ferravex_play::camera::{Follow2, Follow2Settings};
let mut camera = Follow2::new(Follow2Settings {
    offset: Vec2::X * 4.,
    ..Default::default()
})?;
let spawn = Vec2::new(100., 20.);
let frame = camera.snap(spawn, Vec2::ZERO)?;
assert!(frame.cut);
assert_eq!(frame.center, spawn + Vec2::X * 4.);
Source

pub fn update( &mut self, dt: f32, subject: Vec2, velocity: Vec2, ) -> Result<Frame2, CameraError>

Follow a sampled subject using elapsed seconds in 0..=0.25.

Negative, non-finite or larger timesteps return CameraError::InvalidInput without changing state. For a long host suspension, explicitly Self::snap to the current subject or bound presentation time before calling this method. Zero time freezes damping; initialization, cuts and limits still apply.

Trait Implementations§

Source§

impl Clone for Follow2

Source§

fn clone(&self) -> Follow2

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Follow2

Source§

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

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

impl Default for Follow2

Source§

fn default() -> Follow2

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Follow2

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Follow2

Source§

impl StructuralPartialEq for Follow2

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.