Skip to content

Commit

Permalink
Use Position more often, and use its convenience functions
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 12, 2023
1 parent d81c88f commit 2d3cbe8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
21 changes: 16 additions & 5 deletions src/geopos.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::tilemap::TileCoord;
use crate::{player::Position, tilemap::TileCoord, GalacticTransformOwned};
use bevy::prelude::*;
use big_space::FloatingOriginSettings;
use glam::DVec3;
use globe_rs::{CartesianPoint, GeographicPoint};
use std::f32::consts::PI;
Expand Down Expand Up @@ -55,7 +56,9 @@ impl GeoPos {
TileCoord::new(Vec2 { x, y }, zoom)
}

pub fn to_cartesian(self) -> DVec3 {
/// Prefer using `to_cartesian`, which returns a [`Position`] that has a lot more convenience
/// methods.
pub fn to_cartesian_vec(self) -> DVec3 {
let geo = GeographicPoint::new(
(self.lon as f64).to_radians(),
(self.lat as f64).to_radians(),
Expand All @@ -65,6 +68,14 @@ impl GeoPos {
DVec3::new(-cart.x(), -cart.y(), cart.z())
}

pub fn to_cartesian(self, space: &FloatingOriginSettings) -> Position<'_> {
let pos = self.to_cartesian_vec();
let (cell, pos) = space.translation_to_grid(pos);
let transform = Transform::from_translation(pos);
let pos = GalacticTransformOwned { transform, cell };
Position { pos, space }
}

pub fn from_cartesian(pos: DVec3) -> Self {
let cart = CartesianPoint::new(-pos.x, -pos.y, pos.z);
let geo = GeographicPoint::from_cartesian(&cart);
Expand All @@ -77,9 +88,9 @@ impl GeoPos {
/// Tile width and height in meters
pub fn tile_size(self, zoom: u8) -> Vec2 {
let coord = self.to_tile_coordinates(zoom);
let pos = self.to_cartesian();
let x = coord.right().to_geo_pos().to_cartesian().distance(pos) as f32;
let y = coord.down().to_geo_pos().to_cartesian().distance(pos) as f32;
let pos = self.to_cartesian_vec();
let x = coord.right().to_geo_pos().to_cartesian_vec().distance(pos) as f32;
let y = coord.down().to_geo_pos().to_cartesian_vec().distance(pos) as f32;
Vec2 { x, y }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn main() {

let mut app = App::new();
app.insert_resource(Args {
starting_position: pos.to_cartesian(),
starting_position: pos.to_cartesian_vec(),
height,
direction,
view,
Expand Down
19 changes: 12 additions & 7 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy::{
};
use big_space::FloatingOriginSettings;

use crate::GalacticGrid;
use crate::{GalacticGrid, GalacticTransformOwned};

mod coord;
mod index;
Expand Down Expand Up @@ -138,9 +138,9 @@ impl TileMap {
LoadState::NotLoaded | LoadState::Loading => unreachable!(),
LoadState::Loaded => {
entity.remove::<PbrBundle>();
let (grid, transform) = pos.to_cartesian(&space);
let GalacticTransformOwned { transform, cell } = pos.to_cartesian(&space);
let scene = scenes.get(scene).unwrap().scenes[0].clone();
entity.insert(grid);
entity.insert(cell);
entity.insert(SceneBundle {
scene, // "models/17430_11371.glb#Scene0"
transform,
Expand Down Expand Up @@ -178,10 +178,15 @@ fn flat_tile(

// Four corners of the tile in cartesian coordinates relative to the
// planet's center.
let a = coord.to_geo_pos().to_cartesian();
let b = pos.right().as_coord().to_geo_pos().to_cartesian();
let c = pos.down().right().as_coord().to_geo_pos().to_cartesian();
let d = pos.down().as_coord().to_geo_pos().to_cartesian();
let a = coord.to_geo_pos().to_cartesian_vec();
let b = pos.right().as_coord().to_geo_pos().to_cartesian_vec();
let c = pos
.down()
.right()
.as_coord()
.to_geo_pos()
.to_cartesian_vec();
let d = pos.down().as_coord().to_geo_pos().to_cartesian_vec();

// Normals on a sphere are just the position on the sphere normalized.
let normals = vec![
Expand Down
1 change: 1 addition & 0 deletions src/tilemap/coord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl TileCoord {
}
}

#[allow(dead_code)]
pub fn up(self) -> Self {
Self {
pos: self.pos - Vec2::Y,
Expand Down
25 changes: 7 additions & 18 deletions src/tilemap/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Display;

use super::coord::TileCoord;
use crate::GalacticGrid;
use crate::{player::Directions, GalacticTransformOwned};
use bevy::prelude::*;
use big_space::FloatingOriginSettings;

Expand Down Expand Up @@ -64,24 +64,13 @@ impl TileIndex {
}
}

pub fn to_cartesian(self, space: &FloatingOriginSettings) -> (GalacticGrid, Transform) {
pub fn to_cartesian(self, space: &FloatingOriginSettings) -> GalacticTransformOwned {
let coord = self.as_coord().center();
let pos = coord.to_geo_pos().to_cartesian();
let up = pos.normalize().as_vec3();
let next = coord.up().to_geo_pos().to_cartesian();
let (grid, pos) = space.translation_to_grid(pos);
let (grid_next, next) = space.translation_to_grid(next);
let diff = grid_next - grid;
let diff = Vec3 {
x: diff.x as f32 * space.grid_edge_length(),
y: diff.y as f32 * space.grid_edge_length(),
z: diff.z as f32 * space.grid_edge_length(),
};
let next = next + diff;
(
grid,
Transform::from_translation(pos).looking_to(next - pos, up),
)
let pos = coord.to_geo_pos().to_cartesian(space);
let Directions { up, north, west: _ } = pos.directions();
let mut pos = pos.pos;
pos.transform.look_to(north, up);
pos
}

pub fn zoom(&self) -> u8 {
Expand Down

0 comments on commit 2d3cbe8

Please sign in to comment.