Skip to content

Commit

Permalink
height maxes at 0 at more places
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 22, 2024
1 parent 1046451 commit 1f31f1b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
4 changes: 3 additions & 1 deletion geom/src/heightmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ impl<const RESOLUTION: usize, const SIZE: u32> Heightmap<RESOLUTION, SIZE> {
.filter_map(|(x, y)| {
let chunk_id = (x as u16, y as u16);
let corner = vec2(x as f32, y as f32) * SIZE as f32;
let (t_min, t_max) = self.get_chunk(chunk_id)?.bbox(corner).raycast(ray)?;
let mut bbox = self.get_chunk(chunk_id)?.bbox(corner);
bbox.ll.z -= Self::CELL_SIZE; // give a bit of margin to avoid missing intersections when the heightmap is at the lowest
let (t_min, t_max) = bbox.raycast(ray)?;
Some((t_min, t_max))
});

Expand Down
9 changes: 7 additions & 2 deletions native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,13 @@ impl engine::framework::State for State {
if let Some(ray) = ray {
let cast = map.environment.raycast(ray);

self.uiw.write::<InputMap>().unprojected = cast.map(|x| x.0);
self.uiw.write::<InputMap>().unprojected_normal = cast.map(|x| x.1);
let inp = &mut self.uiw.write::<InputMap>();

inp.unprojected = cast.map(|(mut proj, _)| {
proj.z = proj.z.max(0.0);
proj
});
inp.unprojected_normal = cast.map(|x| x.1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion native_app/src/inputmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct InputMap {
pub act: FastSet<InputAction>,
/// Mouse wheel delta
pub wheel: f32,
/// Mouse position in world space on the terrain
/// Mouse position in world space on the terrain (max height 0)
pub unprojected: Option<Vec3>,

pub unprojected_normal: Option<Vec3>,
Expand Down
2 changes: 1 addition & 1 deletion native_app/src/rendering/map_rendering/map_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ fn inter_pylon(
) {
let interpos = inter.pos.up(ROAD_Z_OFFSET);

let h = unwrap_ret!(env.height(inter.pos.xy()));
let h = unwrap_ret!(env.true_height(inter.pos.xy()));
if (h - interpos.z).abs() <= 2.0 {
return;
}
Expand Down
13 changes: 5 additions & 8 deletions simulation/src/map/objects/road.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Road {
interfaced_points
.equipoints_dir(80.0, true)
.filter_map(move |(pos, dir)| {
let h = env.height(pos.xy())?;
let h = env.true_height(pos.xy())?;
if (h - pos.z).abs() <= 2.0 {
return None;
}
Expand Down Expand Up @@ -331,13 +331,10 @@ impl Road {
)
.chain(std::iter::once(p.last()))
{
let h = env
.height(pos)
.unwrap_or_else(|| {
height_error = true;
0.0
})
.max(0.0);
let h = env.height(pos).unwrap_or_else(|| {
height_error = true;
0.0
});
contour.push(h);
points.push(pos.z(h));
}
Expand Down
6 changes: 6 additions & 0 deletions simulation/src/map/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ impl Environment {
me
}

/// Returns the height of the terrain at the given position in meters, capped at 0
pub fn height(&self, pos: Vec2) -> Option<f32> {
self.heightmap.height(pos).map(|x| x.max(0.0))
}

/// Returns the height of the terrain at the given position in meters, not capped at 0 (can be negative in water)
pub fn true_height(&self, pos: Vec2) -> Option<f32> {
self.heightmap.height(pos)
}

Expand Down

0 comments on commit 1f31f1b

Please sign in to comment.