Add height to the world
Description
So far we've been rendering a flat world on the X-Z plane, but we'll want to have elevation to make the world look more interesting.
This change adds a configurable height to the world. Right now we're creating a 100x100x100 cube, but we'll change that in upcoming commits.
Screenshot
Commands
git clone git@github.com:atsheehan/iridium
cd iridium
git checkout b37a8cec7feb7d807bee9f80e2348b31d8d10e76
cargo run --release
Code Changes
Modified src/main.rsGitHub
@@ -24,7 +24,7 @@2424 let event_loop = EventLoop::new().unwrap();
2525 let mut renderer = Renderer::new(&event_loop, options.windowed);
2626
27- let mut world = World::new(1000, 1000);
27+ let mut world = World::new(100, 100, 100);
2828 renderer.update_block_cache(world.block_positions());
2929
3030 let mut last_instant = Instant::now();
@@ -24,7 +24,7 @@24 let event_loop = EventLoop::new().unwrap();
25 let mut renderer = Renderer::new(&event_loop, options.windowed);
26
27- let mut world = World::new(1000, 1000);
28 renderer.update_block_cache(world.block_positions());
29
30 let mut last_instant = Instant::now();
@@ -24,7 +24,7 @@24 let event_loop = EventLoop::new().unwrap();
25 let mut renderer = Renderer::new(&event_loop, options.windowed);
26
27+ let mut world = World::new(100, 100, 100);
28 renderer.update_block_cache(world.block_positions());
29
30 let mut last_instant = Instant::now();
Modified src/world.rsGitHub
@@ -5,14 +5,17 @@55
66 pub(crate) struct World {
77 x_width: u32,
8+ y_height: u32,
89 z_depth: u32,
910 camera: Camera,
1011 }
1112
1213 impl World {
13- pub(crate) fn new(x_width: u32, z_depth: u32) -> Self {
14+ pub(crate) fn new(x_width: u32, y_height: u32, z_depth: u32) -> Self {
15+ let starting_position = Vec3((x_width / 2) as f32, (y_height + 1) as f32, 0.0);
16+
1417 let camera = Camera {
15- position: Vec3(0.0, 0.0, 0.0),
18+ position: starting_position,
1619 velocity: Vec3(0.0, 0.0, 0.0),
1720 heading: 0.0,
1821 pitch: 0.0,
@@ -20,6 +23,7 @@2023
2124 Self {
2225 x_width,
26+ y_height,
2327 z_depth,
2428 camera,
2529 }
@@ -33,11 +37,16 @@3337 pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> {
3438 let x_start = 0;
3539 let x_end = self.x_width;
40+ let y_start = 0;
41+ let y_end = self.y_height;
3642 let z_start = 0;
3743 let z_end = self.z_depth;
3844
39- (x_start..x_end)
40- .flat_map(move |x| (z_start..z_end).map(move |z| Vec3(x as f32, -2.0, z as f32)))
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+ })
4150 }
4251
4352 pub(crate) fn start_moving_forward(&mut self) {
@@ -5,14 +5,17 @@5
6 pub(crate) struct World {
7 x_width: u32,
8 z_depth: u32,
9 camera: Camera,
10 }
11
12 impl World {
13- pub(crate) fn new(x_width: u32, z_depth: u32) -> Self {
14 let camera = Camera {
15- position: Vec3(0.0, 0.0, 0.0),
16 velocity: Vec3(0.0, 0.0, 0.0),
17 heading: 0.0,
18 pitch: 0.0,
@@ -20,6 +23,7 @@20
21 Self {
22 x_width,
23 z_depth,
24 camera,
25 }
@@ -33,11 +37,16 @@33 pub(crate) fn block_positions(&self) -> impl Iterator<Item = Vec3> {
34 let x_start = 0;
35 let x_end = self.x_width;
36 let z_start = 0;
37 let z_end = self.z_depth;
38
39- (x_start..x_end)
40- .flat_map(move |x| (z_start..z_end).map(move |z| Vec3(x as f32, -2.0, z as f32)))
41 }
42
43 pub(crate) fn start_moving_forward(&mut self) {
@@ -5,14 +5,17 @@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 {
14+ pub(crate) fn new(x_width: u32, y_height: u32, z_depth: u32) -> Self {
15+ let starting_position = Vec3((x_width / 2) as f32, (y_height + 1) as f32, 0.0);
16+
17 let camera = Camera {
18+ position: starting_position,
19 velocity: Vec3(0.0, 0.0, 0.0),
20 heading: 0.0,
21 pitch: 0.0,
@@ -20,6 +23,7 @@23
24 Self {
25 x_width,
26+ y_height,
27 z_depth,
28 camera,
29 }
@@ -33,11 +37,16 @@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 }
51
52 pub(crate) fn start_moving_forward(&mut self) {