-
Notifications
You must be signed in to change notification settings - Fork 1
/
setting_ray.c
102 lines (93 loc) · 3.31 KB
/
setting_ray.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setting_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jaesjeon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/03 11:59:24 by jaemjeon #+# #+# */
/* Updated: 2022/11/12 09:51:19 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
#include <math.h>
void set_ray_step(t_vector2 *vec_pos, t_ray *ray, t_vector2 *step)
{
{
if (ray->cast_angle < 90 || ray->cast_angle >= 270)
step->x = 1;
else
step->x = -1;
if (ray->cast_angle < 180)
step->y = 1;
else
step->y = -1;
}
{
ray->hit_idx_x = floor(vec_pos->x);
ray->hit_idx_y = floor(vec_pos->y);
}
}
void set_dist(t_vector2 *pos, double cast_angle, \
t_vector2 *side_dist, t_vector2 *delta_dist)
{
const double radian_cast_angle = deg2rad(cast_angle);
delta_dist->x = 1 / fabs(cos(radian_cast_angle));
delta_dist->y = 1 / fabs(sin(radian_cast_angle));
if (cos(radian_cast_angle) >= 0)
side_dist->x = delta_dist->x * (ceil(pos->x) - pos->x);
else
side_dist->x = delta_dist->x * (pos->x - floor(pos->x));
if (sin(radian_cast_angle) >= 0)
side_dist->y = delta_dist->y * (ceil(pos->y) - pos->y);
else
side_dist->y = delta_dist->y * (pos->y - floor(pos->y));
}
double __get_abs_delta_len(t_vector2 *cast_pos, t_ray *ray)
{
if (ray->hit_wall_side == EA)
return (sqrt(pow(ray->ray_length, 2) - \
pow(cast_pos->x - ray->hit_point.x, 2)));
else if (ray->hit_wall_side == WE)
return (sqrt(pow(ray->ray_length, 2) - \
pow(ray->hit_point.x - cast_pos->x, 2)));
else if (ray->hit_wall_side == SO)
return (sqrt(pow(ray->ray_length, 2) - \
pow(cast_pos->y - ray->hit_point.y, 2)));
else
return (sqrt(pow(ray->ray_length, 2) - \
pow(ray->hit_point.y - cast_pos->y, 2)));
}
void set_ray_hit_point(t_vector2 *cast_pos, t_ray *ray)
{
double abs_delta_len;
if (ray->hit_wall_side == EA)
ray->hit_point.x = ray->hit_idx_x + 1;
else if (ray->hit_wall_side == WE)
ray->hit_point.x = ray->hit_idx_x;
else if (ray->hit_wall_side == SO)
ray->hit_point.y = ray->hit_idx_y + 1;
else
ray->hit_point.y = ray->hit_idx_y;
abs_delta_len = __get_abs_delta_len(cast_pos, ray);
if (ray->hit_wall_side == SO || ray->hit_wall_side == NO)
{
if (cos(deg2rad(ray->cast_angle)) > 0)
ray->hit_point.x = cast_pos->x + abs_delta_len;
else
ray->hit_point.x = cast_pos->x - abs_delta_len;
}
else
{
if (sin(deg2rad(ray->cast_angle)) > 0)
ray->hit_point.y = cast_pos->y + abs_delta_len;
else
ray->hit_point.y = cast_pos->y - abs_delta_len;
}
}
void set_wall_distance(t_player *player_info, t_ray *ray)
{
double delta_angle;
delta_angle = fabs(player_info->camera_angle_h - ray->cast_angle);
ray->wall_distance = cos(deg2rad(delta_angle)) * ray->ray_length;
}