-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraycasting.c
109 lines (100 loc) · 3.43 KB
/
raycasting.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
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycasting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aboncine <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 15:49:34 by aboncine #+# #+# */
/* Updated: 2023/02/22 16:45:31 by aboncine ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
static void ft_get_direction(t_cub3d *box, t_ray *ray)
{
if (ray->raycos < 0)
ray->raycos *= -1;
if (ray->raysin < 0)
ray->raysin *= -1;
if (box->parsed_map[(int)
((ray->currenty) - ray->raysin)][(int)ray->currentx] != '1')
ray->direction = 0;
else if (box->parsed_map[(int)
((ray->currenty) + ray->raysin)][(int)ray->currentx] != '1')
ray->direction = 1;
else if (box->parsed_map[(int)
(ray->currenty)][(int)((ray->currentx) + ray->raycos)] != '1')
ray->direction = 2;
else if (box->parsed_map[(int)
(ray->currenty)][(int)((ray->currentx) - ray->raycos)] != '1')
ray->direction = 3;
}
static t_column ft_do_column(t_cub3d *box, t_ray ray, int raycount)
{
t_column res;
res.sky_start = 0;
res.sky_end = box->half_h - ray.wallheight;
if (res.sky_end < 0)
res.sky_end = 0;
res.sky_color = box->sky_color;
res.wall_start = box->half_h - ray.wallheight;
if (res.wall_start < 0)
res.wall_start = 0;
res.wall_end = box->half_h + ray.wallheight;
if (res.wall_end >= box->height)
res.wall_end = box->height;
res.direction = ray.direction;
res.floor_start = box->half_h + ray.wallheight;
if (res.floor_start >= box->height)
res.floor_start = box->height;
res.floor_end = box->height;
res.colnbr = raycount;
res.currentx = ray.currentx;
res.currenty = ray.currenty;
res.floor_color = box->floor_color;
return (res);
}
static void ft_init_ray(t_cub3d *box, t_ray *ray)
{
ray->currentx = box->player_x;
ray->currenty = box->player_y;
ray->raycos = cos(ft_degrees_to_radiants
(ray->ray_angle)) / box->rc_precision;
ray->raysin = sin(ft_degrees_to_radiants
(ray->ray_angle)) / box->rc_precision;
}
static void ft_raycasting2(t_cub3d *box, t_ray *ray)
{
int wall;
wall = 48;
ft_init_ray(box, ray);
while (wall == 48 || ft_is_direction(wall) == 1)
{
ray->currentx += ray->raycos;
ray->currenty += ray->raysin;
wall = box->parsed_map
[(int)floor(ray->currenty)][(int)floor(ray->currentx)];
}
}
void ft_raycasting(t_cub3d *box)
{
int raycount;
t_ray ray;
t_column currentcol;
raycount = 0;
ray.ray_angle = box->angle - box->half_fov;
while (raycount < box->width)
{
ft_raycasting2(box, &ray);
ray.dist = sqrt(pow(box->player_x - ray.currentx, 2)
+ pow(box->player_y - ray.currenty, 2));
ray.dist = ray.dist * cos(ft_degrees_to_radiants
(ray.ray_angle - box->angle));
ray.wallheight = floor(box->half_h / ray.dist);
ft_get_direction(box, &ray);
currentcol = ft_do_column(box, ray, raycount);
ft_render_full_column(box, box->img, currentcol);
ray.ray_angle += box->rc_incrementing;
raycount++;
}
}