-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere_collision.c
77 lines (69 loc) · 2.09 KB
/
sphere_collision.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
/*
** sphere_collision.c for sphere_collision in /home/nicolas/Modules/gfx_raytracer1
**
** Made by menett_a
** Login <[email protected]>
**
** Started on Tue Mar 1 23:31:49 2016 menett_a
** Last update Sat Mar 12 16:39:57 2016 menett_a
*/
#include <lapin.h>
#include <math.h>
#include "struct.h"
#include "proto.h"
void sphere_norm(t_loop *data, int i)
{
data->norm.n_x = data->collision.pos_x - data->info.sphe[i].pos_x;
data->norm.n_y = data->collision.pos_y - data->info.sphe[i].pos_y;
data->norm.n_z = data->collision.pos_z - data->info.sphe[i].pos_z;
}
void save_collision(t_loop *data, int i, double x)
{
double dist;
calcul_pos(data, x, &dist);
if (dist < data->collision.dist || data->collision.dist == 0)
{
data->collision.dist = dist;
sphere_norm(data, i);
data->collision.color = data->info.sphe[i].color;
data->collision.is_collision = 1;
}
}
void x_two(t_loop *data, int i)
{
if (data->collision.x < data->collision._x
&& data->collision.x > 0.01)
save_collision(data, i, data->collision.x);
else if (data->collision._x < data->collision.x
&& data->collision._x > 0.01)
save_collision(data, i, data->collision._x);
}
void calcul_sphere(t_loop *data, t_info *sphe, int i)
{
data->delta.a =
pow(data->ray.vec_x, 2) + pow(data->ray.vec_y, 2) +
pow(data->ray.vec_z, 2);
data->delta.b =
2 * (data->pos_x - sphe->sphe[i].pos_x) * data->ray.vec_x +
2 * (data->pos_y - sphe->sphe[i].pos_y) * data->ray.vec_y +
2 * (data->pos_z - sphe->sphe[i].pos_z) * data->ray.vec_z;
data->delta.c =
pow(data->pos_x - sphe->sphe[i].pos_x, 2)
+ pow(data->pos_y - sphe->sphe[i].pos_y, 2)
+ pow(data->pos_z - sphe->sphe[i].pos_z, 2) - pow(sphe->sphe[i].radius, 2);
data->delta.delta = pow(data->delta.b, 2) - 4 * data->delta.a * data->delta.c;
if (find_delta(data) == 2)
x_two(data, i);
}
void sphere_collision(t_loop *data, t_info *sphe)
{
int i;
i = 0;
while (i < sphe->nb_sphere)
{
calcul_sphere(data, sphe, i);
if (data->collision.is_collision == 1)
find_light(data);
i = i + 1;
}
}