-
Notifications
You must be signed in to change notification settings - Fork 0
/
raycaster.rs
347 lines (302 loc) · 11.3 KB
/
raycaster.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::{line, set_pixel, vector::Vector, verline, ACCELERATION, HEIGHT, WIDTH};
pub struct RayCaster {
player: Player,
map: Vec<Vec<MapCell>>,
fov: f64,
}
struct Ray {
dir: Vector<f64>,
hit: bool,
}
struct Player {
pub pos: Vector<f64>,
pub dir: Vector<f64>,
pub vel: Vector<f64>,
}
pub enum Direction {
Up,
Down,
Left,
Right,
Mouse(f64, f64),
}
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
struct MapCell {
pub color: [u8; 4],
pub solid: MapCellType,
pub height: f64,
}
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
enum MapCellType {
Empty,
Wall,
}
impl MapCell {
pub fn new(color: [u8; 4], solid: MapCellType, height: f64) -> Self {
Self {
color,
solid,
height,
}
}
pub fn empty() -> Self {
Self {
color: [0, 0, 0, 0],
solid: MapCellType::Empty,
height: 0.0,
}
}
}
impl RayCaster {
pub fn new(fov: f64) -> Self {
Self {
player: Player {
pos: Vector { x: 22.0, y: 12.0 },
dir: Vector { x: -1.0, y: 0.0 },
vel: Vector { x: 0., y: 0. },
},
map: generate_map(),
// map: [
// [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
// [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1],
// [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
// [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
// ],
fov,
}
}
pub fn draw(&self, frame: &mut [u8], map_toggle: bool) -> Result<(), String> {
if map_toggle {
// map
for y in 0..self.map.len() {
for x in 0..self.map[y].len() {
let cell = self.map[y][x];
set_pixel(frame, x, y, cell.color, 1);
// filled_rectangle(frame, x, y, x+1, y+2, color, PIXELSIZE)
}
}
set_pixel(
frame,
self.player.pos.x as usize,
self.player.pos.y as usize,
[25, 0, 255, 255],
1,
);
line(
frame,
self.player.pos.x as isize,
self.player.pos.y as isize,
(self.player.pos.x + self.player.dir.x * 10.) as isize,
(self.player.pos.y + self.player.dir.y * 10.) as isize,
[255, 0, 0, 255],
1,
);
// orthogonal line
line(
frame,
self.player.pos.x as isize,
self.player.pos.y as isize,
(self.player.pos.x + self.player.dir.orthogonal(Direction::Left).x * 10.) as isize,
(self.player.pos.y + self.player.dir.orthogonal(Direction::Left).y * 10.) as isize,
[0, 255, 0, 255],
1,
);
line(
frame,
self.player.pos.x as isize,
self.player.pos.y as isize,
(self.player.pos.x + self.player.dir.orthogonal(Direction::Right).x * 10.) as isize,
(self.player.pos.y + self.player.dir.orthogonal(Direction::Right).y * 10.) as isize,
[0, 0, 255, 255],
1,
);
return Ok(());
}
// raycasting
let half_fov: f64 = self.fov / 2.;
const NUMRAYS: f64 = WIDTH as f64;
for i in 0..NUMRAYS as usize {
let angle = (self.fov / NUMRAYS * i as f64 - half_fov) * 1f64.to_radians();
let mut ray = Ray {
dir: self.player.dir.rotate(angle),
hit: false,
};
// map_pos is the current map cell we are in
let mut map_pos: Vector<i32> = Vector::new(
self.player.pos.x.floor() as i32,
self.player.pos.y.floor() as i32,
);
// delta of ray to next map cell
let delta_dist = Vector {
x: (1.0 / ray.dir.x).abs(),
y: (1.0 / ray.dir.y).abs(),
};
// step direction for map_pos
let step = Vector {
x: if ray.dir.x < 0. { -1. } else { 1. },
y: if ray.dir.y < 0. { -1. } else { 1. },
};
// ray distance from side of map cell (helps with determining direction to inc)
let mut side_dist: Vector<f64> = Vector {
x: if ray.dir.x < 0. {
// top left edge of map cell
(self.player.pos.x - map_pos.x as f64) * delta_dist.x
} else {
// top right edge of map cell
(map_pos.x as f64 + 1. - self.player.pos.x) * delta_dist.x
},
y: if ray.dir.y < 0. {
// top left edge of map cell
(self.player.pos.y - map_pos.y as f64) * delta_dist.y
} else {
// top right edge of map cell
(map_pos.y as f64 + 1. - self.player.pos.y) * delta_dist.y
},
};
// DDA
let mut side = 0;
while !ray.hit {
if side_dist.x < side_dist.y {
side_dist.x += delta_dist.x;
map_pos.x += step.x as i32;
side = 0;
} else {
side_dist.y += delta_dist.y;
map_pos.y += step.y as i32;
side = 1;
}
if self.map[map_pos.y as usize][map_pos.x as usize].solid != MapCellType::Empty {
ray.hit = true;
}
}
let mut cell = self.map[map_pos.y as usize][map_pos.x as usize];
if side == 1 {
cell.color.div_assign(2)
}
let distance: f64 = if side == 0 {
(map_pos.x as f64 - self.player.pos.x + (1. - step.x) / 2.) / ray.dir.x
} else {
(map_pos.y as f64 - self.player.pos.y + (1. - step.y) / 2.) / ray.dir.y
};
let correct_distance = distance * (self.player.dir.angle() - ray.dir.angle()).cos();
// fog
cell.color.mul_assign(1. / (1. + correct_distance * correct_distance * 0.0001));
let mut height = (HEIGHT as f64 / correct_distance).abs() * 15.;
if height > HEIGHT as f64 {
height = HEIGHT as f64;
}
let column_start = HEIGHT as usize / 2 - height as usize / 2;
let column_end = HEIGHT as usize / 2 + height as usize / 2;
verline(frame, i, column_start, column_end, cell.color, 1);
}
Ok(())
}
pub fn update_player(&mut self) {
let new_pos_x = Vector::new(self.player.pos.x + self.player.vel.x, self.player.pos.y);
if self.is_valid_position(&new_pos_x) {
self.player.pos = new_pos_x;
}
let new_pos_y = Vector::new(self.player.pos.x, self.player.pos.y + self.player.vel.y);
if self.is_valid_position(&new_pos_y) {
self.player.pos = new_pos_y;
}
self.player.vel *= 0.8;
}
fn is_valid_position(&self, pos: &Vector<f64>) -> bool {
if let Some(row) = self.map.get(pos.y as usize) {
if let Some(cell) = row.get(pos.x as usize) {
if cell.solid == MapCellType::Empty {
return true;
}
}
}
false
}
pub fn change_direction(&mut self, dir: Direction) {
const ROTATESPEED: f64 = 0.001;
let acceleration = unsafe { ACCELERATION };
match dir {
Direction::Down => {
self.player.vel.x -= self.player.dir.x * acceleration;
self.player.vel.y -= self.player.dir.y * acceleration;
}
Direction::Up => {
self.player.vel.x += self.player.dir.x * acceleration;
self.player.vel.y += self.player.dir.y * acceleration;
}
Direction::Left => {
let ortho = self.player.dir.orthogonal(Direction::Left);
self.player.vel.x -= ortho.x * acceleration;
self.player.vel.y -= ortho.y * acceleration;
}
Direction::Right => {
let ortho = self.player.dir.orthogonal(Direction::Right);
self.player.vel.x -= ortho.x * acceleration;
self.player.vel.y -= ortho.y * acceleration;
}
Direction::Mouse(dx, _) => {
self.player.dir = self.player.dir.rotate(dx * ROTATESPEED);
}
}
}
}
trait MulAssign {
fn mul_assign(&mut self, rhs: f64);
}
impl MulAssign for [u8; 4] {
fn mul_assign(&mut self, rhs: f64) {
self[0] = (self[0] as f64 * rhs) as u8;
self[1] = (self[1] as f64 * rhs) as u8;
self[2] = (self[2] as f64 * rhs) as u8;
self[3] = (self[3] as f64 * rhs) as u8;
}
}
trait DivAssign {
fn div_assign(&mut self, rhs: u8);
}
impl DivAssign for [u8; 4] {
fn div_assign(&mut self, rhs: u8) {
self[0] /= rhs;
self[1] /= rhs;
self[2] /= rhs;
self[3] /= rhs;
}
}
fn generate_map() -> Vec<Vec<MapCell>> {
let img = image::open("assets/map.png").unwrap();
let img = img.to_rgba8();
let (width, height) = img.dimensions();
let mut buffer: Vec<Vec<MapCell>> = vec![vec![MapCell::empty(); width as usize]; height as usize];
for y in 0..height {
for x in 0..width {
let pixel = img.get_pixel(x, y).0;
let solid = if pixel == [0, 0, 0, 0] {
MapCellType::Empty
} else {
MapCellType::Wall
};
buffer[y as usize][x as usize] = MapCell::new(pixel, solid, 0.);
}
}
buffer
}