15 ray3_aabb_result_false()
17 auto r = Ray3AabbResult{};
18 r.intersected =
false;
19 r.start =
r.end = -1.0f;
25 ray3_aabb_result_true(
float start,
float end)
27 auto r = Ray3AabbResult{};
49 const vec3f r_invdir = 1.0f /
static_cast<vec3f>(
r.dir);
52 r_invdir.
x < 0 ? 1 : 0,
53 r_invdir.
y < 0 ? 1 : 0,
54 r_invdir.
z < 0 ? 1 : 0
57 float tmin = (bounds[r_sign[0]].x -
r.from.x) * r_invdir.
x;
58 float tmax = (bounds[1 - r_sign[0]].x -
r.from.x) * r_invdir.
x;
59 const float tymin = (bounds[r_sign[1]].y -
r.from.y) * r_invdir.
y;
60 const float tymax = (bounds[1 - r_sign[1]].y -
r.from.y) * r_invdir.
y;
62 if((tmin > tymax) || (tymin > tmax))
64 return ray3_aabb_result_false();
75 const float tzmin = (bounds[r_sign[2]].z -
r.from.z) * r_invdir.
z;
76 const float tzmax = (bounds[1 - r_sign[2]].z -
r.from.z) * r_invdir.
z;
78 if((tmin > tzmax) || (tzmin > tmax))
80 return ray3_aabb_result_false();
92 return ray3_aabb_result_true(tmin, tmax);
103 return -(
r.from.dot(
p.normal) +
p.distance) /
r.dir.dot(
p.normal);
110 ray2_ray2_result_parallel()
112 return {
false,
true,
zero2f, -1.0f, -1.0f};
116 ray2_ray2_result_no_collision()
118 return {
false,
false,
zero2f, -1.0f, -1.0f};
122 ray2_ray2_result_collided(
const vec2f& p,
float a,
float b)
124 return {
false,
true,
p,
a,
b};
142 const float p0_x = p1.
x;
143 const float p0_y = p1.
y;
144 const float p1_x = p2.
x;
145 const float p1_y = p2.
y;
146 const float p2_x = p3.
x;
147 const float p2_y = p3.
y;
148 const float p3_x = p4.
x;
149 const float p3_y = p4.
y;
151 const float s1_x = p1_x - p0_x;
152 const float s1_y = p1_y - p0_y;
153 const float s2_x = p3_x - p2_x;
154 const float s2_y = p3_y - p2_y;
156 const float den = (-s2_x * s1_y + s1_x * s2_y);
159 if(
abs(den) < 0.00001f)
161 return ray2_ray2_result_parallel();
164 const float s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / den;
165 const float t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / den;
167 if(
s >= 0 && s <= 1 && t >= 0 &&
t <= 1)
169 const float x = p0_x + (
t * s1_x);
170 const float y = p0_y + (
t * s1_y);
171 return ray2_ray2_result_collided(
vec2f(
x,
y),
s,
t);
174 return ray2_ray2_result_no_collision();
195 return point - distance * plane.
normal;
206 const auto new_normalized = (point - ray.
from).get_normalized();
208 const auto d = new_normalized.dot(ray.
dir);
209 return abs(1.0f -
d);
219 const auto& ab = ray;
220 const auto a = ray.
from;
222 auto t = (
c -
a).
dot(ab.dir) / ab.dir.dot(ab.dir);
235 const vec3f& lhs_center,
237 const vec3f& rhs_center
248 const vec3f& sphere_center,
260 const vec3f& sphere_center,
273 const vec3f& sphere_center
276 const vec3f oc = ray.
from - sphere_center;
278 const auto b = 2.0f * oc.
dot(ray.
dir);
280 const auto discriminant =
b*
b - 4*
a*
c;
281 if (discriminant < 0) {
285 return (-
b -
sqrt(discriminant) ) / (2.0f*
a);
301 point.
x >= aabb.
min.
x &&
302 point.
y >= aabb.
min.
y &&
303 point.
z >= aabb.
min.
z &&
305 point.
x <= aabb.
max.
x &&
306 point.
y <= aabb.
max.
y &&
307 point.
z <= aabb.
max.
z;
320 #define VEC(N) (point.N > aabb.max.N ? aabb.max.N : (point.N < aabb.min.N ? aabb.min.N : point.N))
335 const auto s1 =
c.y -
a.y;
336 const auto s2 =
c.x -
a.x;
337 const auto s3 =
b.y -
a.y;
338 const auto s4 =
p.y -
a.y;
340 const auto w1 = (
a.x * s1 + s4 * s2 -
p.x * s1) / (s3 * s2 - (
b.x -
a.x) * s1);
341 const auto w2 = (s4 - w1*s3) / s1;
342 return w1 >= 0 && w2 >= 0 && (w1 + w2) <= 1;
357 constexpr
float epsilon = 0.0000001f;
359 const auto edge1 = v1 - v0;
360 const auto edge2 = v2 - v0;
362 const auto a = edge1.dot(
h);
363 if (
a > -epsilon &&
a < epsilon)
368 const auto f = 1.0f/
a;
369 const auto s = ray.
from - v0;
370 const auto u =
f *
s.dot(
h);
371 if (u < 0.0f || u > 1.0f)
376 const auto q =
s.cross(edge1);
378 if (v < 0.0f || u + v > 1.0f)
383 const auto t =
f * edge2.dot(
q);
bool contains_point(const Sphere &sphere, const vec3f &sphere_center, const vec3f &point)
vec3f get_closest_point(const Plane &plane, const vec3f &point)
float get_distance_between(const Plane &plane, const vec3f &p)
bool is_point_in_triangle(const vec2f &a, const vec2f &b, const vec2f &c, const vec2f &p)
std::optional< float > get_intersection_ray_triangle(const UnitRay3f &ray, const vec3f &v0, const vec3f &v1, const vec3f &v2)
This implements the Möller–Trumbore intersection algorithm.
std::optional< float > get_intersection(const UnitRay3f &ray, const CollisionMesh &mesh)
constexpr float abs(float r)
size2f max(const size2f lhs, const size2f rhs)
float dot(const quatf &lhs, const quatf &rhs)
vec3f get_point(float at) const
static UnitRay3f from_to(const vec3f &from, const vec3f &to)
constexpr float get_length_squared() const
vec3f cross(const vec3f &u) const
static vec3f from_to(const vec3f &from, const vec3f &to)
float dot(const vec3f &rhs) const