Implement(ing) Ray class
This commit is contained in:
@@ -5,13 +5,67 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||||
|
#include <vector>
|
||||||
|
#include "TriangleMesh.h"
|
||||||
|
#include "Frustum.h"
|
||||||
|
#include "OBB.h"
|
||||||
|
|
||||||
namespace J3ML::Geometry
|
namespace J3ML::Geometry
|
||||||
{
|
{
|
||||||
using LinearAlgebra::Vector3;
|
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
|
class Ray
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
Vector3 Origin;
|
Vector3 Origin;
|
||||||
|
// The normalized direction vector of this ray.
|
||||||
|
// @note: For proper functionality, this direction vector needs to always be normalized
|
||||||
Vector3 Direction;
|
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<Shape> shapes, float maxDistance = 99999999);
|
||||||
};
|
};
|
||||||
}
|
}
|
Reference in New Issue
Block a user