Compute angles for raycaster

This commit is contained in:
Fabien Freling 2017-10-16 22:44:58 +02:00
parent 3106b01f3f
commit 343ccdd12a

View file

@ -14,7 +14,7 @@ struct Position {
struct Player { struct Player {
pos: Position, pos: Position,
angle: f64, angle: f64, // radian or degree
} }
@ -33,10 +33,10 @@ fn main() {
Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall,
]; ];
let mut player: Player; let mut player = Player { pos: Position { x: 2.0, y: 2.0}, angle: 0.0};
player.pos.x = 2.0;
player.pos.y = 2.0; let horiz_fov = 90 as f64;
player.angle = 0.0; //let height_unit = 64;
while let Some(event) = window.next() { while let Some(event) = window.next() {
let window_size = window.size(); let window_size = window.size();
@ -64,6 +64,20 @@ fn main() {
[0.0, h / 2.0, w, h / 2.0], [0.0, h / 2.0, w, h / 2.0],
context.transform, context.transform,
graphics); graphics);
// Walls
let left = player.angle + (horiz_fov / 2.0);
let right = player.angle - (horiz_fov / 2.0);
// for every angle (range / w)
let step = (left - right) / w;
let mut ray_angle = left;
for n in 0..(window_size.width - 1) {
// cast a ray
// see what wall it hits
// compute wall height
// draw wall portion
ray_angle += step;
}
}); });
} }
} }