From 630adb62f131df6dc11fb37b737de588068de8bc Mon Sep 17 00:00:00 2001 From: narnaud Date: Wed, 1 Jun 2022 18:32:55 +0200 Subject: [PATCH] fix: camPlan not sync with playerDir --- includes/cub3d.h | 4 ++-- sources/hooks.c | 7 +++---- sources/parsing.c | 6 ++---- sources/utils.c | 13 +++++++------ 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/includes/cub3d.h b/includes/cub3d.h index 4655f5f..e14f9ee 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 17:45:22 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 18:13:08 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,7 +102,7 @@ int update_hook(t_env *env); int rgb_to_int(char **rgb); void set_vec(t_vec_d *vec, double x, double y); double vec_len(t_vec_d vec); -void rot_vec(t_vec_d *vec, double rad, double init_len); +t_vec_d rot_vec(t_vec_d vec, double rad, double init_len); t_env *parse_envFile(char *filename); char *get_next_line(int fd); diff --git a/sources/hooks.c b/sources/hooks.c index cc4f6b8..e333cea 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 17:49:24 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 18:29:53 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,10 +41,9 @@ int update_hook(t_env *env) 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, + env->playerDir = rot_vec(env->playerDir, (double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 72, 1); - rot_vec(&env->camPlan, - (double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 72, 0.66); + env->camPlan = rot_vec(env->playerDir, M_PI / 2, 0.66); 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/parsing.c b/sources/parsing.c index 42545d0..d7adb71 100644 --- a/sources/parsing.c +++ b/sources/parsing.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/23 17:20:29 by narnaud #+# #+# */ -/* Updated: 2022/05/31 12:25:58 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 18:23:37 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -123,9 +123,7 @@ int init_player(t_env *env, int x, int y) set_vec(&env->playerDir, 1, 0); else if (orientation == 'W') set_vec(&env->playerDir, -1, -1); - set_vec(&env->camPlan, - 2 * (cos(M_PI / 2) * env->playerDir.x - sin(M_PI / 2) * env->playerDir.y) / 3, - 2 * (sin(M_PI / 2) * env->playerDir.x + cos(M_PI / 2) * env->playerDir.y) / 3); + env->camPlan = rot_vec(env->playerDir, M_PI / 2, 0.66); return (1); } diff --git a/sources/utils.c b/sources/utils.c index 0a52bd1..fa10b4a 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 17:44:56 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 18:18:37 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,14 +23,15 @@ void set_vec(t_vec_d *vec, double x, double y) vec->y = y; } -void rot_vec(t_vec_d *vec, double rad, double init_len) +t_vec_d rot_vec(t_vec_d vec, double rad, double init_len) { double len; + t_vec_d ret; - len = vec_len(*vec); - vec->x = (cos(rad) * vec->x - sin(rad) * vec->y) * init_len / len; - vec->y = (sin(rad) * vec->x + cos(rad) * vec->y) * init_len / len; - + len = vec_len(vec); + ret.x = (cos(rad) * vec.x - sin(rad) * vec.y) * init_len / len; + ret.y = (sin(rad) * vec.x + cos(rad) * vec.y) * init_len / len; + return (ret); } int rgb_to_int(char **rgb)