From e8ed68f3c70e80fd422de2cf2acc59a5f7641c93 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 7 Mar 2024 00:40:12 -0500 Subject: [PATCH] Implement(ing) Ray class --- include/J3ML/Geometry/Ray.h | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/include/J3ML/Geometry/Ray.h b/include/J3ML/Geometry/Ray.h index 053a7ed..414e159 100644 --- a/include/J3ML/Geometry/Ray.h +++ b/include/J3ML/Geometry/Ray.h @@ -5,13 +5,67 @@ #pragma once #include +#include +#include "TriangleMesh.h" +#include "Frustum.h" +#include "OBB.h" namespace J3ML::Geometry { using LinearAlgebra::Vector3; + + + class Shape {}; + + // RaycastResult structure containing the first object the ray collides with, + // the surface intersection point, + // and the surface normal at the point of intersection. + struct RaycastResult + { + Vector3 Intersection; + Vector3 SurfaceNormal; + Shape Hit; + }; + + // A ray in 3D space is a line that starts from an origin point and extends to infinity in one direction class Ray { + public: Vector3 Origin; + // The normalized direction vector of this ray. + // @note: For proper functionality, this direction vector needs to always be normalized Vector3 Direction; + Ray() {} + Ray(const Vector3& pos, const Vector3& dir); + //explicit Ray(const Line& line); + explicit Ray(const LineSegment& lineSegment); + bool IsFinite() const; + Vector3 GetPoint(float distance) const; + RaycastResult Cast(const Triangle& target, float maxDistance = 99999999); + RaycastResult Cast(const Plane& target, float maxDistance = 99999999); + RaycastResult Cast(const AABB& target, float maxDistance = 99999999); + // https://gdbooks.gitbooks.io/3dcollisions/content/Chapter3/raycast_sphere.html + RaycastResult Cast(const Sphere& target, float maxDistance = 99999999) + { + Vector3 p0 = this->Origin; + Vector3 d = this->Direction.Normalize(); + + Vector3 c = target.Position; + float r = target.Radius; + + Vector3 e = c - p0; + float Esq = Vector3::LengthSquared(e); + float a = Vector3::Dot(e, d); + + } + RaycastResult Cast(const OBB& target, float maxDistance = 99999999); + RaycastResult Cast(const Capsule& target, float maxDistance = 99999999); + RaycastResult Cast(const Frustum& target, float maxDistance = 99999999); + RaycastResult Cast(const TriangleMesh& target, float maxDistance = 9999999); + + // Returns a RaycastResult structure containing the first object the ray collides with, + // the surface intersection point, + // and the surface normal at the point of intersection. + RaycastResult Cast(std::vector shapes, float maxDistance = 99999999); }; } \ No newline at end of file