-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCylinder.cpp
47 lines (34 loc) · 828 Bytes
/
Cylinder.cpp
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
#include "Cylinder.h"
#include <math.h>
float Cylinder::intersect(glm::vec3 pos, glm::vec3 dir)
{
float a = (dir.x * dir.x) + (dir.z * dir.z);
float b = 2 * (dir.x * (pos.x-middle.x) + dir.z * (pos.z-middle.z));
float c = (pos.x - middle.x) * (pos.x - middle.x) + (pos.z - middle.z) * (pos.z - middle.z) - (rad*rad);
float delta = b*b - 4*(a*c);
if (delta < 0.0 || (fabs(delta)) < 0.001) {
return -1.0;
}
float t1 = (-b - sqrt(delta))/(2*a);
float t2 = (-b + sqrt(delta))/(2*a);
float t;
if (t1 <= t2) {
t = t1;
}
else {
t = t2;
}
float r = pos.y + t*dir.y;
if (r >= middle.y && r <= middle.y + height) {
return t;
}
else {
return -1;
}
}
glm::vec3 Cylinder::normal(glm::vec3 p)
{
glm::vec3 n = glm::vec3(p.x-middle.x, 0, p.z-middle.z);
n = glm::normalize(n);
return n;
}