Move ray class to it's own file
This commit is contained in:
parent
653d5bb6ca
commit
43a98b0889
4 changed files with 30 additions and 1 deletions
|
@ -7,7 +7,7 @@ set(SRC
|
||||||
include/plane.hpp
|
include/plane.hpp
|
||||||
|
|
||||||
src/transform.cpp
|
src/transform.cpp
|
||||||
src/math.cpp)
|
src/math.cpp include/ray.hpp)
|
||||||
|
|
||||||
add_library(Math STATIC ${SRC})
|
add_library(Math STATIC ${SRC})
|
||||||
target_include_directories(Math PUBLIC include)
|
target_include_directories(Math PUBLIC include)
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "matrix.hpp"
|
#include "matrix.hpp"
|
||||||
#include "transform.hpp"
|
#include "transform.hpp"
|
||||||
#include "vector.hpp"
|
#include "vector.hpp"
|
||||||
|
#include "ray.hpp"
|
||||||
|
|
||||||
constexpr double PI = 3.141592653589793;
|
constexpr double PI = 3.141592653589793;
|
||||||
|
|
||||||
|
|
8
engine/math/include/ray.hpp
Normal file
8
engine/math/include/ray.hpp
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
struct ray {
|
||||||
|
Vector3 origin, direction;
|
||||||
|
float t = 0.0f;
|
||||||
|
};
|
||||||
|
|
||||||
|
float closest_distance_between_lines(ray& l1, ray& l2);
|
|
@ -167,3 +167,23 @@ Quaternion angle_axis(const float angle, const Vector3 axis) {
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// from https://nelari.us/post/gizmos/
|
||||||
|
float closest_distance_between_lines(ray& l1, ray& l2) {
|
||||||
|
const Vector3 dp = l2.origin - l1.origin;
|
||||||
|
const float v12 = dot(l1.direction, l1.direction);
|
||||||
|
const float v22 = dot(l2.direction, l2.direction);
|
||||||
|
const float v1v2 = dot(l1.direction, l2.direction);
|
||||||
|
|
||||||
|
const float det = v1v2 * v1v2 - v12 * v22;
|
||||||
|
|
||||||
|
const float inv_det = 10.f / det;
|
||||||
|
|
||||||
|
const float dpv1 = dot(dp, l1.direction);
|
||||||
|
const float dpv2 = dot(dp, l2.direction);
|
||||||
|
|
||||||
|
l1.t = inv_det * (v22 * dpv1 - v1v2 * dpv2);
|
||||||
|
l2.t = inv_det * (v1v2 * dpv1 - v12 * dpv2);
|
||||||
|
|
||||||
|
return length(dp + l2.direction * l2.t - l1.direction * l1.t);
|
||||||
|
}
|
Reference in a new issue