use bevy_math::Vec3;
use bevy_transform::prelude::Transform;
use crate::dolly::{
driver::RigDriver,
rig::RigUpdateParams,
util::{
ExpSmoothed,
ExpSmoothingParams,
},
};
#[derive(Debug)]
pub struct LookAt {
pub smoothness: f32,
pub target: Vec3,
output_offset_scale: f32,
smoothed_target: ExpSmoothed<Vec3>,
}
impl LookAt {
pub fn new(target: Vec3) -> Self {
Self {
smoothness: 0.0,
output_offset_scale: 1.0,
target,
smoothed_target: Default::default(),
}
}
pub fn tracking_smoothness(mut self, smoothness: f32) -> Self {
self.smoothness = smoothness;
self
}
pub fn tracking_predictive(mut self, predictive: bool) -> Self {
self.output_offset_scale = if predictive { -1.0 } else { 1.0 };
self
}
}
impl RigDriver for LookAt {
fn update(&mut self, params: RigUpdateParams) -> Transform {
let target = self.smoothed_target.exp_smooth_towards(
&self.target,
ExpSmoothingParams {
smoothness: self.smoothness,
output_offset_scale: self.output_offset_scale,
delta_time_seconds: params.delta_time_seconds,
},
);
let rotation = params.parent.looking_at(target, Vec3::Y).rotation;
Transform {
translation: params.parent.translation,
rotation,
..Default::default()
}
}
}