From 3ded59ed47985b346ea12db2abc73138e2e893b4 Mon Sep 17 00:00:00 2001 From: Matty Date: Fri, 23 Aug 2024 12:21:23 -0400 Subject: [PATCH] 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` --- examples/transforms/align.rs | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/examples/transforms/align.rs b/examples/transforms/align.rs index 681523b1be..1e84ca38c3 100644 --- a/examples/transforms/align.rs +++ b/examples/transforms/align.rs @@ -2,6 +2,7 @@ use bevy::color::palettes::basic::{GRAY, RED, WHITE}; use bevy::input::mouse::{AccumulatedMouseMotion, MouseButtonInput}; +use bevy::math::StableInterpolate; use bevy::prelude::*; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; @@ -18,15 +19,9 @@ fn main() { /// This struct stores metadata for a single rotational move of the ship #[derive(Component, Default)] struct Ship { - /// The initial transform of the ship move, the starting point of interpolation - initial_transform: Transform, - /// The target transform of the ship move, the endpoint of interpolation target_transform: Transform, - /// The progress of the ship move in percentage points - progress: u16, - /// Whether the ship is currently in motion; allows motion to be paused in_motion: bool, } @@ -92,7 +87,6 @@ fn setup( ..default() }, Ship { - initial_transform: Transform::IDENTITY, target_transform: random_axes_target_alignment(&RandomAxes(first, second)), ..default() }, @@ -147,37 +141,33 @@ fn draw_random_axes(mut gizmos: Gizmos, query: Query<&RandomAxes>) { } // Actually update the ship's transform according to its initial source and target -fn rotate_ship(mut ship: Query<(&mut Ship, &mut Transform)>) { +fn rotate_ship(mut ship: Query<(&mut Ship, &mut Transform)>, time: Res