From 81aef1eab0d270135903b09abfb0f7ddec92c367 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 28 Apr 2022 23:42:15 +0200 Subject: [PATCH] resolve floating point exception when wliding through walls --- headers/cube3d_proto.h | 2 ++ maps/map_valid_14.cub | 27 +++++++++++++++++++++++++++ srcs/cube3d.c | 5 +++++ srcs/draw/raycast.c | 4 ---- srcs/hook/keyhook.c | 6 ++++-- srcs/player/player_moves.c | 28 ++++++++++++++++++++++++++++ srcs/player/player_rotates.c | 15 +++++++++++++++ 7 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 maps/map_valid_14.cub diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index ea12ba9..2ac7f01 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -1,6 +1,8 @@ #ifndef CUBE3D_PROTO_H # define CUBE3D_PROTO_H +void plr_turn(t_plr *plr, int deg); + // ------------------------------- // SRC // ------------------------------- diff --git a/maps/map_valid_14.cub b/maps/map_valid_14.cub new file mode 100644 index 0000000..b3cb4e8 --- /dev/null +++ b/maps/map_valid_14.cub @@ -0,0 +1,27 @@ +NO textures/coin.xpm +SO textures/coin.xpm +EA textures/coin.xpm +WE textures/coin.xpm + +F 220 , 100, 30 + +C 225 , 30 , 0 + +1111111111111111111111111111 +1N00000000000000011100000001 +1111000000000000100000110001 +1111001000000000000110000111 +1000000000000000010101000001 +1111110000000000000000001111 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1000000000000000000000000001 +1111111111111111111111111111 diff --git a/srcs/cube3d.c b/srcs/cube3d.c index 18c47b2..af82b9a 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -42,6 +42,11 @@ int main(int ac, char **av) mb_init(destroy_mlx, game); // draw game a first time before it start + + // tmp + plr_turn(&game->plr, -63); + // tmp end + draw(game); // receive a keypress event diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 510cd3e..48a8d3e 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -36,13 +36,11 @@ static void wall_length(t_rcast *rcast) rcast->wall.start.x = rcast->ray_nb; rcast->wall.end.x = rcast->ray_nb; -// len = sqrt(pow(ray->end.x - ray->start.x, 2) + pow(ray->end.y - ray->start.y, 2)); if (rcast->is_x == 1) { len = rcast->next_x - rcast->ray_step_x; if (rcast->slope_y) len /= ft_abs(rcast->slope_y); - // len = (double)len * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; len = rcast->screen_dist * len / (rcast->slope_x); } else if (rcast->is_x == 0) @@ -50,7 +48,6 @@ static void wall_length(t_rcast *rcast) len = rcast->next_y - rcast->ray_step_y; if (rcast->slope_x) len /= ft_abs(rcast->slope_x); - // len = (double)len * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; len = rcast->screen_dist * len / (rcast->slope_y); } rcast->ray_len = ft_abs(len); @@ -110,7 +107,6 @@ void raycast(t_game *game, t_rcast *rcast) draw_line(&game->map_img, &ray, 0x00FF00FF); // tmp end - wall_length(rcast); wall_height(rcast); draw_column(game, rcast); diff --git a/srcs/hook/keyhook.c b/srcs/hook/keyhook.c index 8641fd2..a4cad21 100644 --- a/srcs/hook/keyhook.c +++ b/srcs/hook/keyhook.c @@ -1,9 +1,11 @@ #include "cube3d.h" // temp, to map all the keys on linux and mac - static int print_keycode(int keycode) + static int print_keycode(int keycode, t_plr *plr) { ft_putnbr_fd(keycode, 1); + ft_putchar_fd(' ', 1); + ft_putnbr_fd(plr->rot, 1); ft_putchar_fd('\n', 1); return(0); } @@ -38,7 +40,7 @@ int keypress(int keycode, t_game *game) unsigned i; // temp - print_keycode(keycode); + print_keycode(keycode, &game->plr); // temp end i = 0; diff --git a/srcs/player/player_moves.c b/srcs/player/player_moves.c index f436e40..1a62c4c 100644 --- a/srcs/player/player_moves.c +++ b/srcs/player/player_moves.c @@ -5,16 +5,23 @@ void plr_posx_decrement(t_game *game, t_plr *plr) t_d_coord pos; int limit_x; int limit_y; + int limit_xy; pos.x = plr->exact.x - PLR_MV; pos.y = plr->exact.y; rotate_double(plr, &pos); limit_x = plr_out_limits(game, pos.x, plr->pos.y); limit_y = plr_out_limits(game, plr->pos.x, pos.y); + limit_xy = plr_out_limits(game, pos.x, pos.y); if (limit_x) pos.x = plr->exact.x; if (limit_y) pos.y = plr->exact.y; + if (limit_xy && !limit_y && !limit_x) + { + pos.x = plr->exact.x; + pos.y = plr->exact.y; + } plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); @@ -26,16 +33,23 @@ void plr_posy_decrement(t_game *game, t_plr *plr) t_d_coord pos; int limit_x; int limit_y; + int limit_xy; pos.x = plr->exact.x; pos.y = plr->exact.y - PLR_MV; rotate_double(plr, &(pos)); limit_x = plr_out_limits(game, pos.x, plr->pos.y); limit_y = plr_out_limits(game, plr->pos.x, pos.y); + limit_xy = plr_out_limits(game, pos.x, pos.y); if (limit_x) pos.x = plr->exact.x; if (limit_y) pos.y = plr->exact.y; + if (limit_xy && !limit_y && !limit_x) + { + pos.x = plr->exact.x; + pos.y = plr->exact.y; + } plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); @@ -47,16 +61,23 @@ void plr_posx_increment(t_game *game, t_plr *plr) t_d_coord pos; int limit_x; int limit_y; + int limit_xy; pos.x = plr->exact.x + PLR_MV; pos.y = plr->exact.y; rotate_double(plr, &(pos)); limit_x = plr_out_limits(game, pos.x, plr->pos.y); limit_y = plr_out_limits(game, plr->pos.x, pos.y); + limit_xy = plr_out_limits(game, pos.x, pos.y); if (limit_x) pos.x = plr->exact.x; if (limit_y) pos.y = plr->exact.y; + if (limit_xy && !limit_y && !limit_x) + { + pos.x = plr->exact.x; + pos.y = plr->exact.y; + } plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); @@ -68,16 +89,23 @@ void plr_posy_increment(t_game *game, t_plr *plr) t_d_coord pos; int limit_x; int limit_y; + int limit_xy; pos.x = plr->exact.x; pos.y = plr->exact.y + PLR_MV; rotate_double(plr, &(pos)); limit_x = plr_out_limits(game, pos.x, plr->pos.y); limit_y = plr_out_limits(game, plr->pos.x, pos.y); + limit_xy = plr_out_limits(game, pos.x, pos.y); if (limit_x) pos.x = plr->exact.x; if (limit_y) pos.y = plr->exact.y; + if (limit_xy && !limit_y && !limit_x) + { + pos.x = plr->exact.x; + pos.y = plr->exact.y; + } plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); diff --git a/srcs/player/player_rotates.c b/srcs/player/player_rotates.c index a0f3f55..0a31c75 100644 --- a/srcs/player/player_rotates.c +++ b/srcs/player/player_rotates.c @@ -73,3 +73,18 @@ void plr_turn_right(t_plr *plr) plr->cosj = cos(radj); plr->sinj = sin(radj); } + +void plr_turn(t_plr *plr, int deg) +{ + double radi; + double radj; + + plr->rot = deg; + // calculate trigo for rotations + radi = plr->rot * M_PI / 180; + radj = radi + (M_PI / 2); + plr->cosi = cos(radi); + plr->sini = sin(radi); + plr->cosj = cos(radj); + plr->sinj = sin(radj); +}