-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcub3d_player_movement.c
executable file
·74 lines (68 loc) · 2.69 KB
/
cub3d_player_movement.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d_player_movement.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mg <mg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/30 21:40:25 by mg #+# #+# */
/* Updated: 2020/10/07 21:35:16 by mg ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
/*
** As grid array x, y coordinate are stored inversed
** lower left (x.min, y.max) the following changes are made accordingly
*/
void c3d_player_next_position(t_param *cub3d, int direction)
{
cub3d->next.angle = cub3d->player.angle;
if (direction == FORWARD)
cub3d->next.angle -= 0;
else if (direction == BACKWARD)
cub3d->next.angle += M_PI;
else if (direction == LEFT)
cub3d->next.angle += M_PI_2;
else if (direction == RIGHT)
cub3d->next.angle -= M_PI_2;
c3d_player_normalize_next_angle(cub3d);
c3d_player_next_position_wall_check(cub3d);
c3d_player_position_sprite_check(cub3d);
}
void c3d_player_normalize_next_angle(t_param *cub3d)
{
if (cub3d->next.angle < 0)
cub3d->next.angle = (2 * M_PI) + cub3d->next.angle;
else if (cub3d->next.angle > (2 * M_PI))
cub3d->next.angle = cub3d->next.angle - (2 * M_PI);
}
void c3d_player_next_position_wall_check(t_param *cub3d)
{
cub3d->next.x = cub3d->player.x +
(cub3d->map.step) * cos(cub3d->next.angle);
cub3d->next.y = cub3d->player.y -
(cub3d->map.step) * sin(cub3d->next.angle);
if (cub3d->next.x > 0 && cub3d->next.x < cub3d->map.width &&
cub3d->next.y > 0 && cub3d->next.y < cub3d->map.height &&
!c3d_player_wall_collusion(cub3d, cub3d->next.x, cub3d->next.y))
{
if (!c3d_player_wall_collusion(cub3d,
cub3d->next.x + (cub3d->map.step - 2) * cos(cub3d->next.angle),
cub3d->next.y - (cub3d->map.step - 2) * sin(cub3d->next.angle)))
{
cub3d->player.x = cub3d->next.x;
cub3d->player.y = cub3d->next.y;
cub3d->window.render = 1;
}
}
}
void c3d_player_position_sprite_check(t_param *cub3d)
{
if (c3d_player_sprite_collusion(cub3d, cub3d->player.x, cub3d->player.y))
{
cub3d->map.grid
[(int)(cub3d->player.y / cub3d->map.tile_size)]
[(int)(cub3d->player.x / cub3d->map.tile_size)] = 0;
cub3d->window.render = 1;
}
}