Skip to content

Commit

Permalink
Correctly compute the radius in tiles at any given position
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 30, 2023
1 parent 74ec361 commit 7301c0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/geopos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ impl GeoPos {
lon: geo.longitude() as f32 / PI * 180.0,
}
}

/// Tile width and height in meters
pub fn tile_size(self, zoom: u8) -> Vec2 {
let pow_zoom = 2_u32.pow(zoom.into()) as f32;
let tile_width = EQUATOR_METERS / pow_zoom;
let tile_height = tile_width * self.lat.cos();
Vec2::new(tile_width, tile_height.abs())
}
}

pub const EARTH_RADIUS: f32 = 6378000.;
pub const EQUATOR_METERS: f32 = 40_075_016.686;
13 changes: 5 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn load_next_tile(
} else if fps > 59.5 {
sky.scale = Vec3::splat(sky.scale.x * 1.01)
}
sky.scale = Vec3::splat(sky.scale.x.clamp(1000.0, 10000.0));
sky.scale = Vec3::splat(sky.scale.x.clamp(1000.0, 3000.0));
fog.single_mut().falloff = FogFalloff::from_visibility_colors(
sky.scale.x, // distance in world units up to which objects retain visibility (>= 5% contrast)
Color::rgb(0.35, 0.5, 0.66), // atmospheric extinction color (after light is lost due to absorption by atmospheric particles)
Expand All @@ -175,13 +175,10 @@ fn load_next_tile(
}
}

let origin = GeoPos::from_cartesian(pos - transform.translation).to_tile_coordinates(15);
let (x, y) = pos.any_orthonormal_pair();
let radius =
GeoPos::from_cartesian(pos - transform.translation + x * sky.scale.x + y * sky.scale.x)
.to_tile_coordinates(15)
.0
- origin.0;
let origin = GeoPos::from_cartesian(pos - transform.translation);
let tile_size = origin.tile_size(15);
let radius = sky.scale.x / tile_size;
let origin = origin.to_tile_coordinates(15);

tilemap.load_next(
id,
Expand Down

0 comments on commit 7301c0f

Please sign in to comment.