mirror of
https://github.com/open-goal/jak-project.git
synced 2024-10-20 21:27:52 -04:00
95366d21df
* wip * temp * temp2 * first part of log macros * more log macros * logtest * clean up * dont initialize game info because we are missing stuff
47 lines
1.1 KiB
C++
47 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include "common/math/Vector.h"
|
|
|
|
namespace math {
|
|
|
|
template <typename T>
|
|
T squared(const T& in) {
|
|
return in * in;
|
|
}
|
|
|
|
template <typename T>
|
|
struct RaySphereResult {
|
|
bool hit = false;
|
|
T u[2] = {0, 0};
|
|
};
|
|
|
|
/*!
|
|
* Check if a line intersects a sphere.
|
|
*/
|
|
template <typename T>
|
|
RaySphereResult<T> ray_sphere_intersect(const Vector3<T>& ray_origin,
|
|
const Vector3<T>& ray_direction_in,
|
|
const Vector3<T>& sphere_origin,
|
|
T sphere_radius) {
|
|
RaySphereResult<T> result;
|
|
Vector3<T> ray_direction = ray_direction_in.normalized();
|
|
|
|
Vector3<T> oc = ray_origin - sphere_origin;
|
|
|
|
T value_under_sqrt =
|
|
squared(ray_direction.dot(oc)) - (oc.squared_length() - squared(sphere_radius));
|
|
|
|
if (value_under_sqrt < 0) {
|
|
result.hit = false;
|
|
return result;
|
|
}
|
|
|
|
T minus_b = -ray_direction.dot(oc);
|
|
result.hit = true;
|
|
T sqrt_val = std::sqrt(value_under_sqrt);
|
|
|
|
result.u[0] = minus_b + sqrt_val;
|
|
result.u[1] = minus_b - sqrt_val;
|
|
return result;
|
|
}
|
|
} // namespace math
|