Massive Refactor
This commit is contained in:
@@ -29,43 +29,7 @@ file(GLOB_RECURSE J3ML_SRC "src/J3ML/*.c" "src/J3ML/*.cpp")
|
||||
include_directories("include")
|
||||
|
||||
add_library(J3ML SHARED ${J3ML_SRC}
|
||||
src/J3ML/LinearAlgebra/AxisAngle.cpp
|
||||
include/J3ML/LinearAlgebra/Vector.h
|
||||
include/J3ML/Geometry/Plane.h
|
||||
include/J3ML/Geometry/AABB.h
|
||||
include/J3ML/Geometry/Frustum.h
|
||||
include/J3ML/Geometry/OBB.h
|
||||
include/J3ML/Geometry/Capsule.h
|
||||
include/J3ML/Geometry/Sphere.h
|
||||
include/J3ML/Geometry/Ray.h
|
||||
include/J3ML/Geometry/QuadTree.h
|
||||
include/J3ML/Geometry/LineSegment.h
|
||||
include/J3ML/Geometry/TriangleMesh.h
|
||||
include/J3ML/Geometry/Polygon.h
|
||||
include/J3ML/Geometry/Triangle.h
|
||||
include/J3ML/Geometry/Triangle2D.h
|
||||
src/J3ML/Geometry/AABB.cpp
|
||||
src/J3ML/Geometry/Plane.cpp
|
||||
src/J3ML/Geometry/Sphere.cpp
|
||||
src/J3ML/Geometry/Frustum.cpp
|
||||
src/J3ML/Geometry/OBB.cpp
|
||||
src/J3ML/Geometry/Ray.cpp
|
||||
src/J3ML/Geometry/Capsule.cpp
|
||||
src/J3ML/Geometry/TriangleMesh.cpp
|
||||
src/J3ML/Geometry/QuadTree.cpp
|
||||
src/J3ML/Geometry/LineSegment.cpp
|
||||
include/J3ML/Geometry/AABB2D.h
|
||||
src/J3ML/Geometry/Polygon.cpp
|
||||
include/J3ML/Geometry/Polyhedron.h
|
||||
src/J3ML/Geometry/Polyhedron.cpp
|
||||
include/J3ML/Algorithm/RNG.h
|
||||
src/J3ML/Algorithm/RNG.cpp
|
||||
include/J3ML/Algorithm/Spring.h
|
||||
include/J3ML/Algorithm/DifferentialSolvers.h
|
||||
include/J3ML/Units.h
|
||||
src/J3ML/J3ML.cpp
|
||||
include/J3ML/LinearAlgebra/Matrix.h
|
||||
src/J3ML/Geometry/AABB2D.cpp)
|
||||
include/J3ML/Geometry/Common.h)
|
||||
set_target_properties(J3ML PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
|
||||
|
@@ -1,32 +1,22 @@
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace J3ML::Geometry {
|
||||
using Vector2 = J3ML::LinearAlgebra::Vector2;
|
||||
using Vector3 = J3ML::LinearAlgebra::Vector3;
|
||||
#include <J3ML/Geometry/AABB2D.h>
|
||||
#include <J3ML/Geometry/Plane.h>
|
||||
#include <J3ML/Geometry/Sphere.h>
|
||||
#include <J3ML/Geometry/LineSegment.h>
|
||||
#include <J3ML/Geometry/Frustum.h>
|
||||
#include <J3ML/Geometry/OBB.h>
|
||||
#include <J3ML/Geometry/Capsule.h>
|
||||
#include <J3ML/Geometry/AABB.h>
|
||||
#include <J3ML/Geometry/Polyhedron.h>
|
||||
#include <J3ML/Geometry/QuadTree.h>
|
||||
#include <J3ML/Geometry/Ray.h>
|
||||
#include <J3ML/Geometry/Shape.h>
|
||||
#include <J3ML/Geometry/Sphere.h>
|
||||
#include <J3ML/Geometry/Triangle.h>
|
||||
#include <J3ML/Geometry/Triangle2D.h>
|
||||
#include <J3ML/Geometry/TriangleMesh.h>
|
||||
|
||||
class LineSegment2D
|
||||
{
|
||||
Vector2 A;
|
||||
Vector2 B;
|
||||
};
|
||||
|
||||
class Rectangle;
|
||||
class AABB;
|
||||
class OBB;
|
||||
class Capsule;
|
||||
class Frustum;
|
||||
class OBB2D;
|
||||
class Line2D;
|
||||
class Ray2D;
|
||||
class Triangle2D;
|
||||
class Polygon2D;
|
||||
|
||||
|
||||
struct IntersectionResult2D {};
|
||||
|
||||
bool Intersects2D(LineSegment2D seg, Rectangle rect);
|
||||
IntersectionResult2D GetIntersection2D(LineSegment2D seg, Rectangle rect);
|
||||
}
|
||||
using namespace J3ML::Geometry;
|
@@ -1,30 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
|
||||
#include "J3ML/LinearAlgebra.h"
|
||||
#include <J3ML/Geometry.h>
|
||||
|
||||
#include <J3ML/Geometry/Plane.h>
|
||||
#include <J3ML/Geometry/Sphere.h>
|
||||
#include <J3ML/Geometry/OBB.h>
|
||||
#include <J3ML/Geometry/LineSegment.h>
|
||||
#include <J3ML/Geometry/Triangle.h>
|
||||
#include <J3ML/Geometry/Polygon.h>
|
||||
#include <J3ML/Geometry/Frustum.h>
|
||||
#include <J3ML/Geometry/Capsule.h>
|
||||
#include <J3ML/Geometry/Ray.h>
|
||||
#include <J3ML/Geometry/TriangleMesh.h>
|
||||
#include <J3ML/Geometry/Polyhedron.h>
|
||||
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
#include <J3ML/Geometry/Common.h>
|
||||
#include <J3ML/Geometry/Shape.h>
|
||||
|
||||
// TODO: Fix circular include between AABB and OBB
|
||||
|
||||
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
using namespace LinearAlgebra;
|
||||
using namespace J3ML::LinearAlgebra;
|
||||
// A 3D axis-aligned bounding box
|
||||
// This data structure can be used to represent coarse bounds of objects, in situations where detailed triangle-level
|
||||
// computations can be avoided. In physics systems, bounding boxes are used as an efficient early-out test for geometry
|
||||
@@ -34,11 +18,15 @@ namespace J3ML::Geometry
|
||||
// be arbitrarily oriented in the space with respect to each other.
|
||||
// If you need to represent a box in 3D space with arbitrary orientation, see the class OBB. */
|
||||
|
||||
class AABB {
|
||||
class AABB : public Shape {
|
||||
public:
|
||||
Vector3 minPoint;
|
||||
Vector3 maxPoint;
|
||||
|
||||
AABB();
|
||||
|
||||
AABB(const Vector3& min, const Vector3& max);
|
||||
|
||||
static int NumFaces() { return 6; }
|
||||
|
||||
static int NumEdges() { return 12; }
|
||||
@@ -102,6 +90,7 @@ namespace J3ML::Geometry
|
||||
static AABB MinimalEnclosingAABB(const Vector3 *pointArray, int numPoints);
|
||||
float GetVolume() const;
|
||||
float GetSurfaceArea() const;
|
||||
Vector3 GetClosestPoint(const Vector3& point) const;
|
||||
Vector3 GetRandomPointInside();
|
||||
Vector3 GetRandomPointOnSurface();
|
||||
Vector3 GetRandomPointOnEdge();
|
||||
|
@@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include "Shape.h"
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
using LinearAlgebra::Vector2;
|
||||
// CaveGame AABB
|
||||
class AABB2D
|
||||
class AABB2D : public Shape2D
|
||||
{
|
||||
public:
|
||||
|
||||
|
@@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "LineSegment.h"
|
||||
#include "Shape.h"
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
using namespace LinearAlgebra;
|
||||
class Capsule
|
||||
class Capsule : public Shape
|
||||
{
|
||||
// Specifies the two inner points of this capsule
|
||||
LineSegment l;
|
||||
|
30
include/J3ML/Geometry/Common.h
Normal file
30
include/J3ML/Geometry/Common.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
// Forward declarations for classes that include each other
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
class Shape;
|
||||
class AABB2D;
|
||||
class AABB;
|
||||
class Capsule;
|
||||
class Frustum;
|
||||
class LineSegment;
|
||||
class OBB;
|
||||
class Plane;
|
||||
class Polygon;
|
||||
class Polyhedron;
|
||||
template<typename T>class QuadTree;
|
||||
class Ray;
|
||||
class Shape;
|
||||
class Sphere;
|
||||
class Triangle;
|
||||
class Triangle2D;
|
||||
class TriangleMesh;
|
||||
}
|
||||
|
||||
// Methods required by Geometry types
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
|
||||
}
|
@@ -4,6 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Plane.h"
|
||||
#include "Shape.h"
|
||||
#include <J3ML/LinearAlgebra/CoordinateFrame.h>
|
||||
|
||||
namespace J3ML::Geometry
|
||||
@@ -27,7 +28,7 @@ namespace J3ML::Geometry
|
||||
Perspective
|
||||
};
|
||||
|
||||
class Frustum {
|
||||
class Frustum : public Shape {
|
||||
public:
|
||||
Plane TopFace;
|
||||
Plane BottomFace;
|
||||
|
@@ -1,31 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/Geometry.h>
|
||||
#include <J3ML/Geometry/Common.h>
|
||||
#include <J3ML/Geometry/AABB.h>
|
||||
#include <J3ML/Geometry/LineSegment.h>
|
||||
#include <J3ML/Geometry/Polyhedron.h>
|
||||
|
||||
|
||||
namespace J3ML::Geometry {
|
||||
class OBB
|
||||
class OBB : public Shape
|
||||
{
|
||||
public:
|
||||
// The center position of this OBB
|
||||
Vector3 pos;
|
||||
// Stores half-sizes to x, y, and z directions in the local space of this OBB.
|
||||
Vector3 r;
|
||||
// Specifies normalized direc tion vectors for the local axes
|
||||
// Specifies normalized direction vectors for the local axes
|
||||
Vector3 axis[3];
|
||||
|
||||
OBB() {}
|
||||
OBB(const Vector3& pos, const Vector3& radii, const Vector3& axis0, const Vector3& axis1, const Vector3& axis2);
|
||||
OBB(const Geometry::AABB& aabb);
|
||||
OBB(const AABB& aabb);
|
||||
inline static int NumFaces() { return 6; }
|
||||
inline static int NumEdges() { return 12; }
|
||||
inline static int NumVertices() { return 8; }
|
||||
|
||||
Polyhedron ToPolyhedron() const;
|
||||
|
||||
Geometry::AABB MinimalEnclosingAABB() const;
|
||||
AABB MinimalEnclosingAABB() const
|
||||
{
|
||||
AABB aabb;
|
||||
aabb.SetFrom(*this);
|
||||
return aabb;
|
||||
}
|
||||
|
||||
Sphere MinimalEnclosingSphere() const;
|
||||
Sphere MaximalContainedSphere() const;
|
||||
|
@@ -1,15 +1,18 @@
|
||||
#pragma once
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
|
||||
#include "Shape.h"
|
||||
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
using J3ML::LinearAlgebra::Vector3;
|
||||
|
||||
class Plane
|
||||
class Plane : public Shape
|
||||
{
|
||||
public:
|
||||
Plane() : Shape() {}
|
||||
Plane(const Vector3& pos, const Vector3& norm)
|
||||
: Shape(), Position(pos), Normal(norm) {}
|
||||
Vector3 Position;
|
||||
Vector3 Normal;
|
||||
float distance = 0.f;
|
||||
|
@@ -1,7 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
namespace J3ML::Geometry {
|
||||
class Polygon {
|
||||
#include "Shape.h"
|
||||
|
||||
namespace J3ML::Geometry {
|
||||
class Polygon : public Shape
|
||||
{
|
||||
public:
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
}
|
@@ -1,8 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/Geometry/Shape.h>
|
||||
#include <vector>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
class Polyhedron {
|
||||
using namespace J3ML::LinearAlgebra;
|
||||
|
||||
// Represents a three-dimensional closed geometric solid defined by flat polygonal faces.
|
||||
class Polyhedron : public Shape
|
||||
{
|
||||
public:
|
||||
// Stores a list of indices of a single face of a Polygon
|
||||
struct Face
|
||||
{
|
||||
// Specifies the indices of the corner vertices of the polyhedron.
|
||||
// Indices point to the polyhedron vertex array.
|
||||
// The face vertices should all lie on the same plane.
|
||||
// The positive direction of the plane (the direction the face outwards normal points)
|
||||
// is the one where the vertices are wound in counter-clockwise order.
|
||||
std::vector<int> v;
|
||||
|
||||
// Reverses the winding order of this face. This has the effect of reversing the direction
|
||||
// the normal of this face points to.
|
||||
void FlipWindingOrder();
|
||||
|
||||
};
|
||||
|
||||
// Specifies the vertices of this polyhedron.
|
||||
std::vector<Vector3> v;
|
||||
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
}
|
@@ -12,11 +12,9 @@
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
using LinearAlgebra::Vector3;
|
||||
using J3ML::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.
|
||||
@@ -24,13 +22,16 @@ namespace J3ML::Geometry
|
||||
{
|
||||
Vector3 Intersection;
|
||||
Vector3 SurfaceNormal;
|
||||
Shape Hit;
|
||||
bool Hit;
|
||||
Shape* Target;
|
||||
static RaycastResult NoHit() { return {Vector3::NaN, Vector3::NaN, false, nullptr};}
|
||||
};
|
||||
|
||||
// A ray in 3D space is a line that starts from an origin point and extends to infinity in one direction
|
||||
class Ray
|
||||
{
|
||||
public:
|
||||
// The position of this ray.
|
||||
Vector3 Origin;
|
||||
// The normalized direction vector of this ray.
|
||||
// @note: For proper functionality, this direction vector needs to always be normalized
|
||||
@@ -45,19 +46,7 @@ namespace J3ML::Geometry
|
||||
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 Sphere& target, float maxDistance = 99999999);
|
||||
RaycastResult Cast(const OBB& target, float maxDistance = 99999999);
|
||||
RaycastResult Cast(const Capsule& target, float maxDistance = 99999999);
|
||||
RaycastResult Cast(const Frustum& target, float maxDistance = 99999999);
|
||||
|
26
include/J3ML/Geometry/Shape.h
Normal file
26
include/J3ML/Geometry/Shape.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
class GeometricPrimitive
|
||||
{
|
||||
public:
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
class Shape
|
||||
{
|
||||
public:
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
class Shape2D
|
||||
{
|
||||
public:
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
}
|
@@ -5,6 +5,7 @@
|
||||
#include <J3ML/LinearAlgebra/Matrix4x4.h>
|
||||
#include <J3ML/Geometry/LineSegment.h>
|
||||
#include <J3ML/Geometry/TriangleMesh.h>
|
||||
#include <J3ML/Geometry/Shape.h>
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
@@ -12,7 +13,7 @@ namespace J3ML::Geometry
|
||||
using J3ML::LinearAlgebra::Matrix4x4;
|
||||
|
||||
// A mathematical representation of a 3-dimensional sphere
|
||||
class Sphere
|
||||
class Sphere : public Shape
|
||||
{
|
||||
public:
|
||||
Vector3 Position;
|
||||
|
@@ -4,6 +4,9 @@ namespace J3ML::Geometry
|
||||
{
|
||||
class Triangle
|
||||
{
|
||||
|
||||
public:
|
||||
Vector3 V0;
|
||||
Vector3 V1;
|
||||
Vector3 V2;
|
||||
};
|
||||
}
|
@@ -3,7 +3,6 @@
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
class Shape2D {};
|
||||
class Triangle2D {
|
||||
public:
|
||||
};
|
||||
|
@@ -1,11 +1,22 @@
|
||||
#include <J3ML/Geometry/AABB.h>
|
||||
#include <cassert>
|
||||
#include <J3ML/Geometry/Plane.h>
|
||||
#include <J3ML/Geometry/Sphere.h>
|
||||
//#include <J3ML/Geometry/OBB.h>
|
||||
#include <J3ML/Geometry/LineSegment.h>
|
||||
#include <J3ML/Geometry/Triangle.h>
|
||||
#include <J3ML/Geometry/Polygon.h>
|
||||
#include <J3ML/Geometry/Frustum.h>
|
||||
#include <J3ML/Geometry/Capsule.h>
|
||||
#include <J3ML/Geometry/Ray.h>
|
||||
#include <J3ML/Geometry/TriangleMesh.h>
|
||||
#include <J3ML/Geometry/Polyhedron.h>
|
||||
|
||||
namespace J3ML::Geometry {
|
||||
|
||||
AABB AABB::FromCenterAndSize(const J3ML::Geometry::Vector3 ¢er, const J3ML::Geometry::Vector3 &size) {
|
||||
Vector3 halfSize = size * 0.5f;
|
||||
return {center - halfSize, center + halfSize};
|
||||
return AABB{center - halfSize, center + halfSize};
|
||||
}
|
||||
|
||||
float AABB::MinX() const { return minPoint.x; }
|
||||
@@ -234,4 +245,35 @@ namespace J3ML::Geometry {
|
||||
Vector3 d = obb.r.x * absAxis0 + obb.r.y * absAxis1 + obb.r.z * absAxis2;
|
||||
}
|
||||
|
||||
Vector3 AABB::GetClosestPoint(const Vector3 &point) const {
|
||||
Vector3 result = point;
|
||||
if (point.x > this->maxPoint.x)
|
||||
result.x = this->maxPoint.x;
|
||||
else if (point.x < this->minPoint.x)
|
||||
result.x = this->minPoint.x;
|
||||
else
|
||||
result.x = point.x;
|
||||
|
||||
if (point.y > this->maxPoint.y)
|
||||
result.y = this->maxPoint.y;
|
||||
else if (point.y < this->minPoint.y)
|
||||
result.y = this->minPoint.y;
|
||||
else
|
||||
result.y = point.y;
|
||||
|
||||
if (point.z > this->maxPoint.z)
|
||||
result.z = this->maxPoint.z;
|
||||
else if (point.z < this->minPoint.z)
|
||||
result.z = this->minPoint.z;
|
||||
else
|
||||
result.z = point.z;
|
||||
}
|
||||
|
||||
AABB::AABB(const Vector3 &min, const Vector3 &max) : Shape(), minPoint(min), maxPoint(max)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
AABB::AABB() : Shape() {}
|
||||
|
||||
}
|
||||
|
@@ -1,3 +1,48 @@
|
||||
//
|
||||
// Created by dawsh on 1/25/24.
|
||||
//
|
||||
#include <J3ML/Geometry/Shape.h>
|
||||
#include <J3ML/Geometry/AABB.h>
|
||||
#include <J3ML/Geometry/LineSegment.h>
|
||||
#include <J3ML/Geometry/Polyhedron.h>
|
||||
#include <J3ML/Geometry/OBB.h>
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
|
||||
Polyhedron OBB::ToPolyhedron() const {
|
||||
// Note to maintainer: This function is an exact copy of AABB::ToPolyhedron() and Frustum::ToPolyhedron()
|
||||
|
||||
Polyhedron p;
|
||||
// populate the corners of this OBB
|
||||
// this will be in the order 0: ---, 1: --+, 2: -+-, 3: -++, 4: +--, 5: +-+, 6: ++-, 7: +++
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
p.v.push_back(CornerPoint(i));
|
||||
}
|
||||
|
||||
// generate the 6 faces of this OBB.
|
||||
const int faces[6][4] =
|
||||
{
|
||||
{0, 1, 3, 2}, // X-
|
||||
{4, 6, 7, 5}, // X+
|
||||
{0, 4, 5, 1}, // Y-
|
||||
{7, 6, 2, 3}, // Y+
|
||||
{0, 2, 6, 4}, // Z-
|
||||
{1, 5, 7, 3} // Z+
|
||||
};
|
||||
|
||||
for (int f = 0; f < 6; ++f)
|
||||
{
|
||||
Polyhedron::Face face;
|
||||
for (int v = 0; v < 4; ++v)
|
||||
{
|
||||
face.v.push_back(faces[f][v]);
|
||||
}
|
||||
//p.f.push_back(face);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
Vector3 OBB::CornerPoint(int cornerIndex) const {
|
||||
// TODO: implement
|
||||
return Vector3();
|
||||
}
|
||||
}
|
@@ -1,6 +1,126 @@
|
||||
#include <J3ML/Geometry/Ray.h>
|
||||
#include <J3ML/Geometry/Sphere.h>
|
||||
|
||||
namespace Geometry
|
||||
|
||||
namespace J3ML::Geometry
|
||||
{
|
||||
RaycastResult Ray::Cast(const Sphere &target, float maxDistance)
|
||||
{
|
||||
Vector3 p0 = this->Origin;
|
||||
Vector3 d = this->Direction.Normalize();
|
||||
|
||||
Vector3 c = target.Position;
|
||||
float r = target.Radius;
|
||||
|
||||
Vector3 e = c - p0;
|
||||
// Using Length here would cause floating point error to creep in
|
||||
float Esq = Vector3::LengthSquared(e);
|
||||
float a = Vector3::Dot(e, d);
|
||||
float b = std::sqrt(Esq - (a*a));
|
||||
float f = std::sqrt((r*r) - (b*b));
|
||||
|
||||
float t = 0;
|
||||
// No collision
|
||||
if (r * r - Esq + a * a < 0.f)
|
||||
{
|
||||
t = -1;
|
||||
} else if ( Esq < r*r) {
|
||||
t = a + f;
|
||||
} else
|
||||
{
|
||||
t = a - f;
|
||||
}
|
||||
|
||||
// TODO: Verify this
|
||||
Vector3 intersection = p0.Project(d*t);
|
||||
Vector3 intersection_from_sphere_origin = (intersection - target.Position);
|
||||
|
||||
Vector3 normal = -intersection_from_sphere_origin.Normalize();
|
||||
|
||||
return RaycastResult{
|
||||
intersection,
|
||||
normal,
|
||||
true,
|
||||
(Shape *) &target
|
||||
};
|
||||
}
|
||||
|
||||
RaycastResult Ray::Cast(const AABB &target, float maxDistance) {
|
||||
float t1 = (target.minPoint.x - Origin.x) / Direction.x;
|
||||
float t2 = (target.maxPoint.x - Origin.x) / Direction.x;
|
||||
float t3 = (target.minPoint.y - Origin.y) / Direction.y;
|
||||
float t4 = (target.maxPoint.y - Origin.y) / Direction.y;
|
||||
float t5 = (target.minPoint.z - Origin.z) / Direction.z;
|
||||
float t6 = (target.maxPoint.z - Origin.z) / Direction.z;
|
||||
|
||||
float tmin = std::max( std::max( std::min(t1, t2), std::min(t3, t4)), std::min(t5, t6));
|
||||
float tmax = std::min( std::min( std::max(t1, t2), std::max(t3, t4)), std::max(t5, t6));
|
||||
|
||||
// if tmax < 0, ray is intersecting aabb, but whole aabb is behind us.
|
||||
if (tmax < 0)
|
||||
return RaycastResult::NoHit();
|
||||
|
||||
// if tmin > tmax, ray doesn't intersect AABB
|
||||
if (tmin > tmax)
|
||||
return RaycastResult::NoHit();
|
||||
|
||||
float t = 0.f;
|
||||
|
||||
if (tmin < 0.f)
|
||||
t = tmax;
|
||||
|
||||
t = tmin;
|
||||
|
||||
Vector3 p0 = this->Origin;
|
||||
Vector3 d = this->Direction.Normalize();
|
||||
|
||||
// TODO: Verify this
|
||||
Vector3 intersection = p0.Project(d*t);
|
||||
|
||||
// WTF: This algorithm is only valid for spheres!!!
|
||||
// TODO: Calculate surfacenormal against rectangle
|
||||
Vector3 intersection_from_sphere_origin = (intersection - target.Centroid());
|
||||
|
||||
Vector3 normal = -intersection_from_sphere_origin.Normalize();
|
||||
|
||||
return RaycastResult
|
||||
{
|
||||
intersection,
|
||||
normal,
|
||||
true,
|
||||
(Shape*)&target
|
||||
};
|
||||
}
|
||||
|
||||
RaycastResult Ray::Cast(const Plane &target, float maxDistance) {
|
||||
float nd = Vector3::Dot(Direction, target.Normal);
|
||||
float pn = Vector3::Dot(Origin, target.Normal);
|
||||
|
||||
if (nd >= 0.f)
|
||||
return RaycastResult::NoHit();
|
||||
|
||||
float t = (target.distance - pn) / nd;
|
||||
|
||||
Vector3 d = this->Direction.Normalize();
|
||||
|
||||
// TODO: verify this
|
||||
Vector3 intersection = this->Origin.Project(d*t);
|
||||
|
||||
// TODO: flip the axis based on direction of incoming ray?
|
||||
// Take dot product
|
||||
Vector3 normal = target.Normal;
|
||||
|
||||
if (t >= 0.f)
|
||||
return RaycastResult
|
||||
{
|
||||
intersection,
|
||||
normal,
|
||||
true,
|
||||
(Shape*) &target
|
||||
};
|
||||
|
||||
return RaycastResult::NoHit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
#include <J3ML/LinearAlgebra/Vector4.h>
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
const Matrix4x4 Matrix4x4::Zero = Matrix4x4(0);
|
||||
const Matrix4x4 Matrix4x4::Zero = Matrix4x4(0.f);
|
||||
const Matrix4x4 Matrix4x4::Identity = Matrix4x4({1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1});
|
||||
const Matrix4x4 Matrix4x4::NaN = Matrix4x4(NAN);
|
||||
|
||||
|
@@ -161,6 +161,7 @@ namespace J3ML::LinearAlgebra {
|
||||
const Vector2 Vector2::Left = {-1, 0};
|
||||
const Vector2 Vector2::Right = {1, 0};
|
||||
const Vector2 Vector2::NaN = {NAN, NAN};
|
||||
const Vector2 Vector2::Infinity = {INFINITY, INFINITY};
|
||||
|
||||
float Vector2::GetX() const { return x; }
|
||||
|
||||
@@ -324,4 +325,12 @@ namespace J3ML::LinearAlgebra {
|
||||
x = scalar;
|
||||
y = scalar;
|
||||
}
|
||||
|
||||
float Vector2::DistanceSq(const Vector2 &to) const {
|
||||
return (*this-to).LengthSquared();
|
||||
}
|
||||
|
||||
float Vector2::DistanceSq(const Vector2 &from, const Vector2 &to) {
|
||||
return (from-to).LengthSquared();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user