eqemu

view src/bvol.cc @ 12:2656099aff12

added copyright notices and license
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Jul 2014 07:04:21 +0300
parents 3d3656360a82
children
line source
1 /*
2 eqemu - electronic queue system emulator
3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>,
4 Eleni-Maria Stea <eleni@mutantstargoat.com>
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.
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.
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"
22 BSphere::BSphere()
23 {
24 radius = 1.0f;
25 }
27 BSphere::BSphere(const Vector3 &c, float rad)
28 : center(c)
29 {
30 radius = rad;
31 }
33 void BSphere::set_center(const Vector3 &center)
34 {
35 this->center = center;
36 }
38 const Vector3 &BSphere::get_center() const
39 {
40 return center;
41 }
43 void BSphere::set_radius(float rad)
44 {
45 radius = rad;
46 }
48 float BSphere::get_radius() const
49 {
50 return radius;
51 }
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;
60 float disc = b * b - 4.0f * a * c;
61 if(disc < 1e-6) {
62 return false;
63 }
65 float sqrt_disc = sqrt(disc);
66 float x1 = (-b + sqrt_disc) / (2.0f * a);
67 float x2 = (-b - sqrt_disc) / (2.0f * a);
69 if(x1 < 1e-6) x1 = x2;
70 if(x2 < 1e-6) x2 = x1;
72 float t = x1 < x2 ? x1 : x2;
73 if(t < 1e-6) {
74 return false;
75 }
77 hit->t = t;
78 hit->pos = ray.origin + ray.dir * t;
79 return true;
80 }