X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2Fgeometry.cpp;fp=src%2Fgeometry%2Fgeometry.cpp;h=1d7beeb20777747d500bbebf3e6a1300f60f1fee;hb=48d34439f3d5bc8bebabe9f0ee35970359e61bfa;hp=002c68f0d1ba69760b65d41049e069a2ef2d490a;hpb=bda41b98427c8d34f954dae0dcaf261c5ad6cb43;p=gong.git diff --git a/src/geometry/geometry.cpp b/src/geometry/geometry.cpp index 002c68f..1d7beeb 100644 --- a/src/geometry/geometry.cpp +++ b/src/geometry/geometry.cpp @@ -302,6 +302,31 @@ std::ostream &operator <<(std::ostream &out, const Sphere &s) { return out << "Sphere(" << s.origin << ", " << s.radius << ')'; } +bool Intersection( + const Sphere &a, + const Sphere &b, + float &dist, + glm::vec3 &norm +) noexcept { + glm::vec3 diff(b.origin - a.origin); + float dist2 = glm::length2(diff); + if (dist2 < std::numeric_limits::epsilon()) { + // origins coincide, use smaller of the diameters for + // depth and pick arbitrary normal + dist = 2.0f * std::min(a.radius, b.radius); + norm = glm::vec3(1.0f, 0.0f, 0.0f); + return true; + } + if (dist2 < (a.radius + b.radius) * (a.radius + b.radius)) { + dist = std::sqrt(dist2); + norm = diff / dist; + dist = a.radius - (dist - b.radius); + return true; + } else { + return false; + } +} + bool Intersection( const Sphere &sphere, const Plane &plane,