-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_horizontal.c
104 lines (95 loc) · 2.81 KB
/
check_horizontal.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_horizontal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mlegendr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/06 10:52:11 by mlegendr #+# #+# */
/* Updated: 2014/12/08 16:59:52 by mlegendr ### ########.fr */
/* */
/* ************************************************************************** */
#include "wolf3d.h"
#include <math.h>
static double get_dist(double angle, t_player player, int wall_y)
{
double dist;
if (angle < M_PI / 2.0f)
dist = fabs(wall_y - player.y) * tan(M_PI / 2.0f - angle);
else if (angle < M_PI)
dist = fabs(wall_y - player.y) * tan(angle - M_PI / 2.0f);
else if (angle < M_PI * 1.5f)
dist = fabs(wall_y - player.y) * tan(M_PI * 1.5f - angle);
else
dist = fabs(wall_y - player.y) * tan(angle - M_PI * 1.5f);
if (angle > M_PI / 2.0f && angle < M_PI * 1.5f)
dist *= -1;
return (dist);
}
static int wall_bot(t_map wall, t_inter *inter, int *wall_y,
t_map_info info)
{
inter->from = kBot;
if (*wall_y < info.height && wall.wall_type != 0)
{
inter->wall = wall;
return (1);
}
++(*wall_y);
return (0);
}
static int wall_top(t_map wall, t_inter *inter, int *wall_y)
{
inter->from = kTop;
if (*wall_y > 0 && wall.wall_type != 0)
{
inter->wall = wall;
return (1);
}
--(*wall_y);
return (0);
}
static t_inter check_each_line(double angle, t_player player, t_map_info info,
int *wall_y)
{
double dist;
t_inter inter;
int wall_x;
inter.dist = -1;
while (1)
{
dist = get_dist(angle, player, *wall_y);
wall_x = floor(player.x + dist);
if (wall_x < 0 || wall_x > info.width + 1)
return (inter);
if (angle > M_PI)
{
if (wall_bot(info.map[*wall_y][wall_x], &inter, wall_y, info))
break ;
}
else if (wall_top(info.map[*wall_y - 1][wall_x], &inter, wall_y))
{
break ;
}
}
inter.dist = dist;
return (inter);
}
t_inter check_horizontal(double angle, t_player player, t_map_info info)
{
int wall_y;
t_inter inter;
inter.dist = -1;
if (angle < M_PI)
wall_y = floor(player.y);
else
wall_y = ceil(player.y);
inter = check_each_line(angle, player, info, &wall_y);
if (inter.dist == -1)
return (inter);
inter.x = player.x + inter.dist;
inter.y = wall_y;
inter.dist = sqrt((inter.dist * inter.dist) + (wall_y - player.y)
* (wall_y - player.y));
return (inter);
}