diff --git a/engine/inc/physics/ray.hpp b/engine/inc/physics/ray.hpp index a94e3e4..c58bf13 100644 --- a/engine/inc/physics/ray.hpp +++ b/engine/inc/physics/ray.hpp @@ -29,34 +29,17 @@ class Ray { Ray(const Vec4& origin, const Vec4& direction); ~Ray(); - // Methods - - /** - * @param origin Vec4 starting point of the ray; - * @param direction Vec4 normalized vector pointing to direction; - */ - void set(const Vec4& origin, const Vec4& direction); - - /** - * @brief Set origin of the ray; - * @param origin Vec4 starting point of the ray; - */ - void setOrigin(const Vec4& origin); - - /** - * @brief Set direction of the ray; - * @param direction Vec4 normalized vector pointing to direction; - */ - void setDirection(const Vec4& direction); + Vec4 origin; + Vec4 direction; /** * @return Vec4 that is a given distance along this Ray * @param t - the distance along the Ray to retrieve a position for. */ - Vec4 at(const float& t); + Vec4 at(const float& t) const; /** @return Distance from the Vec4 point to the origin */ - float distanceToPoint(const Vec4& point); + float distanceToPoint(const Vec4& point) const; /** * @param minCorner - pointer to min corner position. (bottom left) @@ -64,15 +47,11 @@ class Ray { * @param distance - the box to intersect with. * @return Vec4 point of intersection */ - u8 intersectBox(const Vec4& minCorner, const Vec4& maxCorner, - float& distance); + bool intersectBox(const Vec4& minCorner, const Vec4& maxCorner, + float& distance) const; /** Returns inverse direction */ - const Vec4 invDir(); - - private: - Vec4 _origin; - Vec4 _direction; + Vec4 invDir() const; }; } // namespace Tyra \ No newline at end of file diff --git a/engine/src/physics/ray.cpp b/engine/src/physics/ray.cpp index 73d5f40..45eb9a3 100644 --- a/engine/src/physics/ray.cpp +++ b/engine/src/physics/ray.cpp @@ -16,40 +16,28 @@ namespace Tyra { Ray::Ray() {} -Ray::Ray(const Vec4& origin, const Vec4& direction) { - this->_origin.set(origin); - this->_direction.set(direction); +Ray::Ray(const Vec4& t_origin, const Vec4& t_direction) { + origin.set(t_origin); + direction.set(t_direction); } Ray::~Ray() {} -void Ray::set(const Vec4& origin, const Vec4& direction) { - this->_origin.set(origin); - this->_direction.set(direction); +Vec4 Ray::at(const float& t) const { return (direction * t) + origin; } + +float Ray::distanceToPoint(const Vec4& point) const { + return origin.distanceTo(point); } -void Ray::setOrigin(const Vec4& origin) { this->_origin.set(origin); } +bool Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner, + float& distance) const { + auto inv = invDir(); + inv.normalize(); -void Ray::setDirection(const Vec4& direction) { - this->_direction.set(direction); -} - -Vec4 Ray::at(const float& t) { return (this->_direction * t) + this->_origin; } - -float Ray::distanceToPoint(const Vec4& point) { - return this->_origin.distanceTo(point); -} - -u8 Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner, - float& distance) { - float tmin, tmax, tymin, tymax, tzmin, tzmax; - Vec4 invDir = this->invDir(); - invDir.normalize(); - - tmin = (minCorner.x - this->_origin.x) * invDir.x; - tmax = (maxCorner.x - this->_origin.x) * invDir.x; - tymin = (minCorner.y - this->_origin.y) * invDir.y; - tymax = (maxCorner.y - this->_origin.y) * invDir.y; + float tmin = (minCorner.x - this->origin.x) * inv.x; + float tmax = (maxCorner.x - this->origin.x) * inv.x; + float tymin = (minCorner.y - this->origin.y) * inv.y; + float tymax = (maxCorner.y - this->origin.y) * inv.y; if ((tmin > tymax) || (tymin > tmax)) { distance = -1.0f; @@ -60,8 +48,8 @@ u8 Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner, if (tymax < tmax) tmax = tymax; - tzmin = (minCorner.z - this->_origin.z) * invDir.z; - tzmax = (maxCorner.z - this->_origin.z) * invDir.z; + float tzmin = (minCorner.z - this->origin.z) * inv.z; + float tzmax = (maxCorner.z - this->origin.z) * inv.z; if ((tmin > tzmax) || (tzmin > tmax)) { distance = -1.0f; @@ -81,9 +69,9 @@ u8 Ray::intersectBox(const Vec4& minCorner, const Vec4& maxCorner, return 1; } -const Vec4 Ray::invDir() { - return Vec4(1 / this->_direction.x, 1 / this->_direction.y, - 1 / this->_direction.z, 1); +Vec4 Ray::invDir() const { + return Vec4(1 / this->direction.x, 1 / this->direction.y, + 1 / this->direction.z, 1); } } // Namespace Tyra \ No newline at end of file