initial commit, eq circuit emulator
[eqemu] / src / bvol.cc
1 /*
2 eqemu - electronic queue system emulator
3 Copyright (C) 2014  John Tsiombikas <nuclear@member.fsf.org>,
4                     Eleni-Maria Stea <eleni@mutantstargoat.com>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <math.h>
20 #include "bvol.h"
21
22 BSphere::BSphere()
23 {
24         radius = 1.0f;
25 }
26
27 BSphere::BSphere(const Vector3 &c, float rad)
28         : center(c)
29 {
30         radius = rad;
31 }
32
33 void BSphere::set_center(const Vector3 &center)
34 {
35         this->center = center;
36 }
37
38 const Vector3 &BSphere::get_center() const
39 {
40         return center;
41 }
42
43 void BSphere::set_radius(float rad)
44 {
45         radius = rad;
46 }
47
48 float BSphere::get_radius() const
49 {
50         return radius;
51 }
52
53 bool BSphere::intersect(const Ray &ray, HitPoint *hit) const
54 {
55         float a = dot(ray.dir, ray.dir);
56         float b = 2.0 * dot(ray.dir, ray.origin - center);
57         float c = dot(ray.origin, ray.origin) + dot(center, center) -
58                 2.0 * dot(ray.origin, center) - radius * radius;
59
60         float disc = b * b - 4.0f * a * c;
61         if(disc < 1e-6) {
62                 return false;
63         }
64
65         float sqrt_disc = sqrt(disc);
66         float x1 = (-b + sqrt_disc) / (2.0f * a);
67         float x2 = (-b - sqrt_disc) / (2.0f * a);
68
69         if(x1 < 1e-6) x1 = x2;
70         if(x2 < 1e-6) x2 = x1;
71
72         float t = x1 < x2 ? x1 : x2;
73         if(t < 1e-6) {
74                 return false;
75         }
76
77         hit->t = t;
78         hit->pos = ray.origin + ray.dir * t;
79         return true;
80 }