diff --git a/Makefile b/Makefile index d09b686..798b6fa 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: jteissie +#+ +:+ +#+ # +# By: tsuchen +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/08/15 12:02:08 by tsuchen #+# #+# # -# Updated: 2024/08/16 19:28:50 by jteissie ### ########.fr # +# Updated: 2024/08/27 13:58:10 by tsuchen ### ########.fr # # # # **************************************************************************** # @@ -22,17 +22,21 @@ SRCS_MAP = map.c SRCS_RC = raycasting.c +SRCS_VEC = vector.c vector_2.c + PATH_M = srcs/ PATH_PS = srcs/parser/ PATH_MAP = srcs/map/ PATH_RC = srcs/raycasting/ +PATH_VEC = srcs/vector/ SRCS = $(addprefix $(PATH_M), $(SRCS_M)) \ $(addprefix $(PATH_PS), $(SRCS_PS)) \ $(addprefix $(PATH_MAP), $(SRCS_MAP)) \ - $(addprefix $(PATH_RC), $(SRCS_RC)) + $(addprefix $(PATH_RC), $(SRCS_RC)) \ + $(addprefix $(PATH_VEC), $(SRCS_VEC)) -HEADERS = cub3d.h +HEADERS = cub3d.h parser.h vector.h OBJS = $(SRCS:.c=.o) diff --git a/includes/cub3d.h b/includes/cub3d.h index d9f3a85..877b0fa 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* cub3d.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/08/16 19:18:23 by jteissie ### ########.fr */ +/* Updated: 2024/08/27 11:37:10 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ # include "get_next_line_bonus.h" # include "parser.h" +# include "vector.h" # define SUCCESS 0 # define PANIC 1 @@ -60,13 +61,17 @@ typedef enum e_p_dir WEST = 'W', EAST = 'E', } t_p_dir; + typedef struct s_data { char *map_path; int map_fd; size_t map_bound; char **map; - t_p_dir p_dir; + t_p_dir p_dir_default; + t_vec p_pos; + t_vec p_dir; + t_vec p_cam; } t_data; #endif diff --git a/includes/parser.h b/includes/parser.h index f07b421..9b46280 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -3,15 +3,16 @@ /* ::: :::::::: */ /* parser.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 15:00:57 by jteissie #+# #+# */ -/* Updated: 2024/08/16 18:58:41 by jteissie ### ########.fr */ +/* Updated: 2024/08/27 14:00:34 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PARSER_H # define PARSER_H + # include "cub3d.h" typedef enum e_bool t_bool; diff --git a/includes/vector.h b/includes/vector.h new file mode 100644 index 0000000..54e1c44 --- /dev/null +++ b/includes/vector.h @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vector.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/27 10:56:32 by tsuchen #+# #+# */ +/* Updated: 2024/08/27 14:42:47 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VECTOR_H +# define VECTOR_H + +# include +# include +# include +# include +# include + +# define PI 3.14159265359f + +typedef struct s_vec +{ + double x; + double y; +} t_vec; + +void vec_init(t_vec *vec, double x, double y); +void vec_add(t_vec *vec_a, const t_vec *vec_b); +void vec_sub(t_vec *vec_a, const t_vec *vec_b); +void vec_muls(t_vec *vec, double scalar); +double vec_dot(const t_vec *vec_a, const t_vec *vec_b); +double vec_absv(const t_vec *vec); +double vec_dist(const t_vec *vec_a, const t_vec *vec_b); +void vec_mirror(t_vec *vec); +void vec_transp(t_vec *vec); +void vec_rotate(t_vec *vec, double angle); + +#endif diff --git a/srcs/main.c b/srcs/main.c index 9c2f6cb..57c56a6 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -3,15 +3,33 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/08/16 19:40:02 by jteissie ### ########.fr */ +/* Updated: 2024/08/27 11:51:43 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" +void print_data(t_data *data) +{ + int i; + + i = 0; + printf("Player position x: %.2f, y: %.2f\n", data->p_pos.x, data->p_pos.y); + printf("Player direction x: %.2f, y: %.2f\n", data->p_dir.x, data->p_dir.y); + printf("Player default dir: %d\n", data->p_dir_default); + printf("Map path is: %s\n", data->map_path); + printf("Map output:\n"); + while (data->map[i]) + { + printf("%s", data->map[i]); + i++; + } + printf("Map bound: %zu\n", data->map_bound); +} + static t_bool is_cub_file(char *file) { uint32_t index; @@ -42,6 +60,7 @@ int main(int ac, char *av[]) return (EXIT_FAILURE); if (parse_map(&data) == PANIC) return (EXIT_FAILURE); + print_data(&data); ft_free_all(data.map); close(data.map_fd); return (0); diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index f739940..e72d707 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -3,7 +3,7 @@ /* ::: :::::::: */ /* parser.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:12:00 by tsuchen #+# #+# */ /* Updated: 2024/08/27 13:17:50 by jteissie ### ########.fr */ @@ -39,13 +39,26 @@ t_parse_status check_walls(char **map, uint32_t x, uint32_t y, size_t bound) static void get_player_dir(t_data *data, u_int32_t x, u_int32_t y) { if (data->map[x][y] == 'N') - data->p_dir = NORTH; + { + data->p_dir_default = NORTH; + vec_init(&data->p_dir, -1.0f, 0.0f); + } else if (data->map[x][y] == 'S') - data->p_dir = SOUTH; + { + data->p_dir_default = SOUTH; + vec_init(&data->p_dir, 1.0f, 0.0f); + } else if (data->map[x][y] == 'W') - data->p_dir = WEST; + { + data->p_dir_default = WEST; + vec_init(&data->p_dir, 0.0f, -1.0f); + } else if (data->map[x][y] == 'E') - data->p_dir = EAST; + { + data->p_dir_default = EAST; + vec_init(&data->p_dir, 0.0f, 1.0f); + } + vec_init(&data->p_pos, (double)x, (double)y); } t_parse_status verify_map(char **map, t_data *data) diff --git a/srcs/parser/parser_utils.c b/srcs/parser/parser_utils.c index 85e52d7..1ac4820 100644 --- a/srcs/parser/parser_utils.c +++ b/srcs/parser/parser_utils.c @@ -3,7 +3,7 @@ /* ::: :::::::: */ /* parser_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/16 18:12:37 by jteissie #+# #+# */ /* Updated: 2024/08/27 13:23:59 by jteissie ### ########.fr */ diff --git a/srcs/vector/vector.c b/srcs/vector/vector.c new file mode 100644 index 0000000..4c6c380 --- /dev/null +++ b/srcs/vector/vector.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vector.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/27 11:11:34 by tsuchen #+# #+# */ +/* Updated: 2024/08/27 15:46:44 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "vector.h" + +void vec_init(t_vec *vec, double x, double y) +{ + vec->x = x; + vec->y = y; +} + +void vec_add(t_vec *vec_a, const t_vec *vec_b) +{ + vec_a->x = vec_a->x + vec_b->x; + vec_a->y = vec_a->y + vec_b->y; +} + +void vec_sub(t_vec *vec_a, const t_vec *vec_b) +{ + vec_a->x = vec_a->x - vec_b->x; + vec_a->y = vec_a->y - vec_b->y; +} + +void vec_muls(t_vec *vec, double scalar) +{ + vec->x = vec->x * scalar; + vec->y = vec->y * scalar; +} + +double vec_dot(const t_vec *vec_a, const t_vec *vec_b) +{ + double dot; + + dot = (vec_a->x * vec_b->x) + (vec_a->y * vec_b->y); + return (dot); +} + +// int main(void) +// { +// t_vec a; +// t_vec b; + +// vec_init(&a, 3, 4); +// vec_init(&b, 1, 2); +// printf("vec_a init ax: %.2f, ay: %.2f\n", a.x, a.y); +// printf("vec_b init bx: %.2f, by: %.2f\n", b.x, b.y); +// vec_add(&a, &b); +// printf("vec_a after adding b ax: %.2f, ay: %.2f\n", a.x, a.y); +// vec_sub(&a, &b); +// printf("vec_a after minus b ax: %.2f, ay: %.2f\n", a.x, a.y); +// vec_muls(&a, 5); +// printf("vec_a after multiply by 5 ax: %.2f, ay: %.2f\n", a.x, a.y); +// printf("vec_a dot vec_b is %.2f\n", vec_dot(&a, &b)); +// printf("abs of a is %.2f\n", vec_absv(&a)); +// printf("dist between a and b: %.2f\n", vec_dist(&a, &b)); +// vec_mirror(&a); +// printf("vec_a after mirror ax: %.2f, ay: %.2f\n", a.x, a.y); +// vec_transp(&a); +// printf("vec_a after transp ax: %.2f, ay: %.2f\n", a.x, a.y); +// vec_rotate(&b, 30); +// printf("vec_b after rotate 30 degree bx: %.2f, by: %.2f\n", b.x, b.y); +// return (0); +// } diff --git a/srcs/vector/vector_2.c b/srcs/vector/vector_2.c new file mode 100644 index 0000000..16d75d8 --- /dev/null +++ b/srcs/vector/vector_2.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vector_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/27 13:33:30 by tsuchen #+# #+# */ +/* Updated: 2024/08/27 15:42:12 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "vector.h" + +double vec_absv(const t_vec *vec) +{ + double absv; + + absv = sqrt((vec->x * vec->x) + (vec->y * vec->y)); + return (absv); +} + +double vec_dist(const t_vec *vec_a, const t_vec *vec_b) +{ + double dist; + double del_x; + double del_y; + + del_x = vec_b->x - vec_a->x; + del_y = vec_b->y - vec_a->y; + dist = sqrt((del_x * del_x) + (del_y * del_y)); + return (dist); +} + +void vec_mirror(t_vec *vec) +{ + vec->x = vec->x * -1; + vec->y = vec->y * -1; +} + +void vec_transp(t_vec *vec) +{ + double tmp; + + tmp = vec->x; + vec->x = vec->y; + vec->y = tmp; +} + +void vec_rotate(t_vec *vec, double angle) +{ + double new_x; + double new_y; + double pi; + double angle_r; + + pi = acos(-1); + angle_r = angle * pi / 180; + new_x = vec->x * cos(angle_r) - vec->y * sin(angle_r); + new_y = vec->x * sin(angle_r) + vec->y * cos(angle_r); + vec->x = new_x; + vec->y = new_y; +}