Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make align deterministic with a seeded random #12518

Merged
merged 1 commit into from Mar 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions examples/transforms/align.rs
Expand Up @@ -6,7 +6,7 @@ use bevy::color::{
};
use bevy::input::mouse::{MouseButton, MouseButtonInput, MouseMotion};
use bevy::prelude::*;
use rand::random;
use rand::{rngs::StdRng, Rng, SeedableRng};
use std::f32::consts::PI;

fn main() {
Expand Down Expand Up @@ -43,13 +43,18 @@ struct Instructions;
#[derive(Resource)]
struct MousePressed(bool);

#[derive(Resource)]
struct SeededRng(StdRng);

// Setup

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let mut seeded_rng = StdRng::seed_from_u64(19878367467712);

// A camera looking at the origin
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(3., 2.5, 4.).looking_at(Vec3::ZERO, Vec3::Y),
Expand All @@ -75,8 +80,8 @@ fn setup(
});

// Initialize random axes
let first = random_direction();
let second = random_direction();
let first = random_direction(&mut seeded_rng);
let second = random_direction(&mut seeded_rng);
commands.spawn(RandomAxes(first, second));

// Finally, our cube that is going to rotate
Expand Down Expand Up @@ -119,6 +124,7 @@ fn setup(
));

commands.insert_resource(MousePressed(false));
commands.insert_resource(SeededRng(seeded_rng));
}

// Update systems
Expand Down Expand Up @@ -172,14 +178,15 @@ fn handle_keypress(
mut random_axes: Query<&mut RandomAxes>,
mut instructions: Query<&mut Visibility, With<Instructions>>,
keyboard: Res<ButtonInput<KeyCode>>,
mut seeded_rng: ResMut<SeededRng>,
) {
let (mut cube, cube_transform) = cube.single_mut();
let mut random_axes = random_axes.single_mut();

if keyboard.just_pressed(KeyCode::KeyR) {
// Randomize the target axes
let first = random_direction();
let second = random_direction();
let first = random_direction(&mut seeded_rng.0);
let second = random_direction(&mut seeded_rng.0);
*random_axes = RandomAxes(first, second);

// Stop the cube and set it up to transform from its present orientation to the new one
Expand Down Expand Up @@ -236,9 +243,9 @@ fn arrow_ends(transform: &Transform, axis: Vec3, length: f32) -> (Vec3, Vec3) {
(transform.translation, transform.translation + local_vector)
}

fn random_direction() -> Vec3 {
let height = random::<f32>() * 2. - 1.;
let theta = random::<f32>() * 2. * PI;
fn random_direction(rng: &mut impl Rng) -> Vec3 {
let height = rng.gen::<f32>() * 2. - 1.;
let theta = rng.gen::<f32>() * 2. * PI;

build_direction(height, theta)
}
Expand Down