From edf44d14993e799cfbea61709fe9c905cbba2a24 Mon Sep 17 00:00:00 2001 From: narnaud Date: Wed, 1 Jun 2022 17:19:12 +0200 Subject: [PATCH] fix: rot_vec reducing vector progressivly, add: some more rework --- Makefile | 2 +- README.md | 10 ++- includes/cub3d.h | 6 +- includes/mlx_keycode.h | 4 +- libft/Makefile | 2 + sources/hooks.c | 12 ++-- sources/render.c | 157 ++++++++++++++++------------------------- sources/utils.c | 10 ++- 8 files changed, 90 insertions(+), 113 deletions(-) diff --git a/Makefile b/Makefile index da5eaa4..1645926 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ endif ifeq ($(UNAME_S), Darwin) LFLAGS = -lmlx -framework OpenGL -framework AppKit -L. -lft endif -CFLAGS = -Werror -Wall -Wextra #-O3 -ffast-math -funsafe-math-optimizations +CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations %.o:%.c ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} diff --git a/README.md b/README.md index 436c79b..f172e87 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ CUBE3D - [Debug maps (Outdated?)](https://github.com/nsterk/cub3D/tree/main/maps) - [Tester & maps](https://github.com/MichelleJiam/cub3D_tester) +## Issues: +- Distances increase while time pass. + ## Program life cycle : ### Parsing - [x] read file, @@ -29,11 +32,12 @@ CUBE3D - [ ] display wall's texture ### Control - [x] enable commands - - [ ] wasd to move - - [x] arrows to move + - [x] wasd to move - [ ] react to commands : - [x] calculate new pos, - [x] display new view, - - [-] avoid colisions + - [x] avoid colisions + - [x] M to show minimap + - [x] SHIFT to run ### Quit - [ ] clean and exit diff --git a/includes/cub3d.h b/includes/cub3d.h index 0b1dd1a..2e376c1 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ -/* Updated: 2022/06/01 14:23:18 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 14:59:38 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,7 +79,7 @@ typedef struct s_env int endian; int *buffer; int minimap; - int controls[256]; + int controls[KEYS_LIMIT]; char *wallTexture[4]; int floorColor; int ceilColor; @@ -91,7 +91,7 @@ typedef struct s_env t_vec_d camPlan; } t_env; -void render_minimap(t_env *env); +void render_minimap(t_env *env, t_vec size); void render_view(t_env *env); void render(t_env *env); diff --git a/includes/mlx_keycode.h b/includes/mlx_keycode.h index 400a5ef..5c142bb 100644 --- a/includes/mlx_keycode.h +++ b/includes/mlx_keycode.h @@ -6,7 +6,7 @@ /* By: bledda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/08/10 20:43:21 by bledda #+# #+# */ -/* Updated: 2022/05/23 09:42:56 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 14:58:39 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define MLX_KEYCODE_H # ifdef __linux__ +# define KEYS_LIMIT 66000 # define KEY_Q 113 # define KEY_W 119 # define KEY_E 101 @@ -63,6 +64,7 @@ # define KEY_CTRL 65507 # define KEY_SPACE 32 # elif __APPLE__ +# define KEYS_LIMIT 300 # define KEY_Q 12 # define KEY_W 13 # define KEY_E 14 diff --git a/libft/Makefile b/libft/Makefile index 465c18a..d25eb64 100755 --- a/libft/Makefile +++ b/libft/Makefile @@ -41,6 +41,8 @@ CFLAGS = -Wall -Wextra -Werror all : $(NAME) +$(VERBOSE).SILENT: + $(NAME): $(OBJS) echo Making libft... ${AR} ${NAME} ${OBJS} diff --git a/sources/hooks.c b/sources/hooks.c index 3382ab5..6211ca4 100644 --- a/sources/hooks.c +++ b/sources/hooks.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */ -/* Updated: 2022/06/01 14:26:26 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 17:10:10 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,17 +32,19 @@ int update_hook(t_env *env) double dX; double dY; - dX = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x / 10; - dY = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y / 10; + dX = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x + / (10 - env->controls[KEY_SHFT] * 5); + dY = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y + / (10 - env->controls[KEY_SHFT] * 5); if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + dX)] == '0') set_vec(&env->playerPos, env->playerPos.x + dX, env->playerPos.y); if (env->map[(int)(env->playerPos.y + dY)][(int)env->playerPos.x] == '0') set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + dY); rot_vec(&env->playerDir, - (double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36); + (double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 36); rot_vec(&env->camPlan, - (double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36); + (double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 36); if (DEBUG) { printf("playerPos: %f, %f. playerDir: %f, %f\n", env->playerPos.x, env->playerPos.y, env->playerDir.x, env->playerDir.y); diff --git a/sources/render.c b/sources/render.c index 8511dba..0db9525 100644 --- a/sources/render.c +++ b/sources/render.c @@ -6,63 +6,51 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */ -/* Updated: 2022/06/01 14:06:23 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 17:00:21 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/cub3d.h" -void draw_vert(t_env *env, int x, int *startEnd, int color) +void draw_rectangle(t_env *env, t_rectangle rect, int borders) { - int y; - - y= 0; - while (y < startEnd[0]) - { - env->buffer[env->line_bytes * y + x] = env->ceilColor; - y++; - } - while (startEnd[0] < startEnd[1]) - { - env->buffer[env->line_bytes * startEnd[0] + x] = color; - startEnd[0]++; - } - y = startEnd[0]; - while (y < WIN_Y_SZ) - { - env->buffer[env->line_bytes * y + x] = env->floorColor; - y++; - } -} - -void draw_rectangle(t_env *env, t_rectangle rect) -{ - int step_x; - int step_y; - - step_y = 0; - while (step_y < rect.sizeY) + int stepX; + int stepY; + + stepY = 0; + while (stepY < rect.sizeY) { - step_x = 0; - while (step_x < rect.sizeX) + stepX = 0; + while (stepX < rect.sizeX) { - if (step_x == rect.sizeX - 1 || step_y == rect.sizeY - 1) - env->buffer[(env->line_bytes * (int)(rect.startY + step_y)) - + (int)(rect.startX + step_x)] = 0; + if (borders && (stepX == rect.sizeX - 1 || stepY == rect.sizeY - 1)) + env->buffer[(env->line_bytes * (int)(rect.startY + stepY)) + + (int)(rect.startX + stepX)] = 0; else - env->buffer[(env->line_bytes * (int)(rect.startY + step_y)) - + (int)(rect.startX+ step_x)] = rect.color; - step_x++; + env->buffer[(env->line_bytes * (int)(rect.startY + stepY)) + + (int)(rect.startX + stepX)] = rect.color; + stepX++; } - step_y++; + stepY++; } } -void render_minimap(t_env *env) +void draw_vert(t_env *env, int x, int *startEnd, int color) +{ + draw_rectangle(env, (t_rectangle){x, 0, 1, startEnd[0] - 1, env->ceilColor}, 0); + draw_rectangle(env, (t_rectangle){x, startEnd[0], 1, startEnd[1] - startEnd[0] - 1, color}, 0); + draw_rectangle(env, (t_rectangle){x, startEnd[1], 1, WIN_Y_SZ - startEnd[1], env->floorColor}, 0); +} + +void render_minimap(t_env *env, t_vec size) { char **map; t_vec vec; + int dX; + int dY; + dX = size.x / env->wide; + dY = size.y / env->deep; vec.y = 0; map = env->map; while (map[vec.y]) @@ -71,87 +59,62 @@ void render_minimap(t_env *env) while (map[vec.y][vec.x]) { if (map[vec.y][vec.x] == '0') - draw_rectangle(env, (t_rectangle){vec.x * 6, vec.y * 6, 6, 6, 39424}); + draw_rectangle(env, (t_rectangle){vec.x * dX, vec.y * dY, dX, dY, 39424}, 1); else if (map[vec.y][vec.x] == '1') - draw_rectangle(env, (t_rectangle){vec.x * 6, vec.y * 6, 6, 6, 11885067}); + draw_rectangle(env, (t_rectangle){vec.x * dX, vec.y * dY, dX, dY, 11885067}, 1); vec.x++; } vec.y++; } - draw_rectangle(env, (t_rectangle){env->playerPos.x * 6 - 2,env->playerPos.y * 6 - 2, 4, 4, 255}); + draw_rectangle(env, (t_rectangle){ + env->playerPos.x * dX - dX / 4, env->playerPos.y * dY - dY / 4, + dX / 2, dY / 2, 255}, 0); } double get_wall_dist(t_env *env, int x) { double camX; t_vec_d ray; - t_vec_d dDist; - t_vec_d sDist; - t_vec cell; - t_vec step; + double dDist[2]; + double sDist[2]; + int cell[2]; + int *step; int hit; int side; - if (env->map[(int)env->playerPos.y][(int)env->playerPos.x] > '0') - return (1e-30); camX = 2 * x / (double)WIN_X_SZ - 1; set_vec(&ray, env->playerDir.x + env->camPlan.x * camX, env->playerDir.y + env->camPlan.y * camX); - cell.x = (int)(env->playerPos.x); - cell.y = (int)(env->playerPos.y); + cell[0] = (int)(env->playerPos.x); + cell[1] = (int)(env->playerPos.y); if (ray.x == 0) - dDist.x = 1e30; + dDist[0] = 1e30; else - dDist.x = fabs(1 / ray.x); + dDist[0] = fabs(1 / ray.x); if (ray.y == 0) - dDist.y = 1e30; + dDist[1] = 1e30; else - dDist.y = fabs(1 / ray.y); + dDist[1] = fabs(1 / ray.y); hit = 0; - if (ray.x < 0) - { - step.x = -1; - sDist.x = (env->playerPos.x - cell.x) * dDist.x; - } - else - { - step.x = 1; - sDist.x = (cell.x + 1.0 - env->playerPos.x) * dDist.x; - } - if (ray.y < 0) - { - step.y = -1; - sDist.y = (env->playerPos.y - cell.y) * dDist.y; - } - else - { - step.y = 1; - sDist.y = (cell.y + 1.0 - env->playerPos.y) * dDist.y; - } + step = (int[2]){0, 0}; + if (ray.x < 0 && --step[0]) + sDist[0] = (env->playerPos.x - cell[0]) * dDist[0]; + else if (++step[0]) + sDist[0] = (cell[0] + 1.0 - env->playerPos.x) * dDist[0]; + if (ray.y < 0 && --step[1]) + sDist[1] = (env->playerPos.y - cell[1]) * dDist[1]; + else if (++step[1]) + sDist[1] = (cell[1] + 1.0 - env->playerPos.y) * dDist[1]; while (hit == 0) { - if (sDist.x < sDist.y) - { - sDist.x += dDist.x; - cell.x += step.x; - side = 0; - } - else - { - sDist.y += dDist.y; - cell.y += step.y; - side = 1; - } - if (env->map[cell.y][cell.x] > '0') + side = 0; + if (sDist[0] > sDist[1]) + side++; + sDist[side] += dDist[side]; + cell[side] += step[side]; + if (env->map[cell[1]][cell[0]] > '0') hit = 1; } - if (DEBUG) - { - //printf("X:%d - sideDist: %f, %f. deltaDist: %f, %f.\n", x, sDist.x, sDist.y, dDist.x, dDist.y); - } - if (side == 0) - return (sDist.x - dDist.x); - else - return (sDist.y - dDist.y); + return (sDist[side] - dDist[side]); } void render_view(t_env *env) @@ -187,7 +150,7 @@ void render(t_env *env) env->line_bytes /= 4; render_view(env); if (env->minimap) - render_minimap(env); + render_minimap(env, (t_vec){env->wide * 8, env->deep * 8}); mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0); mlx_destroy_image(env->mlx, env->img); } diff --git a/sources/utils.c b/sources/utils.c index 4db51be..789fdf4 100644 --- a/sources/utils.c +++ b/sources/utils.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/31 08:59:01 by narnaud #+# #+# */ -/* Updated: 2022/06/01 14:21:41 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 17:16:10 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,12 @@ void set_vec(t_vec_d *vec, double x, double y) void rot_vec(t_vec_d *vec, double rad) { - vec->x = cos(M_PI * rad) * vec->x - sin(M_PI * rad) * vec->y; - vec->y = sin(M_PI * rad) * vec->x + cos(M_PI * rad) * vec->y; + double len; + + len = vec_len(*vec); + vec->x = (cos(rad) * vec->x - sin(rad) * vec->y) * 0.66 / len; + vec->y = (sin(rad) * vec->x + cos(rad) * vec->y) * 0.66 / len; + } int rgb_to_int(char **rgb)