-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall_casting.c
93 lines (85 loc) · 2.94 KB
/
wall_casting.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wall_casting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kmahdi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/01 21:04:25 by kmahdi #+# #+# */
/* Updated: 2023/07/10 02:46:19 by kmahdi ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/cub3d.h"
int collision(t_data *data, double y, double x)
{
int x1[2];
int y1[2];
y1[0] = ((y - 8) / TILE_SIZE);
y1[1] = ((y + 8) / TILE_SIZE);
x1[0] = ((x - 8) / TILE_SIZE);
x1[1] = ((x + 8) / TILE_SIZE);
if (y <= 0 || y >= (data->map->height * TILE_SIZE)
|| x <= 0 || x >= (data->map->width * TILE_SIZE))
return (0);
else if (data->map->map[y1[0]][x1[0]] == '1')
return (0);
else if (data->map->map[y1[1]][x1[1]] == '1')
return (0);
else if (data->map->map[y1[0]][x1[1]] == '1')
return (0);
else if (data->map->map[y1[1]][x1[0]] == '1')
return (0);
else
return (1);
}
int hit_wall(t_data *data, double y, double x)
{
int y1;
int x1;
y1 = (y / TILE_SIZE);
x1 = (x / TILE_SIZE);
if (!data->map->map || !data->map->map[y1] || !data->map->map[y1][x1])
return (0);
if (y < 0 || y > data->map->height * TILE_SIZE || x < 0
|| x > data->map->width * TILE_SIZE)
return (0);
if ((data->map->map[y1][x1] != '\0' && data->map->map[y1][x1] == '1'))
return (0);
return (1);
}
void cast_rays(t_data *data)
{
int i;
double ray_angle;
double hight_wall_hit;
double camera_len;
ray_angle = data->player.rotation_angle - (data->fov / 2);
i = 0;
while (i < WIDTH)
{
data->rays[i] = malloc(sizeof(t_rey));
if (!data->rays[i])
return ;
cast_single_ray(data, ray_angle, i);
data->rays[i]->distance = data->rays[i]->distance
* cos(data->rays[i]->ray_angle - data->player.rotation_angle);
camera_len = (WIDTH / 2) / tan(data->fov / 2);
hight_wall_hit = (TILE_SIZE / data->rays[i]->distance) * camera_len;
data->textures->hight_wall_text = hight_wall_hit;
draw_3d_map(i, data);
ray_angle += (data->fov / WIDTH);
free(data->rays[i]);
i++;
}
}
void render(t_data *data)
{
data->img->img_ptr = mlx_new_image(data->mlx_ptr, WIDTH, HEIGHT);
data->img->addr = mlx_get_data_addr(data->img->img_ptr,
&data->img->bits_per_pixel, &data->img->line_length,
&data->img->endian);
cast_rays(data);
mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, data->img->img_ptr, 0,
0);
mlx_destroy_image(data->mlx_ptr, data->img->img_ptr);
}