Browse Source

fix: camPlan not sync with playerDir

master
narnaud 3 years ago
parent
commit
630adb62f1
  1. 4
      includes/cub3d.h
  2. 7
      sources/hooks.c
  3. 6
      sources/parsing.c
  4. 13
      sources/utils.c

4
includes/cub3d.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

7
sources/hooks.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

6
sources/parsing.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

13
sources/utils.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

Loading…
Cancel
Save