Skip to content

Commit 718e978

Browse files
committed
changed walking to be stored in a double to allow for precise forward backward walking
1 parent 52ad2d8 commit 718e978

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

include/cub3d.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* cub3d.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: mgraf <mgraf@student.42berlin.de> +#+ +:+ +#+ */
6+
/* By: fheld <fheld@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/09/19 15:48:35 by mgraf #+# #+# */
9-
/* Updated: 2023/10/16 16:39:38 by mgraf ### ########.fr */
9+
/* Updated: 2023/10/18 22:26:40 by fheld ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -55,16 +55,35 @@
5555
# define SKY_BLUE 0x87CEEBAA
5656
# define MUD_BROWN 0x553F23AA
5757

58+
/**
59+
* The struct contains two ints that mean the x/y position on a plane
60+
*/
61+
typedef struct s_int_p2
62+
{
63+
int x;
64+
int y;
65+
} t_int_p2;
66+
67+
/**
68+
* The struct contains two doubles that mean the x/y position on a plane
69+
*/
70+
typedef struct s_p2
71+
{
72+
double x;
73+
double y;
74+
} t_p2;
75+
5876
/**
5977
* Player is intialized at starting position with view direction (N, S, E or W)
6078
* TBD: in degrees or radians ???
6179
* later these values update to the current position and view direction
6280
*/
6381
typedef struct s_player
6482
{
65-
int x;
66-
int y;
67-
int dir;
83+
int x;
84+
int y;
85+
int dir;
86+
t_p2 double_pos;
6887
} t_player;
6988

7089
/**
@@ -143,15 +162,6 @@ typedef struct s_data
143162
t_mlx42 mlx42;
144163
} t_data;
145164

146-
/**
147-
* The struct contains two ints that mean the x/y position on a plane
148-
*/
149-
typedef struct s_int_p2
150-
{
151-
int x;
152-
int y;
153-
} t_int_p2;
154-
155165
/**
156166
* holds two doubles repesenting x and y coorinates
157167
*/

srcs/move/move_player.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* move_player.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: mgraf <mgraf@student.42berlin.de> +#+ +:+ +#+ */
6+
/* By: fheld <fheld@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/10/01 22:57:28 by fheld #+# #+# */
9-
/* Updated: 2023/10/14 16:45:45 by mgraf ### ########.fr */
9+
/* Updated: 2023/10/18 22:37:43 by fheld ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -48,26 +48,38 @@ int valid_pos(t_data *data, t_int_p2 pos)
4848
void move_forward(t_data *data)
4949
{
5050
t_int_p2 end_pos;
51+
t_p2 end_pos_d;
5152

52-
end_pos.x = data->start.x - (5.0 * sin(data->start.dir / 180.0 * M_PI));
53-
end_pos.y = data->start.y - (5.0 * cos(data->start.dir / 180.0 * M_PI));
53+
end_pos_d.x = data->start.double_pos.x - (5.0 * \
54+
sin(data->start.dir / 180.0 * M_PI));
55+
end_pos_d.y = data->start.double_pos.y - (5.0 * \
56+
cos(data->start.dir / 180.0 * M_PI));
57+
end_pos.x = (int)end_pos_d.x;
58+
end_pos.y = (int)end_pos_d.y;
5459
if (valid_pos(data, end_pos) == 1)
5560
{
5661
data->start.x = end_pos.x;
5762
data->start.y = end_pos.y;
63+
data->start.double_pos = end_pos_d;
5864
}
5965
}
6066

6167
void move_backward(t_data *data)
6268
{
6369
t_int_p2 end_pos;
70+
t_p2 end_pos_d;
6471

65-
end_pos.x = data->start.x + (5.0 * sin(data->start.dir / 180.0 * M_PI));
66-
end_pos.y = data->start.y + (5.0 * cos(data->start.dir / 180.0 * M_PI));
72+
end_pos_d.x = data->start.double_pos.x + (5.0 * \
73+
sin(data->start.dir / 180.0 * M_PI));
74+
end_pos_d.y = data->start.double_pos.y + (5.0 * \
75+
cos(data->start.dir / 180.0 * M_PI));
76+
end_pos.x = (int)end_pos_d.x;
77+
end_pos.y = (int)end_pos_d.y;
6778
if (valid_pos(data, end_pos) == 1)
6879
{
6980
data->start.x = end_pos.x;
7081
data->start.y = end_pos.y;
82+
data->start.double_pos = end_pos_d;
7183
}
7284
}
7385

@@ -76,14 +88,10 @@ void move_player(void *arg)
7688
t_data *data;
7789

7890
data = arg;
79-
if (mlx_is_key_down(data->mlx42.mlx_ptr, MLX_KEY_D))
80-
move_right(data);
81-
if (mlx_is_key_down(data->mlx42.mlx_ptr, MLX_KEY_A))
82-
move_left(data);
8391
if (mlx_is_key_down(data->mlx42.mlx_ptr, MLX_KEY_W))
84-
move_up(data);
92+
move_forward(data);
8593
if (mlx_is_key_down(data->mlx42.mlx_ptr, MLX_KEY_S))
86-
move_down(data);
94+
move_backward(data);
8795
if (mlx_is_key_down(data->mlx42.mlx_ptr, MLX_KEY_LEFT))
8896
data->start.dir = (data->start.dir + 2) % 360;
8997
if (mlx_is_key_down(data->mlx42.mlx_ptr, MLX_KEY_RIGHT))

srcs/render/render_create.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* render_create.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: mgraf <mgraf@student.42berlin.de> +#+ +:+ +#+ */
6+
/* By: fheld <fheld@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/10/14 16:50:19 by mgraf #+# #+# */
9-
/* Updated: 2023/10/14 18:51:13 by mgraf ### ########.fr */
9+
/* Updated: 2023/10/18 22:38:32 by fheld ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -103,6 +103,8 @@ int create_image_player(t_data *data)
103103
(data->start.x - data->dim.min_x) * SPRITE_SIZE + (SPRITE_SIZE / 2);
104104
data->start.y = \
105105
(data->start.y - data->dim.min_y) * SPRITE_SIZE + (SPRITE_SIZE / 2);
106+
data->start.double_pos = \
107+
(t_p2){(double)data->start.x, (double)data->start.y};
106108
data->mlx42.mm_player_img = \
107109
mlx_new_image(data->mlx42.mlx_ptr, WINDOW_WIDTH, WINDOW_HEIGHT);
108110
if (data->mlx42.mm_player_img == NULL)

0 commit comments

Comments
 (0)