Skip to content

Commit 8624f1f

Browse files
mockersfspectria-limina
authored andcommitted
Improve gizmo axes example (#12335)
# Objective - Improve example from #12299 - Make it frame rate independent - Make it not randomly random ## Solution - Transitions between transforms will take 2 seconds instead of 100 frames - Random is seeded
1 parent 145414c commit 8624f1f

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

examples/gizmos/axes.rs

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! This example demonstrates the implementation and behavior of the axes gizmo.
22
use bevy::prelude::*;
33
use bevy::render::primitives::Aabb;
4-
use rand::random;
4+
use rand::{rngs::StdRng, Rng, SeedableRng};
55
use std::f32::consts::PI;
66

7+
const TRANSITION_DURATION: f32 = 2.0;
8+
79
fn main() {
810
App::new()
911
.add_plugins(DefaultPlugins)
@@ -27,15 +29,20 @@ struct TransformTracking {
2729
/// The target transform of the cube during the move
2830
target_transform: Transform,
2931

30-
/// The progress of the cube during the move in percentage points
31-
progress: u16,
32+
/// The progress of the cube during the move in seconds
33+
progress: f32,
3234
}
3335

36+
#[derive(Resource)]
37+
struct SeededRng(StdRng);
38+
3439
fn setup(
3540
mut commands: Commands,
3641
mut meshes: ResMut<Assets<Mesh>>,
3742
mut materials: ResMut<Assets<StandardMaterial>>,
3843
) {
44+
let mut rng = StdRng::seed_from_u64(19878367467713);
45+
3946
// Lights...
4047
commands.spawn(PointLightBundle {
4148
point_light: PointLight {
@@ -62,8 +69,8 @@ fn setup(
6269
ShowAxes,
6370
TransformTracking {
6471
initial_transform: default(),
65-
target_transform: random_transform(),
66-
progress: 0,
72+
target_transform: random_transform(&mut rng),
73+
progress: 0.0,
6774
},
6875
));
6976

@@ -76,8 +83,8 @@ fn setup(
7683
ShowAxes,
7784
TransformTracking {
7885
initial_transform: default(),
79-
target_transform: random_transform(),
80-
progress: 0,
86+
target_transform: random_transform(&mut rng),
87+
progress: 0.0,
8188
},
8289
));
8390

@@ -88,6 +95,8 @@ fn setup(
8895
transform: Transform::from_xyz(0., -2., 0.),
8996
..default()
9097
});
98+
99+
commands.insert_resource(SeededRng(rng));
91100
}
92101

93102
// This system draws the axes based on the cube's transform, with length based on the size of
@@ -100,19 +109,24 @@ fn draw_axes(mut gizmos: Gizmos, query: Query<(&Transform, &Aabb), With<ShowAxes
100109
}
101110

102111
// This system changes the cubes' transforms to interpolate between random transforms
103-
fn move_cubes(mut query: Query<(&mut Transform, &mut TransformTracking)>) {
112+
fn move_cubes(
113+
mut query: Query<(&mut Transform, &mut TransformTracking)>,
114+
time: Res<Time>,
115+
mut rng: ResMut<SeededRng>,
116+
) {
104117
for (mut transform, mut tracking) in &mut query {
105-
let t = tracking.progress as f32 / 100.;
106-
107-
*transform =
108-
interpolate_transforms(tracking.initial_transform, tracking.target_transform, t);
109-
110-
if tracking.progress < 100 {
111-
tracking.progress += 1;
118+
*transform = interpolate_transforms(
119+
tracking.initial_transform,
120+
tracking.target_transform,
121+
tracking.progress / TRANSITION_DURATION,
122+
);
123+
124+
if tracking.progress < TRANSITION_DURATION {
125+
tracking.progress += time.delta_seconds();
112126
} else {
113127
tracking.initial_transform = *transform;
114-
tracking.target_transform = random_transform();
115-
tracking.progress = 0;
128+
tracking.target_transform = random_transform(&mut rng.0);
129+
tracking.progress = 0.0;
116130
}
117131
}
118132
}
@@ -129,31 +143,31 @@ const TRANSLATION_BOUND_UPPER_Z: f32 = 6.;
129143
const SCALING_BOUND_LOWER_LOG: f32 = -1.2;
130144
const SCALING_BOUND_UPPER_LOG: f32 = 1.2;
131145

132-
fn random_transform() -> Transform {
146+
fn random_transform(rng: &mut impl Rng) -> Transform {
133147
Transform {
134-
translation: random_translation(),
135-
rotation: random_rotation(),
136-
scale: random_scale(),
148+
translation: random_translation(rng),
149+
rotation: random_rotation(rng),
150+
scale: random_scale(rng),
137151
}
138152
}
139153

140-
fn random_translation() -> Vec3 {
141-
let x = random::<f32>() * (TRANSLATION_BOUND_UPPER_X - TRANSLATION_BOUND_LOWER_X)
154+
fn random_translation(rng: &mut impl Rng) -> Vec3 {
155+
let x = rng.gen::<f32>() * (TRANSLATION_BOUND_UPPER_X - TRANSLATION_BOUND_LOWER_X)
142156
+ TRANSLATION_BOUND_LOWER_X;
143-
let y = random::<f32>() * (TRANSLATION_BOUND_UPPER_Y - TRANSLATION_BOUND_LOWER_Y)
157+
let y = rng.gen::<f32>() * (TRANSLATION_BOUND_UPPER_Y - TRANSLATION_BOUND_LOWER_Y)
144158
+ TRANSLATION_BOUND_LOWER_Y;
145-
let z = random::<f32>() * (TRANSLATION_BOUND_UPPER_Z - TRANSLATION_BOUND_LOWER_Z)
159+
let z = rng.gen::<f32>() * (TRANSLATION_BOUND_UPPER_Z - TRANSLATION_BOUND_LOWER_Z)
146160
+ TRANSLATION_BOUND_LOWER_Z;
147161

148162
Vec3::new(x, y, z)
149163
}
150164

151-
fn random_scale() -> Vec3 {
152-
let x_factor_log = random::<f32>() * (SCALING_BOUND_UPPER_LOG - SCALING_BOUND_LOWER_LOG)
165+
fn random_scale(rng: &mut impl Rng) -> Vec3 {
166+
let x_factor_log = rng.gen::<f32>() * (SCALING_BOUND_UPPER_LOG - SCALING_BOUND_LOWER_LOG)
153167
+ SCALING_BOUND_LOWER_LOG;
154-
let y_factor_log = random::<f32>() * (SCALING_BOUND_UPPER_LOG - SCALING_BOUND_LOWER_LOG)
168+
let y_factor_log = rng.gen::<f32>() * (SCALING_BOUND_UPPER_LOG - SCALING_BOUND_LOWER_LOG)
155169
+ SCALING_BOUND_LOWER_LOG;
156-
let z_factor_log = random::<f32>() * (SCALING_BOUND_UPPER_LOG - SCALING_BOUND_LOWER_LOG)
170+
let z_factor_log = rng.gen::<f32>() * (SCALING_BOUND_UPPER_LOG - SCALING_BOUND_LOWER_LOG)
157171
+ SCALING_BOUND_LOWER_LOG;
158172

159173
Vec3::new(
@@ -175,16 +189,16 @@ fn elerp(v1: Vec3, v2: Vec3, t: f32) -> Vec3 {
175189
)
176190
}
177191

178-
fn random_rotation() -> Quat {
179-
let dir = random_direction();
180-
let angle = random::<f32>() * 2. * PI;
192+
fn random_rotation(rng: &mut impl Rng) -> Quat {
193+
let dir = random_direction(rng);
194+
let angle = rng.gen::<f32>() * 2. * PI;
181195

182196
Quat::from_axis_angle(dir, angle)
183197
}
184198

185-
fn random_direction() -> Vec3 {
186-
let height = random::<f32>() * 2. - 1.;
187-
let theta = random::<f32>() * 2. * PI;
199+
fn random_direction(rng: &mut impl Rng) -> Vec3 {
200+
let height = rng.gen::<f32>() * 2. - 1.;
201+
let theta = rng.gen::<f32>() * 2. * PI;
188202

189203
build_direction(height, theta)
190204
}

0 commit comments

Comments
 (0)