diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c119296 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/lib/libft/inc", + "${workspaceFolder}/lib/MLX42/include/MLX42" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-clang-x64", + "configurationProvider": "ms-vscode.makefile-tools" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/inc/types.h b/inc/types.h index 3e1d8b5..77b3e25 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/05/02 14:38:53 by whaffman ######## odam.nl */ +/* Updated: 2025/05/02 18:06:18 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -78,6 +78,21 @@ typedef struct s_screen unsigned int height; } t_screen; +typedef enum e_side +{ + SIDE_NORTH, + SIDE_SOUTH, + SIDE_WEST, + SIDE_EAST, +} t_side; + +typedef struct s_render +{ + float perp_dist; + t_side side; + float wall_x; +} t_render; + typedef struct s_game { t_map *map; diff --git a/src/render/DDAscratch.c b/src/render/DDAscratch.c index 880ac20..3964525 100644 --- a/src/render/DDAscratch.c +++ b/src/render/DDAscratch.c @@ -6,24 +6,28 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */ -/* Updated: 2025/05/02 16:57:20 by whaffman ######## odam.nl */ +/* Updated: 2025/05/02 20:26:34 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "cub3d.h" +#include "vec_math.h" +#include -t_vec2 get_delta_dist(t_vec2 ray_dir) +t_vec2 get_delta_dist(t_vec2 ray_dir) { - t_vec2 delta_dist; + 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); + 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 get_step(t_vec2 ray_dir) { - t_vec2 step; + t_vec2 step; step.x = 2 * (ray_dir.x >= 0) - 1; step.y = 2 * (ray_dir.y >= 0) - 1; @@ -32,46 +36,104 @@ t_vec2 get_step(t_vec2 ray_dir) 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; + 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; + 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) +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; + int hit; - while (1) + hit = 0; + while (hit == 0) { 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; + hit = (map->grid[map_pos.y][map_pos.x] == TILE_WALL); } 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) +float 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; + 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); + 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)); } +t_side get_side(t_vec2 ray_dir, float perp_dist) +{ + if(perp_dist > 0) + { + if (ray_dir.x > 0) + return (SIDE_EAST); + else + return (SIDE_WEST); + } + else + { + if (ray_dir.y > 0) + return (SIDE_SOUTH); + else + return (SIDE_NORTH); + } +} +t_render cast_ray(t_game *game, int x) +{ + t_vec2 ray_dir; + t_vec2 pos; + t_render render; + float perp_dist; + + ray_dir = add(game->player->dir, + mul(game->player->camera, + (2.0f * x / (float)game->screen->width - 1))); + pos = game->player->pos; + perp_dist = DDA(ray_dir, pos, game->map); + render.perp_dist = fabs(perp_dist); + render.side = get_side(ray_dir); + render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist) + + (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist); + render.wall_x -= floor(render.wall_x); + return (render); +} + +void cast_rays(t_game *game) +{ + int x; + t_render render[WIDTH]; + + x = 0; + while (x < game->screen->width) + { + render[x] = cast_ray(game, x); + //ETC + + x++; + } +} \ No newline at end of file