Skip to content

Commit 3ded59e

Browse files
authored
Use quaternionic smooth_nudge in the align example (#14858)
# Objective This example previously had kind of a needlessly complex state machine that tracked moves between its previous orientation and the new one that was randomly generated. Using `smooth_nudge` simplifies the example in addition to making good use of the new API. ## Solution Use `smooth_nudge` to transition between the current transform and the new one. This does away with the need to keep track of the move's starting position and progress. It also just sort of looks nicer. ## Testing Run the `align` example: `cargo run --example align`
1 parent c92ee31 commit 3ded59e

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

examples/transforms/align.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use bevy::color::palettes::basic::{GRAY, RED, WHITE};
44
use bevy::input::mouse::{AccumulatedMouseMotion, MouseButtonInput};
5+
use bevy::math::StableInterpolate;
56
use bevy::prelude::*;
67
use rand::{Rng, SeedableRng};
78
use rand_chacha::ChaCha8Rng;
@@ -18,15 +19,9 @@ fn main() {
1819
/// This struct stores metadata for a single rotational move of the ship
1920
#[derive(Component, Default)]
2021
struct Ship {
21-
/// The initial transform of the ship move, the starting point of interpolation
22-
initial_transform: Transform,
23-
2422
/// The target transform of the ship move, the endpoint of interpolation
2523
target_transform: Transform,
2624

27-
/// The progress of the ship move in percentage points
28-
progress: u16,
29-
3025
/// Whether the ship is currently in motion; allows motion to be paused
3126
in_motion: bool,
3227
}
@@ -92,7 +87,6 @@ fn setup(
9287
..default()
9388
},
9489
Ship {
95-
initial_transform: Transform::IDENTITY,
9690
target_transform: random_axes_target_alignment(&RandomAxes(first, second)),
9791
..default()
9892
},
@@ -147,37 +141,33 @@ fn draw_random_axes(mut gizmos: Gizmos, query: Query<&RandomAxes>) {
147141
}
148142

149143
// Actually update the ship's transform according to its initial source and target
150-
fn rotate_ship(mut ship: Query<(&mut Ship, &mut Transform)>) {
144+
fn rotate_ship(mut ship: Query<(&mut Ship, &mut Transform)>, time: Res<Time>) {
151145
let (mut ship, mut ship_transform) = ship.single_mut();
152146

153147
if !ship.in_motion {
154148
return;
155149
}
156150

157-
let start = ship.initial_transform.rotation;
158-
let end = ship.target_transform.rotation;
159-
160-
let p: f32 = ship.progress.into();
161-
let t = p / 100.;
151+
let target_rotation = ship.target_transform.rotation;
162152

163-
*ship_transform = Transform::from_rotation(start.slerp(end, t));
153+
ship_transform
154+
.rotation
155+
.smooth_nudge(&target_rotation, 3.0, time.delta_seconds());
164156

165-
if ship.progress == 100 {
157+
if ship_transform.rotation.angle_between(target_rotation) <= f32::EPSILON {
166158
ship.in_motion = false;
167-
} else {
168-
ship.progress += 1;
169159
}
170160
}
171161

172162
// Handle user inputs from the keyboard for dynamically altering the scenario
173163
fn handle_keypress(
174-
mut ship: Query<(&mut Ship, &Transform)>,
164+
mut ship: Query<&mut Ship>,
175165
mut random_axes: Query<&mut RandomAxes>,
176166
mut instructions: Query<&mut Visibility, With<Instructions>>,
177167
keyboard: Res<ButtonInput<KeyCode>>,
178168
mut seeded_rng: ResMut<SeededRng>,
179169
) {
180-
let (mut ship, ship_transform) = ship.single_mut();
170+
let mut ship = ship.single_mut();
181171
let mut random_axes = random_axes.single_mut();
182172

183173
if keyboard.just_pressed(KeyCode::KeyR) {
@@ -188,9 +178,7 @@ fn handle_keypress(
188178

189179
// Stop the ship and set it up to transform from its present orientation to the new one
190180
ship.in_motion = false;
191-
ship.initial_transform = *ship_transform;
192181
ship.target_transform = random_axes_target_alignment(&random_axes);
193-
ship.progress = 0;
194182
}
195183

196184
if keyboard.just_pressed(KeyCode::KeyT) {

0 commit comments

Comments
 (0)