bevy_transform_interpolation/extrapolation.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
//! [`Transform`] extrapolation, making movement in [`FixedUpdate`] appear smooth
//! by easing between the current and predicted [`Transform`] in between fixed timesteps.
//!
//! See the [`TransformExtrapolationPlugin`] for more information.
use std::marker::PhantomData;
use crate::{
NoRotationEasing, NoTranslationEasing, RotationEasingState, TransformEasingPlugin,
TransformEasingSet, TranslationEasingState, VelocitySource, VelocitySourceItem,
};
use bevy::prelude::*;
/// A plugin for [`Transform`] extrapolation, making movement in [`FixedUpdate`] appear smooth.
///
/// Transform extrapolation predicts future positions based on velocity, and applies easing
/// between the current and predicted [`Transform`] in between fixed timesteps.
/// This results in movement that looks smooth and feels responsive, but can stutter
/// when the prediction is incorrect, such as when velocity changes abruptly.
///
/// This plugin requires the [`TransformEasingPlugin`] to function. It is automatically added
/// if not already present in the app.
///
/// Note that unlike [`TransformInterpolationPlugin`], this plugin does *not* support scale easing.
/// However, the [`ScaleInterpolation`] component can still be used even when translation and rotation are extrapolated.
///
/// [`TransformInterpolationPlugin`]: crate::interpolation::TransformInterpolationPlugin
/// [`ScaleInterpolation`]: crate::interpolation::ScaleInterpolation
///
/// # Usage
///
/// Transform extrapolation requires velocity to predict future positions.
/// Instead of providing its own velocity components, the [`TransformExtrapolationPlugin`]
/// lets you specify your own velocity components that you manage yourself.
///
/// First, make sure you have components for velocity, and implement the [`VelocitySource`] trait on a [`QueryData`] type:
///
/// ```
/// use bevy::{ecs::query::QueryData, prelude::*};
/// use bevy_transform_interpolation::VelocitySource;
///
/// #[derive(Component, Default)]
/// struct LinearVelocity(Vec3);
///
/// #[derive(Component, Default)]
/// struct AngularVelocity(Vec3);
///
/// #[derive(QueryData)]
/// struct LinVelSource;
///
/// impl VelocitySource for LinVelSource {
/// // Components storing the previous and current velocities.
/// // Note: For extrapolation, the `Previous` component is not used, so we can make it the same as `Current`.
/// type Previous = LinearVelocity;
/// type Current = LinearVelocity;
///
/// fn previous(start: &Self::Previous) -> Vec3 {
/// start.0
/// }
///
/// fn current(end: &Self::Current) -> Vec3 {
/// end.0
/// }
/// }
///
/// #[derive(QueryData)]
/// struct AngVelSource;
///
/// impl VelocitySource for AngVelSource {
/// type Previous = AngularVelocity;
/// type Current = AngularVelocity;
///
/// fn previous(start: &Self::Previous) -> Vec3 {
/// start.0
/// }
///
/// fn current(end: &Self::Current) -> Vec3 {
/// end.0
/// }
/// }
/// ```
///
/// Then, add the [`TransformExtrapolationPlugin`] to the app with the velocity sources:
///
/// ```
/// use bevy::{ecs::query::QueryData, prelude::*};
/// use bevy_transform_interpolation::{prelude::*, VelocitySource};
/// #
/// # #[derive(Component, Default)]
/// # struct LinearVelocity(Vec3);
/// #
/// # #[derive(Component, Default)]
/// # struct AngularVelocity(Vec3);
/// #
/// # #[derive(QueryData)]
/// # struct LinVelSource;
/// #
/// # impl VelocitySource for LinVelSource {
/// # type Previous = LinearVelocity;
/// # type Current = LinearVelocity;
/// #
/// # fn previous(start: &Self::Previous) -> Vec3 {
/// # start.0
/// # }
/// #
/// # fn current(end: &Self::Current) -> Vec3 {
/// # end.0
/// # }
/// # }
/// #
/// # #[derive(QueryData)]
/// # struct AngVelSource;
/// #
/// # impl VelocitySource for AngVelSource {
/// # type Previous = AngularVelocity;
/// # type Current = AngularVelocity;
/// #
/// # fn previous(start: &Self::Previous) -> Vec3 {
/// # start.0
/// # }
/// #
/// # fn current(end: &Self::Current) -> Vec3 {
/// # end.0
/// # }
/// # }
///
/// fn main() {
/// let mut app = App::new();
///
/// app.add_plugins((
/// TransformInterpolationPlugin::default(),
/// TransformExtrapolationPlugin::<LinVelSource, AngVelSource>::default(),
/// ));
///
/// // Optional: Insert velocity components automatically for entities with extrapolation.
/// app.register_required_components::<TranslationExtrapolation, LinearVelocity>();
/// app.register_required_components::<RotationExtrapolation, AngularVelocity>();
///
/// // ...
///
/// app.run();
/// }
/// ```
///
/// Transform extrapolation can now be enabled for a given entity by adding the [`TransformExtrapolation`] component:
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_transform_interpolation::prelude::*;
/// #
/// fn setup(mut commands: Commands) {
/// // Extrapolate translation and rotation.
/// commands.spawn((
/// Transform::default(),
/// TransformExtrapolation,
/// ));
/// }
/// ```
///
/// Now, any changes made to the translation or rotation of the entity in [`FixedPreUpdate`], [`FixedUpdate`],
/// or [`FixedPostUpdate`] will automatically be smoothed in between fixed timesteps.
///
/// Transform properties can also be extrapolated individually by adding the [`TranslationExtrapolation`]
/// and [`RotationExtrapolation`] components.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_transform_interpolation::prelude::*;
/// #
/// fn setup(mut commands: Commands) {
/// // Only extrapolate translation.
/// commands.spawn((Transform::default(), TranslationExtrapolation));
///
/// // Only extrapolate rotation.
/// commands.spawn((Transform::default(), RotationExtrapolation));
/// }
/// ```
///
/// If you want *all* entities with a [`Transform`] to be extrapolated by default, you can use
/// [`TransformExtrapolationPlugin::extrapolate_all()`], or set the [`extrapolate_translation_all`]
/// and [`extrapolate_rotation_all`] fields.
///
/// ```ignore
/// # use bevy::prelude::*;
/// # use bevy_transform_interpolation::prelude::*;
/// #
/// fn main() {
/// App::new()
/// .add_plugins(TransformExtrapolationPlugin::<LinVelSource, AngVelSource> {
/// // Extrapolate translation by default, but not rotation.
/// extrapolate_translation_all: true,
/// extrapolate_rotation_all: false,
/// })
/// // ...
/// .run();
/// }
/// ```
///
/// When extrapolation is enabled for all entities by default, you can still opt out of it for individual entities
/// by adding the [`NoTransformEasing`] component, or the individual [`NoTranslationEasing`] and [`NoRotationEasing`] components.
///
/// Note that changing [`Transform`] manually in any schedule that *doesn't* use a fixed timestep is also supported,
/// but it is equivalent to teleporting, and disables extrapolation for the entity for the remainder of that fixed timestep.
///
/// [`QueryData`]: bevy::ecs::query::QueryData
/// [`TransformExtrapolationPlugin::extrapolate_all()`]: TransformExtrapolationPlugin::extrapolate_all
/// [`extrapolate_translation_all`]: TransformExtrapolationPlugin::extrapolate_translation_all
/// [`extrapolate_rotation_all`]: TransformExtrapolationPlugin::extrapolate_rotation_all
/// [`NoTransformEasing`]: crate::NoTransformEasing
/// [`NoTranslationEasing`]: crate::NoTranslationEasing
/// [`NoRotationEasing`]: crate::NoRotationEasing
///
/// # Alternatives
///
/// For many applications, the stutter caused by mispredictions in extrapolation may be undesirable.
/// In these cases, the [`TransformInterpolationPlugin`] can be a better alternative.
///
/// Transform interpolation eases between the previous and current [`Transform`],
/// resulting in movement that is always smooth and accurate. The downside is that the rendered
/// positions can lag slightly behind the true positions, making movement feel delayed.
///
/// # Easing Backends
///
/// By default, transform extrapolation uses linear interpolation (`lerp`) for easing translation,
/// and spherical linear interpolation (`slerp`) for easing rotation.
///
/// If the previous and current velocities are also available, it is possible to use *Hermite interpolation*
/// with the [`TransformHermiteEasingPlugin`] to get smoother and more accurate easing. To enable Hermite interpolation
/// for extrapolation, add the [`TransformHermiteEasing`] component to the entity in addition to the extrapolation components.
///
/// [`TransformHermiteEasingPlugin`]: crate::hermite::TransformHermiteEasingPlugin
/// [`TransformHermiteEasing`]: crate::hermite::TransformHermiteEasing
#[derive(Debug)]
pub struct TransformExtrapolationPlugin<LinVel: VelocitySource, AngVel: VelocitySource> {
/// If `true`, translation will be extrapolated for all entities with the [`Transform`] component by default.
///
/// This can be overridden for individual entities by adding the [`NoTranslationEasing`] or [`NoTransformEasing`] component.
///
/// [`NoTransformEasing`]: crate::NoTransformEasing
pub extrapolate_translation_all: bool,
/// If `true`, rotation will be extrapolated for all entities with the [`Transform`] component by default.
///
/// This can be overridden for individual entities by adding the [`NoRotationEasing`] or [`NoTransformEasing`] component.
///
/// [`NoTransformEasing`]: crate::NoTransformEasing
pub extrapolate_rotation_all: bool,
/// Phantom data use the type parameters.
#[doc(hidden)]
pub _phantom: PhantomData<(LinVel, AngVel)>,
}
impl<LinVel: VelocitySource, AngVel: VelocitySource> Default
for TransformExtrapolationPlugin<LinVel, AngVel>
{
fn default() -> Self {
Self {
extrapolate_translation_all: false,
extrapolate_rotation_all: false,
_phantom: PhantomData,
}
}
}
impl<LinVel: VelocitySource, AngVel: VelocitySource> TransformExtrapolationPlugin<LinVel, AngVel> {
/// Enables extrapolation for translation and rotation for all entities with the [`Transform`] component.
///
/// This can be overridden for individual entities by adding the [`NoTransformEasing`] component,
/// or the individual [`NoTranslationEasing`] and [`NoRotationEasing`] components.
///
/// [`NoTransformEasing`]: crate::NoTransformEasing
/// [`NoRotationEasing`]: crate::NoRotationEasing
pub fn extrapolate_all() -> Self {
Self {
extrapolate_translation_all: true,
extrapolate_rotation_all: true,
_phantom: PhantomData,
}
}
}
impl<LinVel: VelocitySource, AngVel: VelocitySource> Plugin
for TransformExtrapolationPlugin<LinVel, AngVel>
{
fn build(&self, app: &mut App) {
//Register components.
app.register_type::<(
TransformExtrapolation,
TranslationExtrapolation,
RotationExtrapolation,
)>();
// Reset the transform to the start of the extrapolation at the beginning of the fixed timestep
// to match the true position from the end of the previous fixed tick.
app.add_systems(
FixedFirst,
(
reset_translation_extrapolation,
reset_rotation_extrapolation,
)
.before(TransformEasingSet::Reset),
);
// Update the start and end state of the extrapolation at the end of the fixed timestep.
app.add_systems(
FixedLast,
(
update_translation_extrapolation_states::<LinVel>,
update_rotation_extrapolation_states::<AngVel>,
)
.in_set(TransformEasingSet::UpdateEnd),
);
// Insert extrapolation components automatically for all entities with a `Transform`
// if the corresponding global extrapolation is enabled.
if self.extrapolate_translation_all {
let _ = app.try_register_required_components::<Transform, TranslationExtrapolation>();
}
if self.extrapolate_rotation_all {
let _ = app.try_register_required_components::<Transform, RotationExtrapolation>();
}
}
fn finish(&self, app: &mut App) {
// Add the `TransformEasingPlugin` if it hasn't been added yet.
// It performs the actual easing based on the start and end states set by the extrapolation.
if !app.is_plugin_added::<TransformEasingPlugin>() {
app.add_plugins(TransformEasingPlugin);
}
}
}
/// Enables [`Transform`] extrapolation for an entity, making changes to translation
/// and rotation in [`FixedUpdate`] appear smooth.
///
/// Extrapolation only works for entities with velocity components.
/// [`TransformExtrapolationPlugin`] must be added to the app with the appropriate velocity sources.
///
/// Unlike [`TransformInterpolation`], this does *not* support scale easing.
/// However, the [`ScaleInterpolation`] component can still be used even when translation and rotation are extrapolated.
///
/// See the [`TransformExtrapolationPlugin`] for more information.
///
/// [`TransformInterpolation`]: crate::interpolation::TransformInterpolation
/// [`ScaleInterpolation`]: crate::interpolation::ScaleInterpolation
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Eq, Reflect)]
#[reflect(Component, Debug, Default)]
#[require(TranslationExtrapolation, RotationExtrapolation)]
pub struct TransformExtrapolation;
/// Enables translation extrapolation for an entity, making changes to translation
/// in [`FixedUpdate`] appear smooth.
///
/// Extrapolation only works for entities with velocity components.
/// [`TransformExtrapolationPlugin`] must be added to the app with the appropriate velocity sources.
///
/// See the [`TransformExtrapolationPlugin`] for more information.
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Eq, Reflect)]
#[reflect(Component, Debug, Default)]
#[require(TranslationEasingState)]
pub struct TranslationExtrapolation;
/// Enables rotation extrapolation for an entity, making changes to rotation
/// in [`FixedUpdate`] appear smooth.
///
/// Extrapolation only works for entities with velocity components.
/// [`TransformExtrapolationPlugin`] must be added to the app with the appropriate velocity sources.
///
/// See the [`TransformExtrapolationPlugin`] for more information.
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Eq, Reflect)]
#[reflect(Component, Debug, Default)]
#[require(RotationEasingState)]
pub struct RotationExtrapolation;
/// Resets the translation to the start of the extrapolation at the beginning of the fixed timestep
/// to match the true position from the end of the previous fixed tick.
fn reset_translation_extrapolation(
mut query: Query<
(&mut Transform, &TranslationEasingState),
(With<TranslationExtrapolation>, Without<NoTranslationEasing>),
>,
) {
for (mut transform, translation_easing) in &mut query {
if let Some(start) = translation_easing.start {
transform.translation = start;
}
}
}
/// Resets the rotation to the start of the extrapolation at the beginning of the fixed timestep
/// to match the true position from the end of the previous fixed tick.
fn reset_rotation_extrapolation(
mut query: Query<
(&mut Transform, &RotationEasingState),
(With<RotationExtrapolation>, Without<NoRotationEasing>),
>,
) {
for (mut transform, rotation_easing) in &mut query {
if let Some(start) = rotation_easing.start {
transform.rotation = start;
}
}
}
/// Updates the start and end states of the extrapolation for the next fixed timestep.
fn update_translation_extrapolation_states<V: VelocitySource>(
mut query: Query<
(&Transform, &mut TranslationEasingState, &V::Current),
(With<TranslationExtrapolation>, Without<NoTranslationEasing>),
>,
time: Res<Time>,
) {
let delta_secs = time.delta_secs();
for (transform, mut translation_easing, end_vel) in &mut query {
translation_easing.start = Some(transform.translation);
// Extrapolate the next state based on the current state and velocities.
let lin_vel = <V::Item<'static> as VelocitySourceItem<V>>::current(end_vel);
translation_easing.end = Some(transform.translation + lin_vel * delta_secs);
}
}
/// Updates the start and end states of the extrapolation for the next fixed timestep.
fn update_rotation_extrapolation_states<V: VelocitySource>(
mut query: Query<
(&Transform, &mut RotationEasingState, &V::Current),
(With<RotationExtrapolation>, Without<NoRotationEasing>),
>,
time: Res<Time>,
) {
let delta_secs = time.delta_secs();
for (transform, mut rotation_easing, end_vel) in &mut query {
rotation_easing.start = Some(transform.rotation);
// Extrapolate the next state based on the current state and velocities.
let ang_vel = <V::Item<'static> as VelocitySourceItem<V>>::current(end_vel);
let scaled_axis = ang_vel * delta_secs;
rotation_easing.end = Some(transform.rotation * Quat::from_scaled_axis(scaled_axis));
}
}