make align deterministic with a seeded random (#12518)

# Objective

- Make example align deterministic so that it's easier to check for
regression

## Solution

- Use a seeded random
This commit is contained in:
François Mockers 2024-03-17 19:36:43 +01:00 committed by GitHub
parent ec3e7afa4e
commit 17c3faff07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,7 @@ use bevy::color::{
}; };
use bevy::input::mouse::{MouseButton, MouseButtonInput, MouseMotion}; use bevy::input::mouse::{MouseButton, MouseButtonInput, MouseMotion};
use bevy::prelude::*; use bevy::prelude::*;
use rand::random; use rand::{rngs::StdRng, Rng, SeedableRng};
use std::f32::consts::PI; use std::f32::consts::PI;
fn main() { fn main() {
@ -43,6 +43,9 @@ struct Instructions;
#[derive(Resource)] #[derive(Resource)]
struct MousePressed(bool); struct MousePressed(bool);
#[derive(Resource)]
struct SeededRng(StdRng);
// Setup // Setup
fn setup( fn setup(
@ -50,6 +53,8 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let mut seeded_rng = StdRng::seed_from_u64(19878367467712);
// A camera looking at the origin // A camera looking at the origin
commands.spawn(Camera3dBundle { commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(3., 2.5, 4.).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(3., 2.5, 4.).looking_at(Vec3::ZERO, Vec3::Y),
@ -75,8 +80,8 @@ fn setup(
}); });
// Initialize random axes // Initialize random axes
let first = random_direction(); let first = random_direction(&mut seeded_rng);
let second = random_direction(); let second = random_direction(&mut seeded_rng);
commands.spawn(RandomAxes(first, second)); commands.spawn(RandomAxes(first, second));
// Finally, our cube that is going to rotate // Finally, our cube that is going to rotate
@ -119,6 +124,7 @@ fn setup(
)); ));
commands.insert_resource(MousePressed(false)); commands.insert_resource(MousePressed(false));
commands.insert_resource(SeededRng(seeded_rng));
} }
// Update systems // Update systems
@ -172,14 +178,15 @@ fn handle_keypress(
mut random_axes: Query<&mut RandomAxes>, mut random_axes: Query<&mut RandomAxes>,
mut instructions: Query<&mut Visibility, With<Instructions>>, mut instructions: Query<&mut Visibility, With<Instructions>>,
keyboard: Res<ButtonInput<KeyCode>>, keyboard: Res<ButtonInput<KeyCode>>,
mut seeded_rng: ResMut<SeededRng>,
) { ) {
let (mut cube, cube_transform) = cube.single_mut(); let (mut cube, cube_transform) = cube.single_mut();
let mut random_axes = random_axes.single_mut(); let mut random_axes = random_axes.single_mut();
if keyboard.just_pressed(KeyCode::KeyR) { if keyboard.just_pressed(KeyCode::KeyR) {
// Randomize the target axes // Randomize the target axes
let first = random_direction(); let first = random_direction(&mut seeded_rng.0);
let second = random_direction(); let second = random_direction(&mut seeded_rng.0);
*random_axes = RandomAxes(first, second); *random_axes = RandomAxes(first, second);
// Stop the cube and set it up to transform from its present orientation to the new one // Stop the cube and set it up to transform from its present orientation to the new one
@ -236,9 +243,9 @@ fn arrow_ends(transform: &Transform, axis: Vec3, length: f32) -> (Vec3, Vec3) {
(transform.translation, transform.translation + local_vector) (transform.translation, transform.translation + local_vector)
} }
fn random_direction() -> Vec3 { fn random_direction(rng: &mut impl Rng) -> Vec3 {
let height = random::<f32>() * 2. - 1.; let height = rng.gen::<f32>() * 2. - 1.;
let theta = random::<f32>() * 2. * PI; let theta = rng.gen::<f32>() * 2. * PI;
build_direction(height, theta) build_direction(height, theta)
} }