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
impl Follow2
pub fn new(settings: Follow2Settings) -> Result<Follow2, CameraError>
pub fn settings(&self) -> Follow2Settings
pub fn set_settings( &mut self, settings: Follow2Settings, ) -> Result<(), CameraError>
Sourcepub fn look_ahead_dead_zone(&self) -> Vec2
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.
Sourcepub fn set_look_ahead_dead_zone(
&mut self,
dead_zone: Vec2,
) -> Result<(), CameraError>
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);Sourcepub fn center(&self) -> Option<Vec2>
pub fn center(&self) -> Option<Vec2>
Last successful center, or None before initialization or after Self::reset.
Sourcepub fn reset(&mut self)
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.);Sourcepub fn cut(&mut self)
pub fn cut(&mut self)
Request a cut on the next valid update, retaining the current center until then.
Sourcepub fn snap(
&mut self,
subject: Vec2,
velocity: Vec2,
) -> Result<Frame2, CameraError>
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.);Sourcepub fn update(
&mut self,
dt: f32,
subject: Vec2,
velocity: Vec2,
) -> Result<Frame2, CameraError>
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.