wip debug ray length

This commit is contained in:
hugogogo
2022-04-28 00:44:02 +02:00
parent a0f32dee98
commit 3501a20abb
4 changed files with 102 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
# todo # todo
- resolve wall height
- resolve view buble - resolve view buble
- add slide against wall
- resolve vertical ray - resolve vertical ray
- add textures - add textures

View File

@@ -32,6 +32,7 @@ typedef struct s_vec
typedef struct s_rcast typedef struct s_rcast
{ {
int tmp;
t_vec screen_dist; t_vec screen_dist;
t_vec screen_size; t_vec screen_size;
t_vec ray; t_vec ray;
@@ -56,6 +57,7 @@ typedef struct s_rcast
int ray_step_y; int ray_step_y;
int is_x; int is_x;
int ray_nb; int ray_nb;
int ray_len;
double ratio; double ratio;
} t_rcast; } t_rcast;

View File

@@ -22,37 +22,67 @@ static void init_raycast(t_rcast *rcast, t_vec *ray)
// ray steps in both axis // ray steps in both axis
rcast->ray_step_x = ft_abs(rcast->cell * rcast->slope_y); 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_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) static void init_first_step(t_rcast *rcast, t_vec *ray)
{ {
rcast->first_next_x = ray->start.x % rcast->cell; rcast->first_next_x = 0;
if (rcast->first_next_x && rcast->ray_sign_x < 0) if (rcast->slope_x)
rcast->first_next_x = rcast->cell - rcast->first_next_x; rcast->first_next_x = ray->start.x % rcast->cell;
if (!rcast->first_next_x && rcast->ray_sign_x < 0) if (rcast->ray_sign_x < 0)
rcast->first_next_x = rcast->cell; rcast->first_next_x -= rcast->cell;
rcast->first_next_y = ray->start.y % rcast->cell; rcast->first_next_y = 0;
if (rcast->first_next_y && rcast->ray_sign_y < 0) if (rcast->slope_y)
rcast->first_next_y = rcast->cell - rcast->first_next_y; rcast->first_next_y = ray->start.y % rcast->cell;
if (!rcast->first_next_y && rcast->ray_sign_y < 0) if (rcast->ray_sign_y < 0)
rcast->first_next_y = rcast->cell; rcast->first_next_y -= rcast->cell;
rcast->next_x = ft_abs(rcast->first_next_x * rcast->slope_y); 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_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) 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->cell_y += rcast->next_cell_y;
rcast->next_y += rcast->ray_step_y; rcast->next_y += rcast->ray_step_y;
rcast->is_x = 0; rcast->is_x = 0;
// tmp
if (rcast->tmp)
printf(" [yes] (increase y by %i)", rcast->ray_step_y);
//tmp
} }
else else
{ {
rcast->cell_x += rcast->next_cell_x; rcast->cell_x += rcast->next_cell_x;
rcast->next_x += rcast->ray_step_x; rcast->next_x += rcast->ray_step_x;
rcast->is_x = 1; 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.x += rcast->ray_nb;
ray->end.y = rcast->ray.end.y + game->plr.pos.y; ray->end.y = rcast->ray.end.y + game->plr.pos.y;
rotate(&(game->plr), &(ray->end)); 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_raycast(rcast, ray);
init_first_step(rcast, ray); init_first_step(rcast, ray);
while (!is_wall(game, rcast->cell_x, rcast->cell_y)) while (!is_wall(game, rcast->cell_x, rcast->cell_y))
next_cell(rcast); next_cell(rcast);
// tmp
if (rcast->tmp)
printf("\n");
//tmp end
} }

View File

@@ -30,10 +30,14 @@
} }
// tmp end // tmp end
static void calcul_wall(t_rcast *rcast) static void wall_length(t_rcast *rcast)
{ {
int length; 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.start.x = rcast->ray_nb;
rcast->wall.end.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 = (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 = (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) if (rcast->is_x == 0 && rcast->slope_x != 0)
{ {
length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); 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 = (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); rcast->ray_len = ft_abs(length);
height = rcast->wall_height - 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) if (height < 0)
height = 0; height = 0;
if (height > rcast->screen_height) if (height > rcast->screen_height)
@@ -98,8 +124,16 @@ void raycast(t_game *game, t_rcast *rcast)
draw_line(&game->map_img, &ray, 0x00FF00FF); draw_line(&game->map_img, &ray, 0x00FF00FF);
// tmp end // tmp end
calcul_wall(rcast);
wall_length(rcast);
wall_height(rcast);
draw_column(game, rcast); draw_column(game, rcast);
(rcast->ray_nb)++; (rcast->ray_nb)++;
// tmp, to draw the map
if (rcast->tmp)
printf("\n");
// tmp end
} }
} }