Randomize height

Description

Our solid cube is very boring. To add some variation, we can adjust the height of each coordinate.

This change specifies a max height for each XZ coordinate in our terrain. The values are randomly chosen between 1 and the y height.

The effect looks like a bunch of noise, but it's a start.

Screenshot

Commands

git clone git@github.com:atsheehan/iridium
cd iridium
git checkout 20ccd598328c8f2eb7a965ac2e843bd4eb4c31a3
cargo run --release

Code Changes

Modified src/world.rsGitHub

@@ -1,13 +1,13 @@
1- use crate::math::Vec3;
1+ use crate::math::{RandomNumberGenerator, Vec3};
22
33 const MOUSE_SENSITIVITY: f32 = 0.001;
44 const MOVE_SPEED: f32 = 0.5;
55
66 pub(crate) struct World {
77 x_width: u32,
8- y_height: u32,
98 z_depth: u32,
109 camera: Camera,
10+ heights: Vec<u32>,
1111 }
1212
1313 impl World {
@@ -21,11 +21,20 @@
2121 pitch: 0.0,
2222 };
2323
24+ let mut rng = RandomNumberGenerator::with_seed(42);
25+ let xz_area = (x_width * z_depth) as usize;
26+
27+ let mut heights = Vec::with_capacity(xz_area);
28+
29+ for _ in 0..xz_area {
30+ heights.push(rng.gen_range(1, y_height));
31+ }
32+
2433 Self {
2534 x_width,
26- y_height,
2735 z_depth,
2836 camera,
37+ heights,
2938 }
3039 }
3140
@@ -34,17 +43,13 @@
3443 self.camera.position = self.camera.position + actual_velocity;
3544 }
3645
37- pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> {
38- let x_start = 0;
39- let x_end = self.x_width;
40- let y_start = 0;
41- let y_end = self.y_height;
42- let z_start = 0;
43- let z_end = self.z_depth;
46+ pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
47+ (0..self.z_depth).flat_map(move |z| {
48+ (0..self.x_width).flat_map(move |x| {
49+ let index = ((self.x_width * z) + x) as usize;
50+ let y_max = self.heights[index];
4451
45- (x_start..x_end).flat_map(move |x| {
46- (y_start..y_end).flat_map(move |y| {
47- (z_start..z_end).map(move |z| Vec3(x as f32, y as f32, z as f32))
52+ (0..y_max).map(move |y| Vec3(x as f32, y as f32, z as f32))
4853 })
4954 })
5055 }
@@ -1,13 +1,13 @@
1- use crate::math::Vec3;
2
3 const MOUSE_SENSITIVITY: f32 = 0.001;
4 const MOVE_SPEED: f32 = 0.5;
5
6 pub(crate) struct World {
7 x_width: u32,
8- y_height: u32,
9 z_depth: u32,
10 camera: Camera,
 
11 }
12
13 impl World {
@@ -21,11 +21,20 @@
21 pitch: 0.0,
22 };
23
 
 
 
 
 
 
 
 
 
24 Self {
25 x_width,
26- y_height,
27 z_depth,
28 camera,
 
29 }
30 }
31
@@ -34,17 +43,13 @@
34 self.camera.position = self.camera.position + actual_velocity;
35 }
36
37- pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> {
38- let x_start = 0;
39- let x_end = self.x_width;
40- let y_start = 0;
41- let y_end = self.y_height;
42- let z_start = 0;
43- let z_end = self.z_depth;
44
45- (x_start..x_end).flat_map(move |x| {
46- (y_start..y_end).flat_map(move |y| {
47- (z_start..z_end).map(move |z| Vec3(x as f32, y as f32, z as f32))
48 })
49 })
50 }
@@ -1,13 +1,13 @@
1+ use crate::math::{RandomNumberGenerator, Vec3};
2
3 const MOUSE_SENSITIVITY: f32 = 0.001;
4 const MOVE_SPEED: f32 = 0.5;
5
6 pub(crate) struct World {
7 x_width: u32,
 
8 z_depth: u32,
9 camera: Camera,
10+ heights: Vec<u32>,
11 }
12
13 impl World {
@@ -21,11 +21,20 @@
21 pitch: 0.0,
22 };
23
24+ let mut rng = RandomNumberGenerator::with_seed(42);
25+ let xz_area = (x_width * z_depth) as usize;
26+
27+ let mut heights = Vec::with_capacity(xz_area);
28+
29+ for _ in 0..xz_area {
30+ heights.push(rng.gen_range(1, y_height));
31+ }
32+
33 Self {
34 x_width,
 
35 z_depth,
36 camera,
37+ heights,
38 }
39 }
40
@@ -34,17 +43,13 @@
43 self.camera.position = self.camera.position + actual_velocity;
44 }
45
46+ pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> + '_ {
47+ (0..self.z_depth).flat_map(move |z| {
48+ (0..self.x_width).flat_map(move |x| {
49+ let index = ((self.x_width * z) + x) as usize;
50+ let y_max = self.heights[index];
 
 
51
52+ (0..y_max).map(move |y| Vec3(x as f32, y as f32, z as f32))
 
 
53 })
54 })
55 }