Skip to content

Commit

Permalink
Use a newtype instead of a Vec2 for tile coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 27, 2023
1 parent fc03d86 commit 3a4e252
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/geopos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy::prelude::*;
use std::f32::consts::PI;

use crate::tilemap::TileCoord;

/**
* Geo-position on the (OSM-) world map (GPS position)
*
Expand All @@ -25,11 +27,11 @@ impl GeoPos {
* @param zoom Zoom level of the OSM tile-name(x/y) system
* @return coordinate in tile coordinates
*/
pub fn to_tile_coordinates(self, zoom: u8) -> Vec2 {
pub fn to_tile_coordinates(self, zoom: u8) -> TileCoord {
let pow_zoom = 2_u32.pow(zoom.into()) as f32;

// return
Vec2 {
TileCoord(Vec2 {
// Longitude, (Längengrad) West/East "index"
x: ((self.lon + 180.) / 360. * pow_zoom),

Expand All @@ -40,6 +42,6 @@ impl GeoPos {
* pow_zoom),
// The Nort/South y tile name part is not linear, the tiles gets stretched to the poles
// to compensate the stretching if the stretching of the West/East projection
}
})
}
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bevy_screen_diagnostics::{
use geopos::GeoPos;
use http_assets::HttpAssetReaderPlugin;
use sun::Sky;
use tilemap::TileCoord;

type TileMap = tilemap::TileMap<8145>;

Expand Down Expand Up @@ -99,7 +100,7 @@ fn setup(

if let Some((lat, lon)) = lat.zip(lon) {
let pos = GeoPos { lat, lon };
Vec2 { x, y } = pos.to_tile_coordinates(15);
TileCoord(Vec2 { x, y }) = pos.to_tile_coordinates(15);
}

commands.spawn((
Expand Down
3 changes: 3 additions & 0 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,6 @@ impl<const TILE_SIZE: u32> TileMap<TILE_SIZE> {
Transform::from_xyz(pos.x, 0., pos.y)
}
}

/// A coordinate in the tile coordinate system. Allows for positions within a tile.
pub struct TileCoord(pub Vec2);

0 comments on commit 3a4e252

Please sign in to comment.