Crate bevy_transform_interpolation

source
Expand description

§bevy_transform_interpolation

A drop-in Transform interpolation solution for fixed timesteps for the Bevy game engine.

§Features

§Getting Started

First, add bevy_transform_interpolation as a dependency in your Cargo.toml:

[dependencies]
bevy_transform_interpolation = "0.1"

To enable Transform interpolation, add the TransformInterpolationPlugin to your app:

use bevy::prelude::*;
use bevy_transform_interpolation::prelude::*;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, TransformInterpolationPlugin::default()))
        // ...other plugins, resources, and systems
        .run();
}

By default, interpolation is only performed for entities with the TransformInterpolation component:

fn setup(mut commands: Commands) {
    // Interpolate the entire transform: translation, rotation, and scale.
    commands.spawn((
        Transform::default(),
        TransformInterpolation,
    ));
}

Now, any changes made to the Transform of the entity in FixedPreUpdate, FixedUpdate, or FixedPostUpdate will automatically be interpolated in between fixed timesteps.

If you want all entities with a Transform to be interpolated by default, you can use TransformInterpolationPlugin::interpolate_all():

fn main() {
   App::new()
      .add_plugins(TransformInterpolationPlugin::interpolate_all())
      // ...
      .run();
}

See the documentation of the TransformInterpolationPlugin for a more detailed overview of what it can do.

§Advanced Usage

For a lot of applications, the functionality shown in the Getting Started guide might be all you need! However, bevy_transform_interpolation has a lot more to offer:

§How Does It Work?

Internally, bevy_transform_interpolation simply maintains components that store the start and end of the interpolation. For example, translation uses the following component for easing the movement:

pub struct TranslationEasingState {
    pub start: Option<Vec3>,
    pub end: Option<Vec3>,
}

The states are updated by the TransformInterpolationPlugin or TransformExtrapolationPlugin depending on whether the entity has TransformInterpolation or TransformExtrapolation components.

If interpolation is used:

If extrapolation is used:

At the start of the FixedFirst schedule, the states are reset to None. If the Transform is detected to have changed since the last easing run but outside of the fixed timestep schedules, the easing is also reset to None to prevent overwriting the change.

The actual easing is performed in RunFixedMainLoop, right after FixedMain, before Update. By default, linear interpolation (lerp) is used for translation and scale, and spherical linear interpolation (slerp) is used for rotation.

However, thanks to the modular and flexible architecture, other easing methods can also be used. The TransformHermiteEasingPlugin provides an easing backend using Hermite interpolation, overwriting the linear interpolation for specific entities with the NonlinearTranslationEasing and NonlinearRotationEasing marker components. Custom easing solutions can be implemented using the same pattern.

Modules§

Structs§

  • A resource that stores the last tick when easing was performed.
  • Explicitly marks this entity as having no rotation easing, disabling interpolation and/or extrapolation.
  • Explicitly marks this entity as having no scale easing, disabling interpolation and/or extrapolation.
  • Explicitly marks this entity as having no transform easing, disabling interpolation and/or extrapolation.
  • Explicitly marks this entity as having no translation easing, disabling interpolation and/or extrapolation.
  • A marker component that indicates that the entity has non-linear rotation easing, and linear easing should not be applied.
  • A marker component that indicates that the entity has non-linear translation easing, and linear easing should not be applied.
  • Stores the start and end states used for interpolating the rotation of an entity. The change in rotation is smoothed from start to end in between FixedUpdate runs.
  • Stores the start and end states used for interpolating the scale of an entity. The change in scale is smoothed from start to end in between FixedUpdate runs.
  • A plugin for applying easing to Transform changes, making movement in FixedUpdate appear smooth.
  • Stores the start and end states used for interpolating the translation of an entity. The change in translation is smoothed from start to end in between FixedUpdate runs.

Enums§

Traits§

Functions§