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