]> git.localhorst.tv Git - blank.git/blobdiff - src/geometry/primitive.hpp
improved ray/world collision a little
[blank.git] / src / geometry / primitive.hpp
index 72da84749140ca6f2d6b2c71a069c3fd9c031199..b9f2187580f5b64e692451553a352c72006e7e42 100644 (file)
@@ -4,6 +4,7 @@
 #include <algorithm>
 #include <iosfwd>
 #include <glm/glm.hpp>
+#include <glm/gtx/norm.hpp>
 
 
 namespace blank {
@@ -42,6 +43,19 @@ struct Ray {
        void Update() noexcept {
                inv_dir = 1.0f / dir;
        }
+
+       /// get shortest distance of this ray's line to given point
+       float Distance(const glm::vec3 &point) const noexcept {
+               // d = |(x2-x1)×(x1-x0)|/|x2-x1|
+               // where x0 is point, and x1 and x2 are points on the line
+               // for derivation, see http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
+               // x1 = orig
+               // x2-x1 = dir, which means |x2-x1| is 1.0
+               return length(cross(dir, orig - point));
+       }
+       float DistanceSquared(const glm::vec3 &point) const noexcept {
+               return length2(cross(dir, orig - point));
+       }
 };
 
 std::ostream &operator <<(std::ostream &, const Ray &);