assert on negative distance

This commit is contained in:
Fabien Freling 2020-04-06 23:35:51 +02:00
parent 5571266cfe
commit eee63a7350

View file

@ -31,9 +31,14 @@ struct Player {
impl Player { impl Player {
pub fn player_space_distance(&self, other: Position) ->f64 { pub fn player_space_distance(&self, other: Position) ->f64 {
let rad = self.angle.to_radians(); let rad = self.angle.to_radians();
let x = (other.x - self.pos.x) * rad.cos(); let dx = other.x - self.pos.x;
let y = (other.y - self.pos.y) * rad.sin(); let dy = other.y - self.pos.y;
x + y let x = dx * rad.cos();
let y = dy * rad.sin();
let distance = x + y;
println!("angle: {}, dx: {}, dy: {}, x; {}, y: {}", self.angle, dx, dy, x, y);
assert!(distance.is_sign_positive());
distance
} }
} }
@ -73,8 +78,8 @@ fn step_for_angle(angle: Radian) -> (f64, f64) {
match angle { match angle {
x if x == 0.0 => (0.0, std::f64::INFINITY), x if x == 0.0 => (0.0, std::f64::INFINITY),
x if x == PI * 0.5 => (std::f64::INFINITY, 0.0), x if x == PI * 0.5 => (std::f64::INFINITY, 0.0),
x if x == PI => (0.0, std::f64::NEG_INFINITY), x if x == PI => (0.0, std::f64::INFINITY),
x if x == PI * 1.5 => (std::f64::NEG_INFINITY, 0.0), x if x == PI * 1.5 => (std::f64::INFINITY, 0.0),
x if (0.0..(PI * 0.5)).contains(&x) => (angle.tan(), ((PI * 0.5) - angle).tan()), x if (0.0..(PI * 0.5)).contains(&x) => (angle.tan(), ((PI * 0.5) - angle).tan()),
x if ((PI * 0.5)..PI).contains(&x) => (-x.tan(), -((x - (PI * 0.5)).tan())), x if ((PI * 0.5)..PI).contains(&x) => (-x.tan(), -((x - (PI * 0.5)).tan())),
x if (PI..(PI * 1.5)).contains(&x) => (-x.tan(), -(((PI * 1.5) - x).tan())), x if (PI..(PI * 1.5)).contains(&x) => (-x.tan(), -(((PI * 1.5) - x).tan())),
@ -197,7 +202,6 @@ impl Engine {
let ray_radian = ray_angle.to_radians(); let ray_radian = ray_angle.to_radians();
let (tile, pos) = closest_point(&self.level, &self.player.pos, ray_radian); let (tile, pos) = closest_point(&self.level, &self.player.pos, ray_radian);
let distance = self.player.player_space_distance(pos); let distance = self.player.player_space_distance(pos);
//assert!(distance.is_sign_positive());
if tile == Tile::Wall { if tile == Tile::Wall {
let wall_height = (self.h / (distance * 3.0)).min(self.h); let wall_height = (self.h / (distance * 3.0)).min(self.h);
let wall_color = match pos { let wall_color = match pos {
@ -280,6 +284,9 @@ mod tests {
fcmp(player.player_space_distance(super::Position { x: 2., y: 4. }), 2.); fcmp(player.player_space_distance(super::Position { x: 2., y: 4. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 3., y: 4. }), 2.); fcmp(player.player_space_distance(super::Position { x: 3., y: 4. }), 2.);
player.angle = 135.;
fcmp(player.player_space_distance(super::Position { x: 0., y: 2. }), 2.);
player.angle = 180.; player.angle = 180.;
fcmp(player.player_space_distance(super::Position { x: 0., y: 1. }), 2.); fcmp(player.player_space_distance(super::Position { x: 0., y: 1. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 0., y: 2. }), 2.); fcmp(player.player_space_distance(super::Position { x: 0., y: 2. }), 2.);
@ -317,6 +324,7 @@ mod tests {
fcmp(step_x, x); fcmp(step_x, x);
}; };
step_cmp(30.0, 0.5773502691896257, 1.7320508075688776);
step_cmp(45.0, 1.0, 1.0); step_cmp(45.0, 1.0, 1.0);
step_cmp(90.0 + 45.0, 1.0, -1.0); step_cmp(90.0 + 45.0, 1.0, -1.0);
step_cmp(180.0 + 45.0, -1.0, -1.0); step_cmp(180.0 + 45.0, -1.0, -1.0);
@ -362,11 +370,11 @@ mod tests {
for n in 0..20 { for n in 0..20 {
let left_ray_angle = (((n as f64) * step) + 360.0) % 360.0; let left_ray_angle = (((n as f64) * step) + 360.0) % 360.0;
let left_ray_radian = left_ray_angle.to_radians(); let left_ray_radian = left_ray_angle.to_radians();
let (left_tile, left_pos) = super::closest_point(&level, &position, left_ray_radian); let (_left_tile, left_pos) = super::closest_point(&level, &position, left_ray_radian);
let right_ray_angle = ((-(n as f64) * step) + 360.0) % 360.0; let right_ray_angle = ((-(n as f64) * step) + 360.0) % 360.0;
let right_ray_radian = right_ray_angle.to_radians(); let right_ray_radian = right_ray_angle.to_radians();
let (right_tile, right_pos) = super::closest_point(&level, &position, right_ray_radian); let (_right_tile, right_pos) = super::closest_point(&level, &position, right_ray_radian);
println!("left: {:?}, angle: {}", left_pos, left_ray_angle); println!("left: {:?}, angle: {}", left_pos, left_ray_angle);
println!("right: {:?}, angle: {}", right_pos, right_ray_angle); println!("right: {:?}, angle: {}", right_pos, right_ray_angle);