Use smooth_nudge in 2d_top_down_camera example (#13907)

# Objective


[`StableInterpolate`](https://dev-docs.bevyengine.org/bevy/math/trait.StableInterpolate.html)
was introduced after this example was written (or more accurately, the
two PRs were merged on the same day and developed in parallel).

The example used `Vec3::lerp` where I believe it is now preferred to use
`smooth_nudge`.

## Solution

Its not entirely clear to me whether `StableInterpolate` should be
preferred in this scenario, although it seems likely. So I figured a PR
to make the change would either result in it being merged or denied with
a reason.

## Testing

```
cargo run --example 2d_top_down_camera
```

Co-authored-by: François Mockers <mockersf@gmail.com>
This commit is contained in:
Chris Biscardi 2024-06-18 04:38:17 -07:00 committed by GitHub
parent c37e81b34a
commit 20eb13f458
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,8 +17,8 @@ use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle};
/// Player movement speed factor. /// Player movement speed factor.
const PLAYER_SPEED: f32 = 100.; const PLAYER_SPEED: f32 = 100.;
/// Camera lerp factor. /// How quickly should the camera snap to the desired location.
const CAM_LERP_FACTOR: f32 = 2.; const CAMERA_DECAY_RATE: f32 = 2.;
#[derive(Component)] #[derive(Component)]
struct Player; struct Player;
@ -103,14 +103,11 @@ fn update_camera(
let Vec3 { x, y, .. } = player.translation; let Vec3 { x, y, .. } = player.translation;
let direction = Vec3::new(x, y, camera.translation.z); let direction = Vec3::new(x, y, camera.translation.z);
// Applies a smooth effect to camera movement using interpolation between // Applies a smooth effect to camera movement using stable interpolation
// the camera position and the player position on the x and y axes. // between the camera position and the player position on the x and y axes.
// Here we use the in-game time, to get the elapsed time (in seconds) camera
// since the previous update. This avoids jittery movement when tracking
// the player.
camera.translation = camera
.translation .translation
.lerp(direction, time.delta_seconds() * CAM_LERP_FACTOR); .smooth_nudge(&direction, CAMERA_DECAY_RATE, time.delta_seconds());
} }
/// Update the player position with keyboard inputs. /// Update the player position with keyboard inputs.