]> git.localhorst.tv Git - gong.git/blobdiff - src/geometry/primitive.hpp
sphere/sphere intersection test
[gong.git] / src / geometry / primitive.hpp
index 4711306ab1d0492df33736416fc9cba0199dc586..e785c635cc1ecf15e7b63225eb12f4b968e7194f 100644 (file)
@@ -30,10 +30,22 @@ struct AABB {
                glm::vec3 high(glm::max(glm::abs(min), glm::abs(max)));
                return glm::length(high);
        }
+
+       void Position(const glm::vec3 &center) noexcept {
+               const glm::vec3 halfsize((max - min) * 0.5f);
+               min = center - halfsize;
+               max = center + halfsize;
+       }
+       void Move(const glm::vec3 &delta) noexcept {
+               min += delta;
+               max += delta;
+       }
 };
 
 std::ostream &operator <<(std::ostream &, const AABB &);
 
+bool Intersection(const AABB &, const AABB &) noexcept;
+
 // TODO: this should really use setters/getters for dir and inv_dir so
 //       manipulating code doesn't "forget" to call Update()
 struct Ray {
@@ -87,7 +99,8 @@ bool Intersection(
        float &depth,
        glm::vec3 &normal) noexcept;
 
-
+/// Plane defined by a surface norml and distance to the origin such that
+/// the point (normal * dist) lies on the plane.
 struct Plane {
        glm::vec3 normal;
        float dist;
@@ -115,6 +128,13 @@ struct Plane {
 
 std::ostream &operator <<(std::ostream &, const Plane &);
 
+/// Shortest distance from point to plane.
+float Distance(const glm::vec3 &point, const Plane &plane);
+/// Shortest distance from point to plane with sign indicating whether
+/// it's in front of (positive, in direction of normal) or behind
+/// (negative, counter direction of normal) the surface.
+float SignedDistance(const glm::vec3 &point, const Plane &plane);
+
 struct Frustum {
        Plane plane[6];
        Plane &Left() noexcept { return plane[0]; }
@@ -154,6 +174,47 @@ std::ostream &operator <<(std::ostream &, const Frustum &);
 bool CullTest(const AABB &box, const glm::mat4 &) noexcept;
 bool CullTest(const AABB &box, const Frustum &) noexcept;
 
+struct Sphere {
+
+       glm::vec3 origin;
+       float radius;
+
+       void Position(const glm::vec3 &center) noexcept {
+               origin = center;
+       }
+       void Move(const glm::vec3 &delta) noexcept {
+               origin += delta;
+       }
+
+};
+
+std::ostream &operator <<(std::ostream &, const Sphere &);
+
+/// Two spheres intersection test.
+bool Intersection(
+       const Sphere &,
+       const Sphere &,
+       float &dist,
+       glm::vec3 &norm) noexcept;
+
+/// Test for intersection of sphere with double sided infinite plane.
+/// If true, dist will hold the smaller interpenetration depth and norm
+/// the respective contact normal.
+bool Intersection(
+       const Sphere &sphere,
+       const Plane &plane,
+       float &dist,
+       glm::vec3 &norm) noexcept;
+
+/// Test for intersection of sphere with half space defined by the
+/// backface of given plane.
+/// In all cases, dist will hold the distance between the near points
+/// of plane and sphere. Contact normal will always be the plane's normal.
+bool Intersection(
+       const Sphere &sphere,
+       const Plane &plane,
+       float &dist) noexcept;
+
 }
 }