From ed1ef1bb1fe3faa70584d547782250db8da1c80b Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 19 Apr 2024 22:41:16 +0200 Subject: [PATCH] fix asteroids spawing inside the sun rather than around jupiter --- src/world.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/world.rs b/src/world.rs index a12a665..a4e0923 100644 --- a/src/world.rs +++ b/src/world.rs @@ -208,16 +208,24 @@ fn spawn_despawn_asteroids( asteroid2_handle: Res, mut q_asteroid: Query<(Entity, &SceneInstance), With>, mut last_player_cell: Local, + id2pos: Res, ) { if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() { //if q_player.is_empty() { return; } + let jupiter_pos = if let Some(jupiter_pos) = id2pos.0.get(&"jupiter".to_string()) { + *jupiter_pos + } else { + error!("Can't spawn asteroids because Jupiter's position can not be determined"); + return; + }; let player = q_player.get_single().unwrap(); + let fromjupiter = player.0 - jupiter_pos; let player_cell = I64Vec3::new( - (player.x / ASTEROID_SPAWN_STEP).round() as i64, - (player.y / ASTEROID_SPAWN_STEP).round() as i64, - (player.z / ASTEROID_SPAWN_STEP).round() as i64, + (fromjupiter.x / ASTEROID_SPAWN_STEP).round() as i64, + (fromjupiter.y / ASTEROID_SPAWN_STEP).round() as i64, + (fromjupiter.z / ASTEROID_SPAWN_STEP).round() as i64, ); if *last_player_cell == player_cell { return; @@ -261,13 +269,13 @@ fn spawn_despawn_asteroids( } // Density based on the radius alone - let radius_plane = (player.x * player.x + player.z * player.z).sqrt(); + let radius_plane = (fromjupiter.x * fromjupiter.x + fromjupiter.z * fromjupiter.z).sqrt(); let density_r = nature::ring_density((radius_plane / 1e6) as f32); if density_r < 0.001 { return; } // Density based on radius and the vertical distance to the ring - let normalized_distance = player.y / (RING_THICKNESS / 2.0); + let normalized_distance = fromjupiter.y / (RING_THICKNESS / 2.0); let density = density_r * (-4.0 * normalized_distance.powf(2.0)).exp() as f32; if density < 0.001 { return; @@ -318,7 +326,7 @@ fn spawn_despawn_asteroids( //let max_viewdist = ASTEROID_VIEW_RADIUS / ASTEROID_SPAWN_STEP; let wobble = ASTEROID_SPAWN_STEP * 0.5; - let pos = DVec3::new( + let pos = jupiter_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, origin.z as f64 * ASTEROID_SPAWN_STEP + wobble * rand_z * 2.0 - 1.0,