-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.cpp
26 lines (24 loc) · 858 Bytes
/
sphere.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
#include "sphere.h"
namespace Tracer {
std::pair<bool, Vector3> Sphere::intersects(const Vector3& point,
const Vector3& dir) const {
Vector3 diff = point - center_position;
float dot_product = dir.dot(diff);
float delta = (std::pow(dot_product, 2)) -
(diff.magnitude_squared() - std::pow(radius, 2));
if (delta < 0) {
return {false, Vector3(0, 0, 0)};
}
// we assume that the sphere is not behind you, therefore distance we are
// looking for is the (-) delta, not (+) delta (the more negative side of the
// sphere)
float d = -dot_product - sqrt(delta);
assert(d >= 0);
return {true, point + dir * d};
}
Vector3 Sphere::get_normal(const Vector3& point) const {
auto normal = point - center_position;
normal.normalize();
return normal;
}
} // namespace Tracer