first DDA scratch

This commit is contained in:
Willem Haffmans 2025-05-02 17:01:15 +02:00
parent 8ba694a25a
commit 7ba83f1772

77
src/render/DDAscratch.c Normal file
View File

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* DDAscratch.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
/* Updated: 2025/05/02 16:57:20 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "cub3d.h"
t_vec2 get_delta_dist(t_vec2 ray_dir)
{
t_vec2 delta_dist;
delta_dist.x = (ray_dir.x == 0) * 1e30 + (ray_dir.x != 0) * fabs(1 / ray_dir.x);
delta_dist.y = (ray_dir.y == 0) * 1e30 + (ray_dir.y != 0) * fabs(1 / ray_dir.y);
return (delta_dist);
}
t_vec2 get_step(t_vec2 ray_dir)
{
t_vec2 step;
step.x = 2 * (ray_dir.x >= 0) - 1;
step.y = 2 * (ray_dir.y >= 0) - 1;
return (step);
}
t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
{
const t_vec2 frac_pos = (t_vec2){pos.x - (int)pos.x, pos.y - (int)pos.y};
const int raydir_x_pos = (ray_dir.x >= 0);
const int raydir_y_pos = (ray_dir.y >= 0);
t_vec2 side_dist;
side_dist.x = ((1 - raydir_x_pos) * frac_pos.x + raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x;
side_dist.y = ((1 - raydir_y_pos) * frac_pos.y + raydir_x_pos * (1 - frac_pos.y)) * delta_dist.y;
return (side_dist);
}
int DDA_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
{
const t_vec2 delta_dist = get_delta_dist(ray_dir);
const t_vec2 step = get_step(ray_dir);
int side;
while (1)
{
side = (side_dist->x >= side_dist->y);
side_dist->x += delta_dist.x * (1 - side);
side_dist->y += delta_dist.y * side;
map_pos.x += step.x * (1 - side);
map_pos.y += step.y * side;
if (map->grid[map_pos.y][map_pos.x] == TILE_WALL)
break;
}
return (side);
}
// Returns the distance to the wall hit if the wall is hit in y direction the result is
// negative, if the wall is hit in x direction the result is positive
int DDA(t_vec2 ray_dir, t_vec2 pos, t_map *map)
{
const t_vec2 delta_dist = get_delta_dist(ray_dir);
t_vec2 side_dist;
int side;
side_dist = get_side_dist(ray_dir, pos, delta_dist);
side = DDA_main(ray_dir,(t_vec2_int) {(int)pos.x, (int)pos.y}, &side_dist, map);
return (1 - side) * (side_dist.x - delta_dist.x) - side * (side_dist.y - delta_dist.y);
}