-
Notifications
You must be signed in to change notification settings - Fork 0
/
raycats.c
94 lines (85 loc) · 2.62 KB
/
raycats.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycats.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yaalaoui <yaalaoui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/14 17:33:00 by yaalaoui #+# #+# */
/* Updated: 2020/10/28 12:44:28 by yaalaoui ### ########.fr */
/* */
/* ************************************************************************** */
#include "lib.h"
#include "ray.h"
int iswall(double x, double y, t_mapdata *map)
{
int mapy;
int mapx;
mapx = floor(x / (double)g_tiles);
mapy = floor(y / (double)g_tiles);
if (mapx >= 0 && mapx < PP && mapy >= 0 && mapy < g_rows)
return (MAP2D[mapy][mapx] == '1');
return (0);
}
void rayfacing(t_horizontal *it)
{
FCEDOWN = ((ARC > 0) && (ARC < M_PI));
FCEUP = !FCEDOWN;
FCERIGHT = ((ARC < M_PI_2) || (ARC > (1.5 * M_PI)));
FCELEFT = !FCERIGHT;
}
int inwin(double x, double y, t_mapdata *map)
{
return (((x >= 0) && (x <= g_tiles * PP))
&& ((y >= 0) && (y <= g_rows * g_tiles)));
}
double ft_horizontal(t_mapdata *map, t_horizontal *it)
{
double ax;
double ay;
double xstep;
double ystep;
double h;
h = 0;
ay = floor(PY / g_tiles) * g_tiles + (g_tiles * FCEDOWN);
ax = PX + ((ay - PY) / tan(ARC));
ystep = g_tiles * (FCEUP ? -1 : 1);
xstep = g_tiles / tan(ARC);
xstep *= (FCELEFT && xstep > 0) ? -1 : 1;
xstep *= (FCERIGHT && xstep < 0) ? -1 : 1;
if (FCEUP)
h++;
while (inwin(ax, ay - h, map) && !iswall(ax, ay - h, map))
{
ax += xstep;
ay += ystep;
}
WALLHX = ax;
WALLHY = ay;
return (sqrtf((ay - PY) * (ay - PY) + (ax - PX) * (ax - PX)));
}
double ft_vertical(t_mapdata *map, t_horizontal *it)
{
double ax;
double ay;
double xstep;
double ystep;
double hu;
hu = 0;
ax = floor(PX / g_tiles) * g_tiles + (g_tiles * FCERIGHT);
ay = PY + ((ax - PX) * tan(ARC));
xstep = g_tiles * (FCELEFT ? -1 : 1);
ystep = g_tiles * tan(ARC);
ystep *= (FCEUP && (ystep > 0)) ? -1 : 1;
ystep *= (FCEDOWN && (ystep < 0)) ? -1 : 1;
if (FCELEFT)
hu++;
while (inwin(ax - hu, ay, map) && !iswall(ax - hu, ay, map))
{
ax += xstep;
ay += ystep;
}
WALLVX = ax;
WALLVY = ay;
return (sqrtf((ay - PY) * (ay - PY) + (ax - PX) * (ax - PX)));
}