moving.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* moving.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: ll-hotel <ll-hotel@student.42.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2024/08/21 01:38:28 by ll-hotel #+# #+# */ 9 /* Updated: 2024/10/25 14:32:57 by ll-hotel ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "cub3D.h" 14 #include "vec2f.h" 15 #include "mlx.h" 16 #include <math.h> 17 18 static void turn_player(t_cube *cube, t_player *player); 19 static void walk_player(t_cube *cube, t_player *player); 20 static void strafe_player(t_cube *cube, t_player *player); 21 22 void move_player(t_cube *cube, t_player *player) 23 { 24 if (player->turning != 0) 25 turn_player(cube, player); 26 if (player->walking != 0) 27 walk_player(cube, player); 28 if (player->strafing != 0) 29 strafe_player(cube, player); 30 } 31 32 void rotation(t_player *player, float coef) 33 { 34 float cam_x; 35 float cam_y; 36 37 player->axis += coef; 38 if (player->axis < 0) 39 player->axis += 2.f * PI; 40 else if (player->axis > 2.f * PI) 41 player->axis -= 2.f * PI; 42 player->dir.x = cosf(player->axis); 43 player->dir.y = -sinf(player->axis); 44 cam_x = player->camera.x; 45 cam_y = player->camera.y; 46 player->camera.x = cam_x * cosf(coef) + cam_y * sinf(coef); 47 player->camera.y = cam_x * -sinf(coef) + cam_y * cosf(coef); 48 } 49 50 void turn_player(t_cube *cube, t_player *player) 51 { 52 float coef; 53 54 if (cube->player.use_pointer) 55 coef = turn_with_mouse(cube, player); 56 else 57 coef = 0.2 * player->turning; 58 rotation(player, coef); 59 } 60 61 void walk_player(t_cube *cube, t_player *player) 62 { 63 t_vec2f step; 64 float axis; 65 66 axis = player->axis + PI * (player->walking == -1); 67 if (axis > 2 * PI) 68 axis -= 2 * PI; 69 else if (axis < 0) 70 axis += 2 * PI; 71 step = vec2f_scal(vec2f(cosf(axis), -sinf(axis)), 0.1f); 72 collision_correction(cube, player, &step); 73 player->pos = vec2f_add(step, player->pos); 74 } 75 76 void strafe_player(t_cube *cube, t_player *player) 77 { 78 t_vec2f step; 79 float axis; 80 81 axis = player->axis + PI * 0.5 * player->strafing; 82 if (axis > 2 * PI) 83 axis -= 2 * PI; 84 else if (axis < 0) 85 axis += 2 * PI; 86 step = vec2f_scal(vec2f(cosf(axis), -sinf(axis)), 0.1f); 87 collision_correction(cube, player, &step); 88 player->pos = vec2f_add(step, player->pos); 89 }