aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_detect.c
blob: 3bf40e314fd26de35bd0dbcba98c4e4add8dae19 (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_detect.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:22:11 by joelecle          #+#    #+#             */
/*   Updated: 2020/02/14 17:23:42 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <math.h>

void
	ft_detection_init_y(t_cub *cl)
{
	cl->rlist.y_delta_dist = sqrt(1 + (cl->rlist.y_ray_dir *
		cl->rlist.y_ray_dir) / (cl->rlist.x_ray_dir *
		cl->rlist.x_ray_dir));
	if (cl->rlist.x_ray_dir < 0)
	{
		cl->mlist.y_step = -1;
		cl->rlist.y_side_dist = (cl->rlist.x_ray_pos -
			cl->rlist.sqx) * cl->rlist.y_delta_dist;
	}
	else
	{
		cl->mlist.y_step = 1;
		cl->rlist.y_side_dist = (cl->rlist.sqx + 1.0 -
			cl->rlist.x_ray_pos) * cl->rlist.y_delta_dist;
	}
}

void
	ft_detection_init_x(t_cub *cl)
{
	cl->rlist.x_delta_dist = sqrt(1 + (cl->rlist.x_ray_dir *
		cl->rlist.x_ray_dir) / (cl->rlist.y_ray_dir *
		cl->rlist.y_ray_dir));
	if (cl->rlist.y_ray_dir < 0)
	{
		cl->mlist.x_step = -1;
		cl->rlist.x_side_dist = (cl->rlist.y_ray_pos -
			cl->rlist.sqy) * cl->rlist.x_delta_dist;
	}
	else
	{
		cl->mlist.x_step = 1;
		cl->rlist.x_side_dist = (cl->rlist.sqy + 1.0 -
			cl->rlist.y_ray_pos) * cl->rlist.x_delta_dist;
	}
}

void
	ft_detect(t_cub *cl)
{
	ft_detection_init_x(cl);
	ft_detection_init_y(cl);
	cl->rlist.hit = 0;
	while (cl->rlist.hit == 0)
	{
		if (cl->rlist.x_side_dist < cl->rlist.y_side_dist)
		{
			cl->rlist.x_side_dist += cl->rlist.x_delta_dist;
			cl->rlist.sqy += cl->mlist.x_step;
			cl->rlist.side = 0;
		}
		else
		{
			cl->rlist.y_side_dist += cl->rlist.y_delta_dist;
			cl->rlist.sqx += cl->mlist.y_step;
			cl->rlist.side = 1;
		}
		if (ft_ischarset(FT_CHRST_DETECT,
				cl->mlist.map[cl->rlist.sqy][cl->rlist.sqx]))
			cl->rlist.hit = 1;
	}
}