Skip to content

Commit a569b35

Browse files
authored
Stable interpolation and smooth following (#13741)
# Objective Partially address #13408 Rework of #13613 Unify the very nice forms of interpolation specifically present in `bevy_math` under a shared trait upon which further behavior can be based. The ideas in this PR were prompted by [Lerp smoothing is broken by Freya Holmer](https://www.youtube.com/watch?v=LSNQuFEDOyQ). ## Solution There is a new trait `StableInterpolate` in `bevy_math::common_traits` which enshrines a quite-specific notion of interpolation with a lot of guarantees: ```rust /// A type with a natural interpolation that provides strong subdivision guarantees. /// /// Although the only required method is `interpolate_stable`, many things are expected of it: /// /// 1. The notion of interpolation should follow naturally from the semantics of the type, so /// that inferring the interpolation mode from the type alone is sensible. /// /// 2. The interpolation recovers something equivalent to the starting value at `t = 0.0` /// and likewise with the ending value at `t = 1.0`. /// /// 3. Importantly, the interpolation must be *subdivision-stable*: for any interpolation curve /// between two (unnamed) values and any parameter-value pairs `(t0, p)` and `(t1, q)`, the /// interpolation curve between `p` and `q` must be the *linear* reparametrization of the original /// interpolation curve restricted to the interval `[t0, t1]`. /// /// The last of these conditions is very strong and indicates something like constant speed. It /// is called "subdivision stability" because it guarantees that breaking up the interpolation /// into segments and joining them back together has no effect. /// /// Here is a diagram depicting it: /// ```text /// top curve = u.interpolate_stable(v, t) /// /// t0 => p t1 => q /// |-------------|---------|-------------| /// 0 => u / \ 1 => v /// / \ /// / \ /// / linear \ /// / reparametrization \ /// / t = t0 * (1 - s) + t1 * s \ /// / \ /// |-------------------------------------| /// 0 => p 1 => q /// /// bottom curve = p.interpolate_stable(q, s) /// ``` /// /// Note that some common forms of interpolation do not satisfy this criterion. For example, /// [`Quat::lerp`] and [`Rot2::nlerp`] are not subdivision-stable. /// /// Furthermore, this is not to be used as a general trait for abstract interpolation. /// Consumers rely on the strong guarantees in order for behavior based on this trait to be /// well-behaved. /// /// [`Quat::lerp`]: crate::Quat::lerp /// [`Rot2::nlerp`]: crate::Rot2::nlerp pub trait StableInterpolate: Clone { /// Interpolate between this value and the `other` given value using the parameter `t`. /// Note that the parameter `t` is not necessarily clamped to lie between `0` and `1`. /// When `t = 0.0`, `self` is recovered, while `other` is recovered at `t = 1.0`, /// with intermediate values lying between the two. fn interpolate_stable(&self, other: &Self, t: f32) -> Self; } ``` This trait has a blanket implementation over `NormedVectorSpace`, where `lerp` is used, along with implementations for `Rot2`, `Quat`, and the direction types using variants of `slerp`. Other areas may choose to implement this trait in order to hook into its functionality, but the stringent requirements must actually be met. This trait bears no direct relationship with `bevy_animation`'s `Animatable` trait, although they may choose to use `interpolate_stable` in their trait implementations if they wish, as both traits involve type-inferred interpolations of the same kind. `StableInterpolate` is not a supertrait of `Animatable` for a couple reasons: 1. Notions of interpolation in animation are generally going to be much more general than those allowed under these constraints. 2. Laying out these generalized interpolation notions is the domain of `bevy_animation` rather than of `bevy_math`. (Consider also that inferring interpolation from types is not universally desirable.) Similarly, this is not implemented on `bevy_color`'s color types, although their current mixing behavior does meet the conditions of the trait. As an aside, the subdivision-stability condition is of interest specifically for the [Curve RFC](bevyengine/rfcs#80), where it also ensures a kind of stability for subsampling. Importantly, this trait ensures that the "smooth following" behavior defined in this PR behaves predictably: ```rust /// Smoothly nudge this value towards the `target` at a given decay rate. The `decay_rate` /// parameter controls how fast the distance between `self` and `target` decays relative to /// the units of `delta`; the intended usage is for `decay_rate` to generally remain fixed, /// while `delta` is something like `delta_time` from an updating system. This produces a /// smooth following of the target that is independent of framerate. /// /// More specifically, when this is called repeatedly, the result is that the distance between /// `self` and a fixed `target` attenuates exponentially, with the rate of this exponential /// decay given by `decay_rate`. /// /// For example, at `decay_rate = 0.0`, this has no effect. /// At `decay_rate = f32::INFINITY`, `self` immediately snaps to `target`. /// In general, higher rates mean that `self` moves more quickly towards `target`. /// /// # Example /// ``` /// # use bevy_math::{Vec3, StableInterpolate}; /// # let delta_time: f32 = 1.0 / 60.0; /// let mut object_position: Vec3 = Vec3::ZERO; /// let target_position: Vec3 = Vec3::new(2.0, 3.0, 5.0); /// // Decay rate of ln(10) => after 1 second, remaining distance is 1/10th /// let decay_rate = f32::ln(10.0); /// // Calling this repeatedly will move `object_position` towards `target_position`: /// object_position.smooth_nudge(&target_position, decay_rate, delta_time); /// ``` fn smooth_nudge(&mut self, target: &Self, decay_rate: f32, delta: f32) { self.interpolate_stable_assign(target, 1.0 - f32::exp(-decay_rate * delta)); } ``` As the documentation indicates, the intention is for this to be called in game update systems, and `delta` would be something like `Time::delta_seconds` in Bevy, allowing positions, orientations, and so on to smoothly follow a target. A new example, `smooth_follow`, demonstrates a basic implementation of this, with a sphere smoothly following a sharply moving target: https://github.com/bevyengine/bevy/assets/2975848/7124b28b-6361-47e3-acf7-d1578ebd0347 ## Testing Tested by running the example with various parameters.
1 parent 1196186 commit a569b35

File tree

5 files changed

+301
-3
lines changed

5 files changed

+301
-3
lines changed

Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,17 @@ description = "Demonstrates how to sample random points from mathematical primit
30353035
category = "Math"
30363036
wasm = true
30373037

3038+
[[example]]
3039+
name = "smooth_follow"
3040+
path = "examples/math/smooth_follow.rs"
3041+
doc-scrape-examples = true
3042+
3043+
[package.metadata.example.smooth_follow]
3044+
name = "Smooth Follow"
3045+
description = "Demonstrates how to make an entity smoothly follow another using interpolation"
3046+
category = "Math"
3047+
wasm = true
3048+
30383049
# Gizmos
30393050
[[example]]
30403051
name = "2d_gizmos"

crates/bevy_math/src/common_traits.rs

+145-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use glam::{Vec2, Vec3, Vec3A, Vec4};
1+
use crate::{Dir2, Dir3, Dir3A, Quat, Rot2, Vec2, Vec3, Vec3A, Vec4};
22
use std::fmt::Debug;
33
use std::ops::{Add, Div, Mul, Neg, Sub};
44

@@ -161,3 +161,147 @@ impl NormedVectorSpace for f32 {
161161
self * self
162162
}
163163
}
164+
165+
/// A type with a natural interpolation that provides strong subdivision guarantees.
166+
///
167+
/// Although the only required method is `interpolate_stable`, many things are expected of it:
168+
///
169+
/// 1. The notion of interpolation should follow naturally from the semantics of the type, so
170+
/// that inferring the interpolation mode from the type alone is sensible.
171+
///
172+
/// 2. The interpolation recovers something equivalent to the starting value at `t = 0.0`
173+
/// and likewise with the ending value at `t = 1.0`. They do not have to be data-identical, but
174+
/// they should be semantically identical. For example, [`Quat::slerp`] doesn't always yield its
175+
/// second rotation input exactly at `t = 1.0`, but it always returns an equivalent rotation.
176+
///
177+
/// 3. Importantly, the interpolation must be *subdivision-stable*: for any interpolation curve
178+
/// between two (unnamed) values and any parameter-value pairs `(t0, p)` and `(t1, q)`, the
179+
/// interpolation curve between `p` and `q` must be the *linear* reparametrization of the original
180+
/// interpolation curve restricted to the interval `[t0, t1]`.
181+
///
182+
/// The last of these conditions is very strong and indicates something like constant speed. It
183+
/// is called "subdivision stability" because it guarantees that breaking up the interpolation
184+
/// into segments and joining them back together has no effect.
185+
///
186+
/// Here is a diagram depicting it:
187+
/// ```text
188+
/// top curve = u.interpolate_stable(v, t)
189+
///
190+
/// t0 => p t1 => q
191+
/// |-------------|---------|-------------|
192+
/// 0 => u / \ 1 => v
193+
/// / \
194+
/// / \
195+
/// / linear \
196+
/// / reparametrization \
197+
/// / t = t0 * (1 - s) + t1 * s \
198+
/// / \
199+
/// |-------------------------------------|
200+
/// 0 => p 1 => q
201+
///
202+
/// bottom curve = p.interpolate_stable(q, s)
203+
/// ```
204+
///
205+
/// Note that some common forms of interpolation do not satisfy this criterion. For example,
206+
/// [`Quat::lerp`] and [`Rot2::nlerp`] are not subdivision-stable.
207+
///
208+
/// Furthermore, this is not to be used as a general trait for abstract interpolation.
209+
/// Consumers rely on the strong guarantees in order for behavior based on this trait to be
210+
/// well-behaved.
211+
///
212+
/// [`Quat::slerp`]: crate::Quat::slerp
213+
/// [`Quat::lerp`]: crate::Quat::lerp
214+
/// [`Rot2::nlerp`]: crate::Rot2::nlerp
215+
pub trait StableInterpolate: Clone {
216+
/// Interpolate between this value and the `other` given value using the parameter `t`. At
217+
/// `t = 0.0`, a value equivalent to `self` is recovered, while `t = 1.0` recovers a value
218+
/// equivalent to `other`, with intermediate values interpolating between the two.
219+
/// See the [trait-level documentation] for details.
220+
///
221+
/// [trait-level documentation]: StableInterpolate
222+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self;
223+
224+
/// A version of [`interpolate_stable`] that assigns the result to `self` for convenience.
225+
///
226+
/// [`interpolate_stable`]: StableInterpolate::interpolate_stable
227+
fn interpolate_stable_assign(&mut self, other: &Self, t: f32) {
228+
*self = self.interpolate_stable(other, t);
229+
}
230+
231+
/// Smoothly nudge this value towards the `target` at a given decay rate. The `decay_rate`
232+
/// parameter controls how fast the distance between `self` and `target` decays relative to
233+
/// the units of `delta`; the intended usage is for `decay_rate` to generally remain fixed,
234+
/// while `delta` is something like `delta_time` from an updating system. This produces a
235+
/// smooth following of the target that is independent of framerate.
236+
///
237+
/// More specifically, when this is called repeatedly, the result is that the distance between
238+
/// `self` and a fixed `target` attenuates exponentially, with the rate of this exponential
239+
/// decay given by `decay_rate`.
240+
///
241+
/// For example, at `decay_rate = 0.0`, this has no effect.
242+
/// At `decay_rate = f32::INFINITY`, `self` immediately snaps to `target`.
243+
/// In general, higher rates mean that `self` moves more quickly towards `target`.
244+
///
245+
/// # Example
246+
/// ```
247+
/// # use bevy_math::{Vec3, StableInterpolate};
248+
/// # let delta_time: f32 = 1.0 / 60.0;
249+
/// let mut object_position: Vec3 = Vec3::ZERO;
250+
/// let target_position: Vec3 = Vec3::new(2.0, 3.0, 5.0);
251+
/// // Decay rate of ln(10) => after 1 second, remaining distance is 1/10th
252+
/// let decay_rate = f32::ln(10.0);
253+
/// // Calling this repeatedly will move `object_position` towards `target_position`:
254+
/// object_position.smooth_nudge(&target_position, decay_rate, delta_time);
255+
/// ```
256+
fn smooth_nudge(&mut self, target: &Self, decay_rate: f32, delta: f32) {
257+
self.interpolate_stable_assign(target, 1.0 - f32::exp(-decay_rate * delta));
258+
}
259+
}
260+
261+
// Conservatively, we presently only apply this for normed vector spaces, where the notion
262+
// of being constant-speed is literally true. The technical axioms are satisfied for any
263+
// VectorSpace type, but the "natural from the semantics" part is less clear in general.
264+
impl<V> StableInterpolate for V
265+
where
266+
V: NormedVectorSpace,
267+
{
268+
#[inline]
269+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
270+
self.lerp(*other, t)
271+
}
272+
}
273+
274+
impl StableInterpolate for Rot2 {
275+
#[inline]
276+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
277+
self.slerp(*other, t)
278+
}
279+
}
280+
281+
impl StableInterpolate for Quat {
282+
#[inline]
283+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
284+
self.slerp(*other, t)
285+
}
286+
}
287+
288+
impl StableInterpolate for Dir2 {
289+
#[inline]
290+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
291+
self.slerp(*other, t)
292+
}
293+
}
294+
295+
impl StableInterpolate for Dir3 {
296+
#[inline]
297+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
298+
self.slerp(*other, t)
299+
}
300+
}
301+
302+
impl StableInterpolate for Dir3A {
303+
#[inline]
304+
fn interpolate_stable(&self, other: &Self, t: f32) -> Self {
305+
self.slerp(*other, t)
306+
}
307+
}

crates/bevy_math/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub mod prelude {
5353
direction::{Dir2, Dir3, Dir3A},
5454
primitives::*,
5555
BVec2, BVec3, BVec4, EulerRot, FloatExt, IRect, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4,
56-
Quat, Ray2d, Ray3d, Rect, Rot2, URect, UVec2, UVec3, UVec4, Vec2, Vec2Swizzles, Vec3,
57-
Vec3Swizzles, Vec4, Vec4Swizzles,
56+
Quat, Ray2d, Ray3d, Rect, Rot2, StableInterpolate, URect, UVec2, UVec3, UVec4, Vec2,
57+
Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles,
5858
};
5959
}
6060

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ Example | Description
332332
[Random Sampling](../examples/math/random_sampling.rs) | Demonstrates how to sample random points from mathematical primitives
333333
[Rendering Primitives](../examples/math/render_primitives.rs) | Shows off rendering for all math primitives as both Meshes and Gizmos
334334
[Sampling Primitives](../examples/math/sampling_primitives.rs) | Demonstrates all the primitives which can be sampled.
335+
[Smooth Follow](../examples/math/smooth_follow.rs) | Demonstrates how to make an entity smoothly follow another using interpolation
335336

336337
## Reflection
337338

examples/math/smooth_follow.rs

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//! This example demonstrates how to use interpolation to make one entity smoothly follow another.
2+
3+
use bevy::math::{prelude::*, vec3, NormedVectorSpace};
4+
use bevy::prelude::*;
5+
use rand::SeedableRng;
6+
use rand_chacha::ChaCha8Rng;
7+
8+
fn main() {
9+
App::new()
10+
.add_plugins(DefaultPlugins)
11+
.add_systems(Startup, setup)
12+
.add_systems(Update, (move_target, move_follower).chain())
13+
.run();
14+
}
15+
16+
// The sphere that the following sphere targets at all times:
17+
#[derive(Component)]
18+
struct TargetSphere;
19+
20+
// The speed of the target sphere moving to its next location:
21+
#[derive(Resource)]
22+
struct TargetSphereSpeed(f32);
23+
24+
// The position that the target sphere always moves linearly toward:
25+
#[derive(Resource)]
26+
struct TargetPosition(Vec3);
27+
28+
// The decay rate used by the smooth following:
29+
#[derive(Resource)]
30+
struct DecayRate(f32);
31+
32+
// The sphere that follows the target sphere by moving towards it with nudging:
33+
#[derive(Component)]
34+
struct FollowingSphere;
35+
36+
/// The source of randomness used by this example.
37+
#[derive(Resource)]
38+
struct RandomSource(ChaCha8Rng);
39+
40+
fn setup(
41+
mut commands: Commands,
42+
mut meshes: ResMut<Assets<Mesh>>,
43+
mut materials: ResMut<Assets<StandardMaterial>>,
44+
) {
45+
// A plane:
46+
commands.spawn(PbrBundle {
47+
mesh: meshes.add(Plane3d::default().mesh().size(12.0, 12.0)),
48+
material: materials.add(Color::srgb(0.3, 0.15, 0.3)),
49+
transform: Transform::from_xyz(0.0, -2.5, 0.0),
50+
..default()
51+
});
52+
53+
// The target sphere:
54+
commands.spawn((
55+
PbrBundle {
56+
mesh: meshes.add(Sphere::new(0.3)),
57+
material: materials.add(Color::srgb(0.3, 0.15, 0.9)),
58+
..default()
59+
},
60+
TargetSphere,
61+
));
62+
63+
// The sphere that follows it:
64+
commands.spawn((
65+
PbrBundle {
66+
mesh: meshes.add(Sphere::new(0.3)),
67+
material: materials.add(Color::srgb(0.9, 0.3, 0.3)),
68+
transform: Transform::from_translation(vec3(0.0, -2.0, 0.0)),
69+
..default()
70+
},
71+
FollowingSphere,
72+
));
73+
74+
// A light:
75+
commands.spawn(PointLightBundle {
76+
point_light: PointLight {
77+
intensity: 15_000_000.0,
78+
shadows_enabled: true,
79+
..default()
80+
},
81+
transform: Transform::from_xyz(4.0, 8.0, 4.0),
82+
..default()
83+
});
84+
85+
// A camera:
86+
commands.spawn(Camera3dBundle {
87+
transform: Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
88+
..default()
89+
});
90+
91+
// Set starting values for resources used by the systems:
92+
commands.insert_resource(TargetSphereSpeed(5.0));
93+
commands.insert_resource(DecayRate(2.0));
94+
commands.insert_resource(TargetPosition(Vec3::ZERO));
95+
commands.insert_resource(RandomSource(ChaCha8Rng::seed_from_u64(68941654987813521)));
96+
}
97+
98+
fn move_target(
99+
mut target: Query<&mut Transform, With<TargetSphere>>,
100+
target_speed: Res<TargetSphereSpeed>,
101+
mut target_pos: ResMut<TargetPosition>,
102+
time: Res<Time>,
103+
mut rng: ResMut<RandomSource>,
104+
) {
105+
let mut target = target.single_mut();
106+
107+
match Dir3::new(target_pos.0 - target.translation) {
108+
// The target and the present position of the target sphere are far enough to have a well-
109+
// defined direction between them, so let's move closer:
110+
Ok(dir) => {
111+
let delta_time = time.delta_seconds();
112+
let abs_delta = (target_pos.0 - target.translation).norm();
113+
114+
// Avoid overshooting in case of high values of `delta_time`:
115+
let magnitude = f32::min(abs_delta, delta_time * target_speed.0);
116+
target.translation += dir * magnitude;
117+
}
118+
119+
// The two are really close, so let's generate a new target position:
120+
Err(_) => {
121+
let legal_region = Cuboid::from_size(Vec3::splat(4.0));
122+
*target_pos = TargetPosition(legal_region.sample_interior(&mut rng.0));
123+
}
124+
}
125+
}
126+
127+
fn move_follower(
128+
mut following: Query<&mut Transform, With<FollowingSphere>>,
129+
target: Query<&Transform, (With<TargetSphere>, Without<FollowingSphere>)>,
130+
decay_rate: Res<DecayRate>,
131+
time: Res<Time>,
132+
) {
133+
let target = target.single();
134+
let mut following = following.single_mut();
135+
let decay_rate = decay_rate.0;
136+
let delta_time = time.delta_seconds();
137+
138+
// Calling `smooth_nudge` is what moves the following sphere smoothly toward the target.
139+
following
140+
.translation
141+
.smooth_nudge(&target.translation, decay_rate, delta_time);
142+
}

0 commit comments

Comments
 (0)