fix asteroids spawing inside the sun rather than around jupiter

This commit is contained in:
yuni 2024-04-19 22:41:16 +02:00
parent 5c29681ee3
commit ed1ef1bb1f

View file

@ -208,16 +208,24 @@ fn spawn_despawn_asteroids(
asteroid2_handle: Res<AsteroidModel2>,
mut q_asteroid: Query<(Entity, &SceneInstance), With<Asteroid>>,
mut last_player_cell: Local<I64Vec3>,
id2pos: Res<actor::Id2Pos>,
) {
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,