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/>.
27 BSphere::BSphere(const Vector3 &c, float rad)
33 void BSphere::set_center(const Vector3 ¢er)
35 this->center = center;
38 const Vector3 &BSphere::get_center() const
43 void BSphere::set_radius(float rad)
48 float BSphere::get_radius() const
53 bool BSphere::intersect(const Ray &ray, HitPoint *hit) const
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;
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;
78 hit->pos = ray.origin + ray.dir * t;