From 16985b0e9bf0611301f30bd17c8c343ef1209d9a Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sun, 17 Sep 2017 17:02:19 -1000 Subject: [PATCH] Add CPlane::rayPlaneIntersection --- include/zeus/CPlane.hpp | 4 +++- src/CPlane.cpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/zeus/CPlane.hpp b/include/zeus/CPlane.hpp index 3fcc8fa..4797ea2 100644 --- a/include/zeus/CPlane.hpp +++ b/include/zeus/CPlane.hpp @@ -48,11 +48,13 @@ public: d = nd * mag; } - float pointToPlaneDist(const zeus::CVector3f& pos) const + float pointToPlaneDist(const CVector3f& pos) const { return pos.dot(vec) - d; } + bool rayPlaneIntersection(const CVector3f& from, const CVector3f& to, CVector3f& point) const; + const CVector3f& normal() const { return vec; } inline float& operator[](size_t idx) { return p[idx]; } diff --git a/src/CPlane.cpp b/src/CPlane.cpp index 8f3628f..e508295 100644 --- a/src/CPlane.cpp +++ b/src/CPlane.cpp @@ -1 +1,18 @@ #include "zeus/CPlane.hpp" + +namespace zeus +{ + +bool CPlane::rayPlaneIntersection(const CVector3f& from, const CVector3f& to, CVector3f& point) const +{ + zeus::CVector3f delta = to - from; + if (std::fabs(delta.normalized().dot(vec)) < 0.01f) + return false; + float tmp = -pointToPlaneDist(from) / delta.dot(vec); + if (tmp < -0.f || tmp > 1.0001f) + return false; + point = delta * tmp + from; + return true; +} + +}