]> git.localhorst.tv Git - gong.git/blobdiff - src/geometry/geometry.cpp
spheres
[gong.git] / src / geometry / geometry.cpp
index 8deed860dc0622825153fd07873240bcac91c0a1..1c1bfd61655bf8e2225d681c35984798f400da56 100644 (file)
@@ -217,6 +217,16 @@ std::ostream &operator <<(std::ostream &out, const Plane &plane) {
        return out << "Plane(" << plane.normal << ", " << plane.dist << ')';
 }
 
+float Distance(const glm::vec3 &point, const Plane &plane) {
+       return std::abs(SignedDistance(point, plane));
+}
+
+float SignedDistance(const glm::vec3 &point, const Plane &plane) {
+       return (
+               plane.A() * point.x + plane.B() * point.y + plane.C() * point.z + plane.D()
+       ) / glm::length(plane.normal);
+}
+
 std::ostream &operator <<(std::ostream &out, const Frustum &frustum) {
        return out << "Frustum(" << std::endl
                << "\tleft:   " << frustum.plane[0] << std::endl
@@ -277,6 +287,35 @@ bool CullTest(const AABB &box, const Frustum &frustum) noexcept {
        }
        return false;
 }
+
+std::ostream &operator <<(std::ostream &out, const Sphere &s) {
+       return out << "Sphere(" << s.origin << ", " << s.radius << ')';
+}
+
+bool Intersection(
+       const Sphere &sphere,
+       const Plane &plane,
+       float &dist,
+       glm::vec3 &norm
+) noexcept {
+       float sig_dist = SignedDistance(sphere.origin, plane);
+       dist = sphere.radius - std::abs(sig_dist);
+       if (dist > 0.0f) {
+               norm = sig_dist > 0.0f ? plane.normal : -plane.normal;
+               return true;
+       } else {
+               return false;
+       }
+}
+
+bool Intersection(
+       const Sphere &sphere,
+       const Plane &plane,
+       float &dist
+) noexcept {
+       dist = sphere.radius - SignedDistance(sphere.origin, plane);
+       return dist > 0.0f;
 }
 
 }
+}