From 770f3d5ec0518c41c63d4c136c4a9d87a4f3bc14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 31 Jul 2025 14:53:18 +0200 Subject: [PATCH] feat: rotation via deltatime + fix deltatime calculation --- src/consts.h | 6 +++--- src/main.c | 7 ++++--- src/player/angle.c | 40 ++++++++++++++++++++++------------------ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/consts.h b/src/consts.h index c54b73b..dd24edd 100644 --- a/src/consts.h +++ b/src/consts.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ -/* Updated: 2025/07/31 14:42:33 by tchampio ### ########.fr */ +/* Updated: 2025/07/31 15:02:18 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,8 @@ # define SIZE 64 # define MAP_SIZE 20 # define RESSOURCE_DIR "ressources" -# define MOVEMENT_SPEED 0.0001 -# define ROTATION_SPEED 0.01 +# define MOVEMENT_SPEED 0.000005 +# define ROTATION_SPEED 0.000002 # define PLANE_VALUE 0.66 # define TEXTURE_SIZE 64 # ifdef BONUS diff --git a/src/main.c b/src/main.c index 91c4a01..29083c8 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/31 13:57:08 by tchampio ### ########.fr */ +/* Updated: 2025/07/31 15:01:00 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int game_loop(t_cub3d_data *data) { t_ray ray; - data->delta = (get_milliseconds() - data->last_tick); + data->last_tick = get_milliseconds(); mlx_destroy_image(data->mlx, data->img_data->img); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); reset_matrix(data); @@ -44,7 +44,8 @@ int game_loop(t_cub3d_data *data) mlx_put_image_to_window(data->mlx, data->mlx_win, data->img_data->img, 0, 0); mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT); - data->last_tick = get_milliseconds(); + data->delta = (get_milliseconds() - data->last_tick); + ft_printf("%d,%d\n", data->last_tick, data->delta); return (0); } diff --git a/src/player/angle.c b/src/player/angle.c index b2b3cba..e1dda61 100644 --- a/src/player/angle.c +++ b/src/player/angle.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* angle.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/07/24 14:41:35 by tchampio #+# #+# */ -/* Updated: 2025/07/29 20:09:55 by tchampio ### ########.fr */ +/* Created: 2025/07/31 14:53:18 by kcolin #+# #+# */ +/* Updated: 2025/07/31 14:53:59 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,17 +22,19 @@ void turn_right_angle(t_cub3d_data *data) t_player *p; double prev_dir_x; double prev_plane_x; + double rotspeed; + rotspeed = ROTATION_SPEED * data->delta; p = &data->player; prev_dir_x = p->dir_x; prev_plane_x = p->plane_x; - p->dir_x = p->dir_x * cos(ROTATION_SPEED) - p->dir_y * sin(ROTATION_SPEED); - p->dir_y = prev_dir_x * sin(ROTATION_SPEED) - + p->dir_y * cos(ROTATION_SPEED); - p->plane_x = p->plane_x * cos(ROTATION_SPEED) - - p->plane_y * sin(ROTATION_SPEED); - p->plane_y = prev_plane_x * sin(ROTATION_SPEED) - + p->plane_y * cos(ROTATION_SPEED); + p->dir_x = p->dir_x * cos(rotspeed) - p->dir_y * sin(rotspeed); + p->dir_y = prev_dir_x * sin(rotspeed) + + p->dir_y * cos(rotspeed); + p->plane_x = p->plane_x * cos(rotspeed) + - p->plane_y * sin(rotspeed); + p->plane_y = prev_plane_x * sin(rotspeed) + + p->plane_y * cos(rotspeed); } void turn_left_angle(t_cub3d_data *data) @@ -40,16 +42,18 @@ void turn_left_angle(t_cub3d_data *data) t_player *p; double prev_dir_x; double prev_plane_x; + double rotspeed; + rotspeed = ROTATION_SPEED * data->delta; p = &data->player; prev_dir_x = p->dir_x; prev_plane_x = p->plane_x; - p->dir_x = p->dir_x * cos(-ROTATION_SPEED) - - p->dir_y * sin(-ROTATION_SPEED); - p->dir_y = prev_dir_x * sin(-ROTATION_SPEED) - + p->dir_y * cos(-ROTATION_SPEED); - p->plane_x = p->plane_x * cos(-ROTATION_SPEED) - - p->plane_y * sin(-ROTATION_SPEED); - p->plane_y = prev_plane_x * sin(-ROTATION_SPEED) - + p->plane_y * cos(-ROTATION_SPEED); + p->dir_x = p->dir_x * cos(-rotspeed) + - p->dir_y * sin(-rotspeed); + p->dir_y = prev_dir_x * sin(-rotspeed) + + p->dir_y * cos(-rotspeed); + p->plane_x = p->plane_x * cos(-rotspeed) + - p->plane_y * sin(-rotspeed); + p->plane_y = prev_plane_x * sin(-rotspeed) + + p->plane_y * cos(-rotspeed); }