From 3ea2f3bc3a43c7cb61386bfdacfe064504ff4fdf Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 2 Apr 2024 05:51:50 +0200 Subject: [PATCH] use fastrand for better asteroid distribution --- Cargo.lock | 5 +++-- Cargo.toml | 1 + src/world.rs | 32 ++++++++++++++++++-------------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ec62f2..b9adf28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1704,9 +1704,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.1" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" [[package]] name = "fdeflate" @@ -2753,6 +2753,7 @@ dependencies = [ "bevy", "bevy_embedded_assets", "bevy_xpbd_3d", + "fastrand", "regex", ] diff --git a/Cargo.toml b/Cargo.toml index 5056c06..d3a7c95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ regex = "1" bevy = { version = "0.13.1", default-features = false, features = ["jpeg", "bevy_asset", "bevy_audio", "bevy_scene", "bevy_winit", "bevy_core_pipeline", "bevy_pbr", "bevy_gltf", "bevy_render", "bevy_text", "bevy_ui", "file_watcher", "multi-threaded", "png", "vorbis", "x11", "tonemapping_luts"]} bevy_xpbd_3d = { version = "0.4.2", default-features = false, features = ["3d", "f64", "parry-f64", "parallel", "async-collider"] } bevy_embedded_assets = "0.10.2" +fastrand = "2.0.2" [features] dev = ["bevy/dynamic_linking"] diff --git a/src/world.rs b/src/world.rs index e6850c3..b0063cb 100644 --- a/src/world.rs +++ b/src/world.rs @@ -6,9 +6,10 @@ use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::plugins::sync::SyncConfig; use std::collections::HashMap; use std::f32::consts::PI; +use fastrand; const ASTEROID_UPDATE_INTERVAL: f32 = 1.0; // seconds -const ASTEROID_SIZE_FACTOR: f32 = 5.0; +const ASTEROID_SIZE_FACTOR: f32 = 3.0; const STARS_MAX_MAGNITUDE: f32 = 5.5; const CENTER_WORLD_ON_PLAYER: bool = true; @@ -232,6 +233,7 @@ fn spawn_despawn_asteroids( } } + let mut rng = fastrand::Rng::new(); for x in -stepmax..=stepmax { for y in -stepmax..=stepmax { for z in -stepmax..=stepmax { @@ -245,9 +247,15 @@ fn spawn_despawn_asteroids( continue; } - // generate some deterministic pseudorandom numbers seeded with the origin coordinates - let rand_s = (4.0*((origin.x+origin.y+origin.z) as f64).sin() - + 3.0*((origin.x+origin.y+origin.z) as f64).cos()).abs() % 1.0; + // Get a seed based on all of the origin axes + // Probably there's a faster way + rng.seed(origin.x as u64); + let seed = rng.u64(0..u64::MAX).wrapping_add(origin.y as u64); + rng.seed(seed); + let seed = rng.u64(0..u64::MAX).wrapping_add(origin.z as u64); + rng.seed(seed); + + let rand_s = rng.f32(); let size: f32 = (rand_s + 1e-6).powf(-0.5) as f32; // -> between ~1 and 1000 if rand_s > 0.7 { @@ -258,18 +266,14 @@ fn spawn_despawn_asteroids( spawned_near += 1; } - let rand_x = (4.0*((origin.x+origin.y) as f64).sin() - + 3.0*((origin.y+origin.z) as f64).cos()) % 1.0; - let rand_y = (4.0*((origin.y+origin.z) as f64).sin() - + 3.0*((origin.z+origin.x) as f64).cos()) % 1.0; - let rand_z = (40.0*((origin.z+origin.x) as f64).sin() - + 30.0*((origin.x+origin.y) as f64).cos()) % 1.0; - let rand_c = (8.0*((origin.x+origin.z) as f64).sin() - + 4.0*((origin.x+origin.z) as f64).cos()).abs() % 1.0; - let class = if rand_c < 0.5 { 0 } else { 1 }; + let rand_x = rng.f64(); + let rand_y = rng.f64(); + let rand_z = rng.f64(); + let rand_c = rng.bool(); + let class = if rand_c { 0 } else { 1 }; //let max_viewdist = ASTEROID_VIEW_RADIUS / ASTEROID_SPAWN_STEP; - let wobble = ASTEROID_SPAWN_STEP * 0.3; + let wobble = ASTEROID_SPAWN_STEP * 0.5; let pos = DVec3::new( origin.x as f64 * ASTEROID_SPAWN_STEP + wobble * rand_x * 2.0 - 1.0, origin.y as f64 * ASTEROID_SPAWN_STEP + wobble * rand_y * 2.0 - 1.0,