From a0f32dee98697307b06fa62abc1e205c40d421f0 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Wed, 27 Apr 2022 00:11:55 +0200 Subject: [PATCH 1/6] plr moves through walls --- README.md | 7 +++++++ headers/cube3d_proto.h | 2 +- srcs/player/player_moves.c | 40 ++++++++++++++++++++++++++++++-------- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index db39df2..59dfb25 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +# todo + +- resolve view buble +- add slide against wall +- resolve vertical ray +- add textures + # ressources - [tuto mlx](https://harm-smits.github.io/42docs/libs/minilibx/getting_started.html) diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index a699fc0..743e4c7 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -55,8 +55,8 @@ void rotate_double(t_plr *plr, t_d_coord *coord); void plr_turn_right(t_plr *plr); void plr_turn_left(t_plr *plr); // player_limits.c -int plr_out_limits(t_game *game, int x, int y); int is_wall(t_game *game, int cell_x, int cell_y); +int plr_out_limits(t_game *game, int x, int y); // ------------------------------- // SRC/DRAW diff --git a/srcs/player/player_moves.c b/srcs/player/player_moves.c index 1bf43d7..f436e40 100644 --- a/srcs/player/player_moves.c +++ b/srcs/player/player_moves.c @@ -3,12 +3,18 @@ void plr_posx_decrement(t_game *game, t_plr *plr) { t_d_coord pos; + int limit_x; + int limit_y; pos.x = plr->exact.x - PLR_MV; pos.y = plr->exact.y; rotate_double(plr, &pos); - if (plr_out_limits(game, pos.x, pos.y)) - return ; + limit_x = plr_out_limits(game, pos.x, plr->pos.y); + limit_y = plr_out_limits(game, plr->pos.x, pos.y); + if (limit_x) + pos.x = plr->exact.x; + if (limit_y) + pos.y = plr->exact.y; plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); @@ -18,12 +24,18 @@ void plr_posx_decrement(t_game *game, t_plr *plr) void plr_posy_decrement(t_game *game, t_plr *plr) { t_d_coord pos; + int limit_x; + int limit_y; pos.x = plr->exact.x; pos.y = plr->exact.y - PLR_MV; rotate_double(plr, &(pos)); - if (plr_out_limits(game, pos.x, pos.y)) - return ; + limit_x = plr_out_limits(game, pos.x, plr->pos.y); + limit_y = plr_out_limits(game, plr->pos.x, pos.y); + if (limit_x) + pos.x = plr->exact.x; + if (limit_y) + pos.y = plr->exact.y; plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); @@ -33,12 +45,18 @@ void plr_posy_decrement(t_game *game, t_plr *plr) void plr_posx_increment(t_game *game, t_plr *plr) { t_d_coord pos; + int limit_x; + int limit_y; pos.x = plr->exact.x + PLR_MV; pos.y = plr->exact.y; rotate_double(plr, &(pos)); - if (plr_out_limits(game, pos.x, pos.y)) - return ; + limit_x = plr_out_limits(game, pos.x, plr->pos.y); + limit_y = plr_out_limits(game, plr->pos.x, pos.y); + if (limit_x) + pos.x = plr->exact.x; + if (limit_y) + pos.y = plr->exact.y; plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); @@ -48,12 +66,18 @@ void plr_posx_increment(t_game *game, t_plr *plr) void plr_posy_increment(t_game *game, t_plr *plr) { t_d_coord pos; + int limit_x; + int limit_y; pos.x = plr->exact.x; pos.y = plr->exact.y + PLR_MV; rotate_double(plr, &(pos)); - if (plr_out_limits(game, pos.x, pos.y)) - return ; + limit_x = plr_out_limits(game, pos.x, plr->pos.y); + limit_y = plr_out_limits(game, plr->pos.x, pos.y); + if (limit_x) + pos.x = plr->exact.x; + if (limit_y) + pos.y = plr->exact.y; plr->exact.x = pos.x; plr->exact.y = pos.y; (plr->pos.x) = (int)(pos.x); From 3501a20abbed3ac333e29ebf76f81266bab940c2 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 28 Apr 2022 00:44:02 +0200 Subject: [PATCH 2/6] wip debug ray length --- README.md | 2 +- headers/cube3d_struct.h | 2 ++ srcs/draw/ray_intersect.c | 69 ++++++++++++++++++++++++++++++++------- srcs/draw/raycast.c | 48 +++++++++++++++++++++++---- 4 files changed, 102 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 59dfb25..5fd0b58 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # todo +- resolve wall height - resolve view buble -- add slide against wall - resolve vertical ray - add textures diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 012c6d6..8f81dad 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -32,6 +32,7 @@ typedef struct s_vec typedef struct s_rcast { + int tmp; t_vec screen_dist; t_vec screen_size; t_vec ray; @@ -56,6 +57,7 @@ typedef struct s_rcast int ray_step_y; int is_x; int ray_nb; + int ray_len; double ratio; } t_rcast; diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index 163160e..c491c88 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -22,37 +22,67 @@ static void init_raycast(t_rcast *rcast, t_vec *ray) // ray steps in both axis rcast->ray_step_x = ft_abs(rcast->cell * rcast->slope_y); rcast->ray_step_y = ft_abs(rcast->cell * rcast->slope_x); + + // tmp + if (rcast->tmp) + printf(" [slope_x:%i, slope_y:%i]", rcast->slope_x, rcast->slope_y); + //tmp + } static void init_first_step(t_rcast *rcast, t_vec *ray) { - rcast->first_next_x = ray->start.x % rcast->cell; - if (rcast->first_next_x && rcast->ray_sign_x < 0) - rcast->first_next_x = rcast->cell - rcast->first_next_x; - if (!rcast->first_next_x && rcast->ray_sign_x < 0) - rcast->first_next_x = rcast->cell; - rcast->first_next_y = ray->start.y % rcast->cell; - if (rcast->first_next_y && rcast->ray_sign_y < 0) - rcast->first_next_y = rcast->cell - rcast->first_next_y; - if (!rcast->first_next_y && rcast->ray_sign_y < 0) - rcast->first_next_y = rcast->cell; + rcast->first_next_x = 0; + if (rcast->slope_x) + rcast->first_next_x = ray->start.x % rcast->cell; + if (rcast->ray_sign_x < 0) + rcast->first_next_x -= rcast->cell; + rcast->first_next_y = 0; + if (rcast->slope_y) + rcast->first_next_y = ray->start.y % rcast->cell; + if (rcast->ray_sign_y < 0) + rcast->first_next_y -= rcast->cell; rcast->next_x = ft_abs(rcast->first_next_x * rcast->slope_y); rcast->next_y = ft_abs(rcast->first_next_y * rcast->slope_x); + + // tmp + if (rcast->tmp) + printf(" [next_x:%i, next_y:%i]", rcast->next_x, rcast->next_y); + //tmp + } static void next_cell(t_rcast *rcast) { - if (rcast->next_x > rcast->next_y) + + // tmp + if (rcast->tmp) + printf("\n [next_x(%i) > next_y(%i) ?]", rcast->next_x, rcast->next_y); + //tmp + + if (rcast->slope_x == 0 || rcast->next_x > rcast->next_y) { rcast->cell_y += rcast->next_cell_y; rcast->next_y += rcast->ray_step_y; rcast->is_x = 0; + + // tmp + if (rcast->tmp) + printf(" [yes] (increase y by %i)", rcast->ray_step_y); + //tmp + } else { rcast->cell_x += rcast->next_cell_x; rcast->next_x += rcast->ray_step_x; rcast->is_x = 1; + + // tmp + if (rcast->tmp) + printf(" [no] (increase x by %i)", rcast->ray_step_x); + //tmp + } } @@ -64,8 +94,25 @@ void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray) ray->end.x += rcast->ray_nb; ray->end.y = rcast->ray.end.y + game->plr.pos.y; rotate(&(game->plr), &(ray->end)); + + // tmp + rcast->tmp = 0; + if (ray->end.x - ray->start.x == 0) + rcast->tmp = 1; + if (ray->end.y - ray->start.y == 0) + rcast->tmp = 1; + if (rcast->tmp) + printf("[ray nb : %3i]", rcast->ray_nb); + //tmp end + init_raycast(rcast, ray); init_first_step(rcast, ray); while (!is_wall(game, rcast->cell_x, rcast->cell_y)) next_cell(rcast); + + // tmp + if (rcast->tmp) + printf("\n"); + //tmp end + } diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index c0d0ea1..0b6b39b 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -30,10 +30,14 @@ } // tmp end -static void calcul_wall(t_rcast *rcast) +static void wall_length(t_rcast *rcast) { int length; - int height; + + // tmp, to draw the map + if (rcast->tmp) + printf("length:"); + // tmp end rcast->wall.start.x = rcast->ray_nb; rcast->wall.end.x = rcast->ray_nb; @@ -41,16 +45,38 @@ static void calcul_wall(t_rcast *rcast) { length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; -// length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); + + // tmp, to draw the map + if (rcast->tmp) + printf("(is_x == 1, slope_y != 0)"); + // tmp end + } if (rcast->is_x == 0 && rcast->slope_x != 0) { length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; -// length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + + // tmp, to draw the map + if (rcast->tmp) + printf("(is_x == 0, slope_x != 0)"); + // tmp end + } - length = ft_abs(length); - height = rcast->wall_height - length; + rcast->ray_len = ft_abs(length); + + // tmp, to draw the map + if (rcast->tmp) + printf(" %i", rcast->ray_len); + // tmp end + +} + +static void wall_height(t_rcast *rcast) +{ + int height; + + height = rcast->wall_height - rcast->ray_len; if (height < 0) height = 0; if (height > rcast->screen_height) @@ -98,8 +124,16 @@ void raycast(t_game *game, t_rcast *rcast) draw_line(&game->map_img, &ray, 0x00FF00FF); // tmp end - calcul_wall(rcast); + + wall_length(rcast); + wall_height(rcast); draw_column(game, rcast); (rcast->ray_nb)++; + + // tmp, to draw the map + if (rcast->tmp) + printf("\n"); + // tmp end + } } From 4f0e5fcd00a398d2b8d4f684617151f492535fbf Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 28 Apr 2022 08:52:21 +0200 Subject: [PATCH 3/6] rays verticals and horizontals are working --- headers/cube3d_struct.h | 2 +- srcs/draw/ray_intersect.c | 26 ++++++++++++++++++----- srcs/draw/raycast.c | 43 ++++++++++++++++++++++++++------------- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 8f81dad..64c14be 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -32,11 +32,11 @@ typedef struct s_vec typedef struct s_rcast { - int tmp; t_vec screen_dist; t_vec screen_size; t_vec ray; t_vec wall; + int tmp; int wall_height; int screen_width; int screen_height; diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index c491c88..47a1e57 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -20,8 +20,20 @@ static void init_raycast(t_rcast *rcast, t_vec *ray) rcast->next_cell_x = -rcast->ray_sign_x; rcast->next_cell_y = -rcast->ray_sign_y; // ray steps in both axis - rcast->ray_step_x = ft_abs(rcast->cell * rcast->slope_y); - rcast->ray_step_y = ft_abs(rcast->cell * rcast->slope_x); + rcast->ray_step_x = 0; + if (rcast->slope_x) + { + rcast->ray_step_x = ft_abs(rcast->cell); + if (rcast->slope_y) + rcast->ray_step_x *= ft_abs(rcast->slope_y); + } + rcast->ray_step_y = 0; + if (rcast->slope_y) + { + rcast->ray_step_y = ft_abs(rcast->cell); + if (rcast->slope_x) + rcast->ray_step_y *= ft_abs(rcast->slope_x); + } // tmp if (rcast->tmp) @@ -42,8 +54,12 @@ static void init_first_step(t_rcast *rcast, t_vec *ray) rcast->first_next_y = ray->start.y % rcast->cell; if (rcast->ray_sign_y < 0) rcast->first_next_y -= rcast->cell; - rcast->next_x = ft_abs(rcast->first_next_x * rcast->slope_y); - rcast->next_y = ft_abs(rcast->first_next_y * rcast->slope_x); + rcast->next_x = ft_abs(rcast->first_next_x); + if (rcast->slope_y != 0) + rcast->next_x *= ft_abs(rcast->slope_y); + rcast->next_y = ft_abs(rcast->first_next_y); + if (rcast->slope_x != 0) + rcast->next_y *= ft_abs(rcast->slope_x); // tmp if (rcast->tmp) @@ -60,7 +76,7 @@ static void next_cell(t_rcast *rcast) printf("\n [next_x(%i) > next_y(%i) ?]", rcast->next_x, rcast->next_y); //tmp - if (rcast->slope_x == 0 || rcast->next_x > rcast->next_y) + if (!rcast->slope_x || (rcast->next_x > rcast->next_y && rcast->slope_y)) { rcast->cell_y += rcast->next_cell_y; rcast->next_y += rcast->ray_step_y; diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 0b6b39b..3a0c57a 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -34,40 +34,55 @@ static void wall_length(t_rcast *rcast) { int length; - // tmp, to draw the map - if (rcast->tmp) - printf("length:"); - // tmp end - rcast->wall.start.x = rcast->ray_nb; rcast->wall.end.x = rcast->ray_nb; - if (rcast->is_x == 1 && rcast->slope_y != 0) + if (rcast->slope_x == 0) { - length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); - length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; // tmp, to draw the map if (rcast->tmp) - printf("(is_x == 1, slope_y != 0)"); + printf("(slope_x == 0) "); // tmp end + length = (rcast->next_y - rcast->ray_step_y); } - if (rcast->is_x == 0 && rcast->slope_x != 0) + else if (rcast->slope_y == 0) { - length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); - length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; // tmp, to draw the map if (rcast->tmp) - printf("(is_x == 0, slope_x != 0)"); + printf("(slope_y == 0) "); // tmp end + length = (rcast->next_x - rcast->ray_step_x); + } + else if (rcast->is_x == 1) + { + + // tmp, to draw the map + if (rcast->tmp) + printf("(is_x == 1) "); + // tmp end + + length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); +// length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; + } + else if (rcast->is_x == 0) + { + + // tmp, to draw the map + if (rcast->tmp) + printf("(is_x == 0) "); + // tmp end + + length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); +// length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; } rcast->ray_len = ft_abs(length); // tmp, to draw the map if (rcast->tmp) - printf(" %i", rcast->ray_len); + printf("length:%i", rcast->ray_len); // tmp end } From 5e8afcb24590b8306c2f1ef0ad2a962c74e191d0 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 28 Apr 2022 09:03:23 +0200 Subject: [PATCH 4/6] clean of debug rays vertical and horizontal --- srcs/draw/ray_intersect.c | 88 +++++++++------------------------------ srcs/draw/raycast.c | 48 ++++----------------- 2 files changed, 27 insertions(+), 109 deletions(-) diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index 47a1e57..bae693e 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -1,45 +1,19 @@ #include "cube3d.h" -static void init_raycast(t_rcast *rcast, t_vec *ray) +static void init_ray_params(t_rcast *rcast, t_vec *ray) { - rcast->is_x = 0; - // ray sign rcast->ray_sign_x = 1; if (ray->start.x < ray->end.x) rcast->ray_sign_x = -1; rcast->ray_sign_y = 1; if (ray->start.y < ray->end.y) rcast->ray_sign_y = -1; - // cell position of ray rcast->cell_x = ray->start.x / rcast->cell; rcast->cell_y = ray->start.y / rcast->cell; - // slope of ray rcast->slope_x = ray->end.x - ray->start.x; rcast->slope_y = ray->end.y - ray->start.y; - // direction of next cell rcast->next_cell_x = -rcast->ray_sign_x; rcast->next_cell_y = -rcast->ray_sign_y; - // ray steps in both axis - rcast->ray_step_x = 0; - if (rcast->slope_x) - { - rcast->ray_step_x = ft_abs(rcast->cell); - if (rcast->slope_y) - rcast->ray_step_x *= ft_abs(rcast->slope_y); - } - rcast->ray_step_y = 0; - if (rcast->slope_y) - { - rcast->ray_step_y = ft_abs(rcast->cell); - if (rcast->slope_x) - rcast->ray_step_y *= ft_abs(rcast->slope_x); - } - - // tmp - if (rcast->tmp) - printf(" [slope_x:%i, slope_y:%i]", rcast->slope_x, rcast->slope_y); - //tmp - } static void init_first_step(t_rcast *rcast, t_vec *ray) @@ -60,45 +34,39 @@ static void init_first_step(t_rcast *rcast, t_vec *ray) rcast->next_y = ft_abs(rcast->first_next_y); if (rcast->slope_x != 0) rcast->next_y *= ft_abs(rcast->slope_x); +} - // tmp - if (rcast->tmp) - printf(" [next_x:%i, next_y:%i]", rcast->next_x, rcast->next_y); - //tmp - +static void init_steps(t_rcast *rcast) +{ + rcast->ray_step_x = 0; + if (rcast->slope_x) + { + rcast->ray_step_x = ft_abs(rcast->cell); + if (rcast->slope_y) + rcast->ray_step_x *= ft_abs(rcast->slope_y); + } + rcast->ray_step_y = 0; + if (rcast->slope_y) + { + rcast->ray_step_y = ft_abs(rcast->cell); + if (rcast->slope_x) + rcast->ray_step_y *= ft_abs(rcast->slope_x); + } } static void next_cell(t_rcast *rcast) { - - // tmp - if (rcast->tmp) - printf("\n [next_x(%i) > next_y(%i) ?]", rcast->next_x, rcast->next_y); - //tmp - if (!rcast->slope_x || (rcast->next_x > rcast->next_y && rcast->slope_y)) { rcast->cell_y += rcast->next_cell_y; rcast->next_y += rcast->ray_step_y; rcast->is_x = 0; - - // tmp - if (rcast->tmp) - printf(" [yes] (increase y by %i)", rcast->ray_step_y); - //tmp - } else { rcast->cell_x += rcast->next_cell_x; rcast->next_x += rcast->ray_step_x; rcast->is_x = 1; - - // tmp - if (rcast->tmp) - printf(" [no] (increase x by %i)", rcast->ray_step_x); - //tmp - } } @@ -110,25 +78,9 @@ void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray) ray->end.x += rcast->ray_nb; ray->end.y = rcast->ray.end.y + game->plr.pos.y; rotate(&(game->plr), &(ray->end)); - - // tmp - rcast->tmp = 0; - if (ray->end.x - ray->start.x == 0) - rcast->tmp = 1; - if (ray->end.y - ray->start.y == 0) - rcast->tmp = 1; - if (rcast->tmp) - printf("[ray nb : %3i]", rcast->ray_nb); - //tmp end - - init_raycast(rcast, ray); + init_ray_params(rcast, ray); init_first_step(rcast, ray); + init_steps(rcast); while (!is_wall(game, rcast->cell_x, rcast->cell_y)) next_cell(rcast); - - // tmp - if (rcast->tmp) - printf("\n"); - //tmp end - } diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 3a0c57a..66d69d1 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -36,55 +36,21 @@ static void wall_length(t_rcast *rcast) rcast->wall.start.x = rcast->ray_nb; rcast->wall.end.x = rcast->ray_nb; - if (rcast->slope_x == 0) + if (rcast->is_x == 1) { - - // tmp, to draw the map - if (rcast->tmp) - printf("(slope_x == 0) "); - // tmp end - - length = (rcast->next_y - rcast->ray_step_y); - } - else if (rcast->slope_y == 0) - { - - // tmp, to draw the map - if (rcast->tmp) - printf("(slope_y == 0) "); - // tmp end - - length = (rcast->next_x - rcast->ray_step_x); - } - else if (rcast->is_x == 1) - { - - // tmp, to draw the map - if (rcast->tmp) - printf("(is_x == 1) "); - // tmp end - - length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); + length = rcast->next_x - rcast->ray_step_x; + if (rcast->slope_y) + length /= ft_abs(rcast->slope_y); // length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; } else if (rcast->is_x == 0) { - - // tmp, to draw the map - if (rcast->tmp) - printf("(is_x == 0) "); - // tmp end - - length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + length = rcast->next_y - rcast->ray_step_y; + if (rcast->slope_x) + length /= ft_abs(rcast->slope_x); // length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; } rcast->ray_len = ft_abs(length); - - // tmp, to draw the map - if (rcast->tmp) - printf("length:%i", rcast->ray_len); - // tmp end - } static void wall_height(t_rcast *rcast) From 285f6f3c55f298431066c241a88f365802ce75e2 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 28 Apr 2022 09:35:05 +0200 Subject: [PATCH 5/6] add struct wall --- README.md | 1 - headers/cube3d_struct.h | 14 ++++++++++--- srcs/draw/raycast.c | 44 +++++++++++++++++++---------------------- srcs/init/init_struct.c | 2 +- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 5fd0b58..59c7835 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ - resolve wall height - resolve view buble -- resolve vertical ray - add textures # ressources diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 64c14be..e2df48d 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -26,6 +26,16 @@ typedef struct s_vec t_coord end; } t_vec; +/* + * struct for walls 3d drawing + */ + +typedef struct s_wall +{ + t_vec pos; + int height; +} t_wall; + /* * struct with all elements for raycasting */ @@ -35,9 +45,6 @@ typedef struct s_rcast t_vec screen_dist; t_vec screen_size; t_vec ray; - t_vec wall; - int tmp; - int wall_height; int screen_width; int screen_height; int cell; @@ -125,6 +132,7 @@ typedef struct s_game t_plr plr; // rays t_rcast rcast; + t_wall wall; // map t_map map; // map window diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 66d69d1..4bef579 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -30,65 +30,67 @@ } // tmp end -static void wall_length(t_rcast *rcast) +static void wall_length(t_rcast *rcast, t_wall *wall) { int length; - rcast->wall.start.x = rcast->ray_nb; - rcast->wall.end.x = rcast->ray_nb; + wall->pos.start.x = rcast->ray_nb; + wall->pos.end.x = rcast->ray_nb; if (rcast->is_x == 1) { length = rcast->next_x - rcast->ray_step_x; if (rcast->slope_y) length /= ft_abs(rcast->slope_y); -// length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; + length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; } else if (rcast->is_x == 0) { length = rcast->next_y - rcast->ray_step_y; if (rcast->slope_x) length /= ft_abs(rcast->slope_x); -// length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; + length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; } rcast->ray_len = ft_abs(length); } -static void wall_height(t_rcast *rcast) +static void wall_height(t_rcast *rcast, t_wall *wall) { int height; - height = rcast->wall_height - rcast->ray_len; + height = wall->height - rcast->ray_len; + if (height < 0) height = 0; if (height > rcast->screen_height) height = rcast->screen_height; - rcast->wall.start.y = rcast->screen_height / 2 + height / 2; - rcast->wall.end.y = rcast->screen_height / 2 - height / 2; + + wall->pos.start.y = rcast->screen_height / 2 + height / 2; + wall->pos.end.y = rcast->screen_height / 2 - height / 2; } -void draw_column(t_game *game, t_rcast *rcast) +void draw_column(t_game *game, t_rcast *rcast, t_wall *wall) { t_vec plane; int color; plane.start.x = rcast->ray_nb; plane.end.x = rcast->ray_nb; - if (rcast->wall.start.y > 0) + if (wall->pos.start.y > 0) { plane.start.y = rcast->screen_height; - plane.end.y = rcast->wall.start.y; + plane.end.y = wall->pos.start.y; draw_line(&game->img, &plane, 0x00FF0000); } - if (rcast->wall.start.y < rcast->screen_height) + if (wall->pos.start.y < rcast->screen_height) { - plane.start.y = rcast->wall.end.y; + plane.start.y = wall->pos.end.y; plane.end.y = 0; draw_line(&game->img, &plane, 0x000000FF); } color = 0x00FF00FF; if (rcast->is_x) color = 0x00EE00EE; - draw_line(&game->img, &rcast->wall, color); + draw_line(&game->img, &wall->pos, color); } void raycast(t_game *game, t_rcast *rcast) @@ -106,15 +108,9 @@ void raycast(t_game *game, t_rcast *rcast) // tmp end - wall_length(rcast); - wall_height(rcast); - draw_column(game, rcast); + wall_length(rcast, &game->wall); + wall_height(rcast, &game->wall); + draw_column(game, rcast, &game->wall); (rcast->ray_nb)++; - - // tmp, to draw the map - if (rcast->tmp) - printf("\n"); - // tmp end - } } diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 5546c5a..542dcc5 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -70,7 +70,6 @@ static void init_raycast(t_rcast *rcast) dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); rcast->screen_width = SCREEN_WIDTH; rcast->screen_height = SCREEN_HEIGHT; - rcast->wall_height = WALL_HEIGHT; // screen dist rcast->screen_dist.start.x = 0; rcast->screen_dist.start.y = 0; @@ -133,5 +132,6 @@ t_game *init_game(void) // raycasting init_raycast(&(game->rcast)); game->rcast.cell = game->map.cell; + game->wall.height = WALL_HEIGHT; return (game); } From 6640c9e6d3fdd0018e52ca53472061113fe2ce19 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 28 Apr 2022 20:27:46 +0200 Subject: [PATCH 6/6] wip several test to avoid buble effects, one wihtout doubles --- README.md | 1 - headers/cube3d_macro.h | 4 +-- headers/cube3d_struct.h | 15 ++-------- srcs/draw/draw.c | 4 ++- srcs/draw/raycast.c | 65 +++++++++++++++++++++-------------------- srcs/init/init_struct.c | 8 ++--- 6 files changed, 44 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 59c7835..7506f0b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # todo -- resolve wall height - resolve view buble - add textures diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index 85cb711..d4e9b89 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -17,8 +17,8 @@ # define SCREEN_FOCAL 90 /* size of a cell on the map */ # define CELL 20 -/* height of a wall without perpectiv */ -# define WALL_HEIGHT 200 +/* horizon fr perspectiv */ +# define HORIZON 700 /* * keys macro diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index e2df48d..37a24c5 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -26,27 +26,19 @@ typedef struct s_vec t_coord end; } t_vec; -/* - * struct for walls 3d drawing - */ - -typedef struct s_wall -{ - t_vec pos; - int height; -} t_wall; - /* * struct with all elements for raycasting */ typedef struct s_rcast { - t_vec screen_dist; t_vec screen_size; t_vec ray; + t_vec wall; + int hor; int screen_width; int screen_height; + int screen_dist; int cell; int cell_x; int cell_y; @@ -132,7 +124,6 @@ typedef struct s_game t_plr plr; // rays t_rcast rcast; - t_wall wall; // map t_map map; // map window diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 08a0245..bdcbfb8 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -102,6 +102,7 @@ void draw_line(t_img *img, t_vec *vec, int color) draw_square(game, pos, 0x00999900, 0x00330033, cell, 1); } +/* static void draw_screen(t_game *game, t_rcast *rcast) { t_vec screen; @@ -122,6 +123,7 @@ void draw_line(t_img *img, t_vec *vec, int color) rotate(&(game->plr), &(screen.end)); draw_line(&game->map_img, &screen, 0x00FFFFFF); } +*/ // tmp end void draw(t_game *game) @@ -133,7 +135,7 @@ void draw(t_game *game) raycast(game, &(game->rcast)); // tmp, to draw map - draw_screen(game, &(game->rcast)); +// draw_screen(game, &(game->rcast)); mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT); // tmp end diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 4bef579..c4c8e02 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -30,67 +30,70 @@ } // tmp end -static void wall_length(t_rcast *rcast, t_wall *wall) +static void wall_length(t_rcast *rcast) { - int length; + int len; - wall->pos.start.x = rcast->ray_nb; - wall->pos.end.x = rcast->ray_nb; + 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) { - length = rcast->next_x - rcast->ray_step_x; + len = rcast->next_x - rcast->ray_step_x; if (rcast->slope_y) - length /= ft_abs(rcast->slope_y); - length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; + 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) { - length = rcast->next_y - rcast->ray_step_y; + len = rcast->next_y - rcast->ray_step_y; if (rcast->slope_x) - length /= ft_abs(rcast->slope_x); - length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; + 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(length); + rcast->ray_len = ft_abs(len); } -static void wall_height(t_rcast *rcast, t_wall *wall) +static void wall_height(t_rcast *rcast) { int height; - height = wall->height - rcast->ray_len; + height = rcast->screen_height * (rcast->hor - rcast->ray_len) / rcast->hor; if (height < 0) height = 0; if (height > rcast->screen_height) height = rcast->screen_height; - wall->pos.start.y = rcast->screen_height / 2 + height / 2; - wall->pos.end.y = rcast->screen_height / 2 - height / 2; + rcast->wall.start.y = rcast->screen_height / 2 + height / 2; + rcast->wall.end.y = rcast->screen_height / 2 - height / 2; } -void draw_column(t_game *game, t_rcast *rcast, t_wall *wall) +void draw_column(t_game *game, t_rcast *rcast) { - t_vec plane; + t_vec plan; int color; - plane.start.x = rcast->ray_nb; - plane.end.x = rcast->ray_nb; - if (wall->pos.start.y > 0) + plan.start.x = rcast->ray_nb; + plan.end.x = rcast->ray_nb; + if (rcast->wall.start.y > 0) { - plane.start.y = rcast->screen_height; - plane.end.y = wall->pos.start.y; - draw_line(&game->img, &plane, 0x00FF0000); + plan.start.y = rcast->screen_height; + plan.end.y = rcast->wall.start.y; + draw_line(&game->img, &plan, 0x00FF0000); } - if (wall->pos.start.y < rcast->screen_height) + if (rcast->wall.start.y < rcast->screen_height) { - plane.start.y = wall->pos.end.y; - plane.end.y = 0; - draw_line(&game->img, &plane, 0x000000FF); + plan.start.y = rcast->wall.end.y; + plan.end.y = 0; + draw_line(&game->img, &plan, 0x000000FF); } color = 0x00FF00FF; if (rcast->is_x) color = 0x00EE00EE; - draw_line(&game->img, &wall->pos, color); + draw_line(&game->img, &rcast->wall, color); } void raycast(t_game *game, t_rcast *rcast) @@ -108,9 +111,9 @@ void raycast(t_game *game, t_rcast *rcast) // tmp end - wall_length(rcast, &game->wall); - wall_height(rcast, &game->wall); - draw_column(game, rcast, &game->wall); + wall_length(rcast); + wall_height(rcast); + draw_column(game, rcast); (rcast->ray_nb)++; } } diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 542dcc5..3f2f426 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -68,13 +68,10 @@ static void init_raycast(t_rcast *rcast) dist = (SCREEN_FOCAL / 2) * M_PI / 180; dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); + rcast->screen_dist = dist; rcast->screen_width = SCREEN_WIDTH; rcast->screen_height = SCREEN_HEIGHT; - // screen dist - rcast->screen_dist.start.x = 0; - rcast->screen_dist.start.y = 0; - rcast->screen_dist.end.x = 0; - rcast->screen_dist.end.y = -dist; + rcast->hor = HORIZON; // screen size rcast->screen_size.start.x = -SCREEN_WIDTH / 2; rcast->screen_size.start.y = -dist; @@ -132,6 +129,5 @@ t_game *init_game(void) // raycasting init_raycast(&(game->rcast)); game->rcast.cell = game->map.cell; - game->wall.height = WALL_HEIGHT; return (game); }